Created
December 12, 2015 16:24
-
-
Save freddi301/331255f33f5637f969b8 to your computer and use it in GitHub Desktop.
JavaScript Functional Double-linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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