Last active
June 21, 2018 20:46
-
-
Save mythril/8a01b5b44c182c452205c1d5e37d4cb6 to your computer and use it in GitHub Desktop.
pure_functional_javascript_queue.js
This file contains 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 pair(a, b) { | |
return function (selector) { | |
if (selector) { | |
return a; | |
} | |
return b; | |
} | |
} | |
function first(p) { | |
if (p) { | |
return p(true); | |
} | |
} | |
function remaining(p) { | |
if (p) { | |
return p(false); | |
} | |
} | |
function enqueue(element, p) { | |
return pair(element, p); | |
} | |
function get(nth, q) { | |
if (nth === 0) { | |
return first(q); | |
} | |
return get(nth - 1, remaining(q)); | |
} | |
console.log(get(2, enqueue(3, enqueue(2, enqueue(1))))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment