Skip to content

Instantly share code, notes, and snippets.

@kraftdorian
Last active April 24, 2021 10:42
Show Gist options
  • Save kraftdorian/1bb11d9b9fa8cdd57104c8c477c13467 to your computer and use it in GitHub Desktop.
Save kraftdorian/1bb11d9b9fa8cdd57104c8c477c13467 to your computer and use it in GitHub Desktop.
Tuple in JavaScript written in Scala style
const tuple = (...atoms) => Object.freeze(
atoms.reduce((partial, atom, index) => ({
...partial,
['_' + ++index]: atom
}), {})
);
@kraftdorian
Copy link
Author

kraftdorian commented Apr 23, 2021

Use case

const t = tuple('foo', 'bar', 'baz');
console.log(t);

Result:

{ _1: 'foo', _2: 'bar', _3: 'baz' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment