Created
December 11, 2020 16:26
-
-
Save KMurphs/7ef1817e532c5484c2d13dfe63ebab1b to your computer and use it in GitHub Desktop.
Interesting Implementation of Stack and Queue using Functional Programming in Javascript
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
// https://stackoverflow.com/a/42242601/9034699 | |
// From original answer: Stack | |
const stackPush = (x, xs) => f => f(x,xs) | |
const stackPop = s => s((x,xs) => [x,xs]) | |
// From a looooong effort on my side. | |
// It is not efficient, or usable. But the goal was a proof of concept: To figure out a way to do this using similar method. | |
const countQ = Q => Q((prevEl, prevQ) => 1 + (prevQ ? countQ(prevQ) : 0)) | |
const enQ = (el, Q) => f => f(el, Q, Q ? countQ(Q): 0) | |
const first = (Q, count) => Q((lastEl, lastQ) => (count === 0 ? lastEl : first(lastQ, count - 1))) | |
const deQ = Q => Q ? Q((lastEl, lastQ, lastCount) => [first(Q, lastCount), lastCount ? f => f(lastEl, lastQ, lastCount - 1) : null]) : null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My understanding:
stackPush
andenQ
actually define the nature of a stack and a queue respectively.They are both functions that have closure over the arguments that were passed to
stackPush
andenQ
In other words,
This means that s (the stack) is a function that remembers (aka has closure over) the arguments provided during its creation.
Now:
The
s3
that is returned is exactly the same object ass
and still remembers data from its original closure.Therefore:
will produce
el2
equal to1
, ands4
equal toemptyStack
In summary, this is just a very clever use of closures to implement a stack. Maybe not practical but certainly interesting...