You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the second post dedicated to implementing native versions of Haskell
functions according to JavaScript ES6 standards. You can see the full source code with unit tests in this GitHub repo.
Thanks for all the contributions! I have set up an email address, [email protected], for feedback or post suggestions.
Infinite lists
Trying to replicate the behaviour of Haskell's infinite lists, we can use ES6 proxies. They allow us to hook to a getter function which will execute every time one of the proxy's properties are accessed.
A setback with proxies is that regular JavaScript array operations will not work. To (kind of) mitigate this, we will return Infinity for any property that is not an index. That way, when we check for the length property, we will get Infinity and we know that we are handling an infinite array.
iterate f x returns an infinite list of repeated applications of f to x.
repeat x is an infinite list, with x the value of every element.
cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.
/** * Returns an infinite list of repeated applications of f to x * @param f function to iterate with * @param x initial value **/exportfunctioniterate(f,x){returnProxy.create({get: (_,property)=>{if(isNaN(property))returnInfinity;varr=x;for(vari=0;i<property;i++)r=f(r);returnr;}});}/** * Returns an infinite list, with x the value of every element * @param x value **/exportfunctionrepeat(x){returnProxy.create({get: (_,property)=>isNaN(property) ?
Infinity : x});}/** * Ties a finite list into a circular one, or equivalently, * the infinite repetition of the original list * @param xs list to be cycled **/exportfunctioncycle(xs){returnProxy.create({get: (_,property)=>isNaN(property) ?
Infinity : xs[property%xs.length]});}
/** * Applied to a list xs, returns the prefix of xs of length n * @param n number of elements to take * @param xs list to take from **/exportfunctiontake(n,xs){if(n<=0)return[];if(n>=xs.length)returnxs;varr=[];for(vari=0;i<n;i++)r.push(xs[i]);returnr;}/** * Returns the suffix of xs after the first n elements * @param n number of elements to drop * @param xs list to drop from **/exportfunctiondrop(n,xs){if(n<=0)returnxs;if(n>=xs.length)return[];if(xs.length===Infinity)returnProxy.create({get: (_,property)=>isNaN(property) ?
Infinity : xs[property+n]});varr=[];for(vari=n;i<xs.length;i++)r.push(xs[i]);returnr;}/** * Returns a tuple where first element is xs prefix of length n and * second element is the remainder of the list * @param n index to split at * @param xs list to split **/exportfunctionsplitAt(n,xs){return[take(n,xs),drop(n,xs)];}/** * Applied to a predicate f and a list xs, returns the longest * prefix (possibly empty) of xs of elements that satisfy f * @param f predicate to satisfy * @param xs list to take from **/exportfunctiontakeWhile(f,xs){varr=[];for(vari=0;i<xs.length;i++){if(!f(xs[i]))returnr;r.push(xs[i]);}returnr;}/** * Returns the suffix remaining after takeWhile * @param f predicate to satisfy * @param xs list to drop from **/exportfunctiondropWhile(f,xs){returndrop(takeWhile(f,xs).length,xs);}
Originally posted
2015-11-17
.Read the first part here.
This is the second post dedicated to implementing native versions of Haskell
functions according to JavaScript ES6 standards. You can see the full source code with unit tests in this GitHub repo.
Thanks for all the contributions! I have set up an email address, [email protected], for feedback or post suggestions.
Infinite lists
Trying to replicate the behaviour of Haskell's infinite lists, we can use ES6 proxies. They allow us to hook to a getter function which will execute every time one of the proxy's properties are accessed.
A setback with proxies is that regular JavaScript array operations will not work. To (kind of) mitigate this, we will return
Infinity
for any property that is not an index. That way, when we check for thelength
property, we will getInfinity
and we know that we are handling an infinite array.Examples
Sublists
The following sublist functions have been constructed to work with the above implementation of infinite lists.
Examples
Happy hacking.
The text was updated successfully, but these errors were encountered: