Skip to content

Instantly share code, notes, and snippets.

@argyleink
Last active September 12, 2022 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argyleink/dd2332b52f698c4c8647d27bba415d1b to your computer and use it in GitHub Desktop.
Save argyleink/dd2332b52f698c4c8647d27bba415d1b to your computer and use it in GitHub Desktop.
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
Copy link
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