Skip to content

Instantly share code, notes, and snippets.

@argyleink argyleink/immutables.js
Last active Jun 14, 2019

Embed
What would you like to do?
immutable array examples
const clone = x => [...x]
const push = y => x => [...x, y]
const pop = x => x.slice(0,-1)
const unshift = y => x => [y, ...x]
const shift = x => x.slice(1)
const sort = f => x => [...x].sort(f)
const delete = i => x => [...x.slice(0,i), ...x.slice(i+1)]
const splice = (s,c,...y) => x => [...x.slice(0,s), ...y, ...x.slice(s+c)]
const unique = arr => [...new Set(arr)]
const {0:first, length:l, [l - 1]:last} = [1,2,3,4,5]
@argyleink

This comment has been minimized.

Copy link
Owner Author

argyleink commented Aug 7, 2018

usage:

clone([1,2,3])
push(4, [1,2,3])
pop([1,2,3])
unshift(0, [1,2,3])
shift([1,2,3])
sort((a,b) => {...}, [1,2,3])
delete(1, [1,2,3])
splice(1, 2, [1,2,3])  
unique([1,1,2,3,4,5])  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.