Skip to content

Instantly share code, notes, and snippets.

@petehunt
Created January 13, 2014 09:14
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petehunt/8396968 to your computer and use it in GitHub Desktop.
Save petehunt/8396968 to your computer and use it in GitHub Desktop.
Sweet.js DSL for making persistent data structures feel imperative
macro := {
rule infix { $obj $([ $key ] ...) | $rval:expr } => {
$obj = mori.assoc_in($obj, [$key (,) ...].reverse(), $rval)
}
}
macro hash_map {
rule {{ $($key : $value) (,) ... }} => {
mori.hash_map($($key, $value) (,) ...)
}
}
// You can use expressions as keys, like Python.
var myName = 'Pete';
var lastNames = hash_map {
myName: 'Hunt',
'Christopher': 'Chedeau'
};
// Save a reference to the original version
var originalLastNames = lastNames;
// Add some new friends
lastNames['Jordan'] := 'Walke';
lastNames['Sebastian'] := 'Markbage';
// This will only have the first two
console.log(originalLastNames);
// This will have all four.
console.log(lastNames);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment