Created
January 13, 2014 09:14
-
-
Save petehunt/8396968 to your computer and use it in GitHub Desktop.
Sweet.js DSL for making persistent data structures feel imperative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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