Skip to content

Destructuring assignments? #63

@00dani

Description

@00dani

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions