You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sweeten transform kicks in only for files with .sjs extension.
Importing macros
You can use a restricted form of ES6 module import statement to import macros in
current module's scope:
import macros from './my-macros.sjs'
import macros from 'some-pkg'
// use everything exported from ./my-macros.sjs or some-pkg node module
Example
As an example, you can see how you can re-use macros already present on npm:
% npm install sparkler
Then create a source file called sparkler-example.sjs:
import macros from 'sparkler/macros';
function myPatterns {
case 42 => 'The meaning of life'
case a @ String => 'Hello ' + a
case [...front, back] => back.concat(front)
case { foo: 'bar', x, 'y' } => x
case Email{ user, domain: 'foo.com' } => user
case (a, b, ...rest) => rest
case [...{ x, y }] => _.zip(x, y)
case x @ Number if x > 10 => x
}
This example uses excellent sparkler
macros library which implements pattern matching for JavaScript. Just run
% browserify -t sweetify ./sparkler-example.sjs
to produce a JavaScript which you can run in a browser or any other runtime (all
pattern matching constructs are compiler into plain JS constructs).