Skip to content

Instantly share code, notes, and snippets.

@songz
Created June 2, 2018 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songz/685d14e3b777185d734f80a17283bbe9 to your computer and use it in GitHub Desktop.
Save songz/685d14e3b777185d734f80a17283bbe9 to your computer and use it in GitHub Desktop.
export const identity = (a) => a
export const add = (a,b) => a+b
export const sub = (a,b) => a-b
export const mul = (a,b) => a*b
export const identityf = (a) => (() => a)
export const addf = (a) => ((b) => a+b)
export const curry = (f,b) => ((a) => f(a,b))
export const curryr = (f,b) => ((a) => f(a,b))
export const liftf = (f) => ((a) => curry(f,a))
export const twice = (f) => ((a) => f(a,a))
export const reverse = (f) => ((a,b)=>f(b,a))
export const composeu = (f1,f2) => ((a) => f2(f1(a)))
export const composeb = (f1, f2) => ((a,b,c)=>f2(f1(a,b), c))
export const limit = (f1, a) => ((...b) => {
if(a <= 0) return;
a = a-1;
return f1(...b);
})
export const from = (a) => ( () => {
a = a+1;
return a-1;
})
export const to = (f1, v) => ( () => {
const result = f1();
if(result >= v) return;
return result;
})
export const thru = (f1, v) => {
return () => {
const result = f1();
if(result > v) return;
return result;
};
}
export const fromTo = (a,b) => {
return () => {
a += 1;
if(a > b) return;
return a-1;
}
}
export const element = (arr, fn=fromTo(0, arr.length)) => {
return () => {
const i = fn();
if(i || i=== 0) return arr[i];
}
}
export const collect = (f, arr) => {
return () => {
const v = f();
if(v || v === 0)
arr.push(v);
return v;
}
}
export const filter = (f1,f2) => {
return () => {
let v = f1();
while(v && !f2(v)) {
v = f1();
}
return v;
}
}
export const concat = (f1, f2) => {
return () => {
let v = f1();
if(v !== undefined) {
return v;
}
v = f2();
if(v !== undefined) {
return v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment