olleicua / hcl

Hot Cocoa Lisp
GNU General Public License v3.0
34 stars 4 forks source link

Add destructuring bind #5

Closed olleicua closed 11 years ago

olleicua commented 11 years ago

I'm thinking the best way to do this would be to add a form to set (and set+ etc). Something like:

HCL:

(set (a b c) [1 2 3])

(var obj {})
(def foo (# () ["foo" "bar"]))
(set (obj.foo obj.bar) (foo))
;; obj is now {foo "foo" bar "bar"}

JavaScript:

((a=1),(b=2),(c=3),[1,2,3]);

var obj = {};
var foo = function() { return ["foo", "bar"]; };
(function(_array_){ obj.foo = _array_[0]; obj.bar = _array_[1]; return _array_; }).call(this, foo());
olleicua commented 11 years ago

It may also be useful to have a destructuring form of let. Perhaps it can be as simple as if an even indexed element of the first argument of the let is a list then it destructures so that

(let (a 1
      (b c) [2 3])
   (console.log a b c))

produces 1 2 3

olleicua commented 11 years ago

I'm considering allowing splats in destructurings but I'm not sure if it would really be worth it to add the full object destructuring from coffeescript.

see coffeescript destructuring