Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created December 12, 2015 16:24
Show Gist options
  • Save freddi301/331255f33f5637f969b8 to your computer and use it in GitHub Desktop.
Save freddi301/331255f33f5637f969b8 to your computer and use it in GitHub Desktop.
JavaScript Functional Double-linked List
function prev(prev){return (self)=>(next)=>prev};
function self(prev){return (self)=>(next)=>self};
function next(prev){return (self)=>(next)=>next};
function node(self){return (next)=>(prev)=>(select)=>select(prev)(self)(next?next(node(self)(next)(prev)):next)};
function array2doublelist(array, index){
return index<array.length?
node(array[index])(array2doublelist(array,index+1))
:null;
}
list = node(1)(node(2)(node(3)(null)))(null);
list2 = array2doublelist([1,2,3,4,5],0)(null);
console.log(list(self), list2(self), list(next)(next)(self), list2(next)(next)(next)(prev)(prev)(self));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment