featurist / pogoscript

A readable, DSL friendly programming language with excellent concurrency primitives
http://pogoscript.org/
BSD 2-Clause "Simplified" License
130 stars 10 forks source link

Destructuring assignments? #63

Open 00dani opened 9 years ago

00dani commented 9 years ago

There doesn't appear to be any support for destructuring objects or arrays during assignment.

I tried the following code:

{count by, is equal} = require 'underscore'
(s1) is permutation of (s2) =
    (count by (s1)) is equal (count by (s2))

expecting it to produce JS equivalent to:

var temp = require('underscore');
var countBy = temp.countBy, isEqual = temp.isEqual;
var isPermutationOf = function(s1, s2) {
    return isEqual(countBy(s1), countBy(s2));
};

but no dice. It looks like currently this is a syntax error, so adding support shouldn't (?) break existing code.

More generally, destructuring assignment should probably work like:

[x, y] = [1, 2] // x = 1, y = 2
[x, y, ..., z] = [1, 2, 3, 4, 5] // x = 1, y = [2, 3, 4], z = 5
{a, b} = {a = 1, b = 2} // a = 1, b = 2
{x = a, y = b} = {x = 5, y = 7} // a = 5, b = 7

It'd also make sense to support the syntax in function arguments:

map (f) over ([head, tail, ...]) =
  f (head).concat (map (f) over (tail))
  // obviously needs a check for the base case but you get the idea

Thoughts?

joshski commented 9 years ago

+1

joshski commented 9 years ago

As in, I'd like to see destructuring assignments 😀

sphvn commented 9 years ago

Would very much like destructuring assignments also.