-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-functions.js
More file actions
36 lines (35 loc) · 1.64 KB
/
Copy pathcommon-functions.js
File metadata and controls
36 lines (35 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const fs = require('fs');
//borrowed from : https://medium.com/dailyjs/functional-js-with-es6-recursive-patterns-b7d0813ef9e3
const head = ([x]) => x;
const tail = ([x,...xs]) => xs;
const definition = x => typeof x !== 'undefined';
const mapper = ([x, ...xs], fn) => !x ? [] : [fn(x), ...mapper(xs, fn)];
const reverseArray = ([x, ...xs]) => definition(x) ? [...reverseArray(xs), x] : [];
const firstN = ([x, ...xs], n) => definition(x) && n ? [x, ...firstN(xs, n-1)] : [];
const lastN = (xs, n) => reverseArray(firstN(reverseArray(xs), n));
const count = ([x, ...xs], i = 0) => definition(x) ? count(xs, i+1) : i;
const filter = ([x, ...xs], pred) => definition(x) ? (pred(x) ? [x, ...filter(xs, pred)] : filter(xs, pred)) : [];
const reject = ([x, ...xs], pred) =>definition(x) ? (pred(x)? reject(xs, pred) : [x, ...reject(xs, pred)]) :[];
const reduce = ([x, ...xs], acc, memo) => definition(x) ? reduce(xs, acc, acc(x, memo)) : memo;
const partial = (fn, ...args) => (...newArgs) => fn(...args, ...newArgs);
const pluck = (key, object) => object[key];
const any = ([x, ...xs], pred) => definition(x) ? pred(x) ? true : any(xs,pred) : false;
exports.head = head;
exports.tail = tail;
exports.def = definition;
exports.undef = x => !definition(x);
exports.copy = array => [...array];
exports.map = mapper;
exports.first = firstN;
exports.last = lastN;
exports.reverse = reverseArray;
exports.length = count;
exports.filter = filter;
exports.reject = reject;
exports.reduce = reduce;
exports.partial = partial;
exports.pluck = pluck;
exports.any = any;
exports.writeToFile = (name, content, callback) => fs.writeFile(name, content, callback);
//TODO: flow and compose