about summary refs log tree commit diff stats
path: root/js/b.js
blob: 7e222010976798435d05557cdf8f6d3010527d56 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
//
//  # The Witch in the Glass
//  
//  My mother says I must not pass
//  Too near that glass;
//  She is afraid that I will see
//  A little witch that looks like me,
//  With a red, red mouth to whisper low
//  The very thing I should not know!
//  
//  Alack for all your mother's care!
//  A bird of the air,
//  A wistful wind, or (I suppose
//  Sent by some hapless boy) a rose,
//  With breath too sweet, will whisper low
//  The very thing you should not know!
//  
//  - Sarah Morgan Bryan Piatt
//
*/

// b is for useful stuff 

'use strict'

const b = {
	pipe: (...args) => args.reduce((acc, el) => el(acc)),
	  compose: (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0],
    identity: x => x,
    curry: (fn) => {
	  const curried = (...args) => {
	      if (args.length >= fn.length)
	          return fn(...args)
	      else
	          return (...rest) => curried(...args, ...rest)
	  }
	  return curried
	},
	match: () => b.curry((what, s) => s.match(what)),
	replace: () => b.curry((what, replacement, s) => s.replace(what, replacement)),
	filter: () => b.curry((f, xs) => xs.filter(f)),
	map: () => b.curry((f, xs) => xs.map(f))
};


// // pipe
// const title = '10 Weird Facts About Dogs';
// const toLowerCase = (str) => str.toLowerCase();
// const addHyphens = (str) => str.replace(/\s/g, '-');
// const reverseText = (str) => (str === '') ? '' : reverseText(str.substr(1)) + str.charAt(0);
// const slug = b.pipe(title, toLowerCase, addHyphens, reverseText);
// console.log('pipe ', slug);


// // compose 
// const toUpperCase = x => x.toUpperCase();
// const exclaim = x => `${x}!`;
// const shout = b.compose(exclaim, toUpperCase);
// const ret = shout('send in the clowns'); // "SEND IN THE CLOWNS!"
// console.log('compose ', ret);


// // curry family
// const hasLetterR = b.match(/r/g);
// const r1 = hasLetterR('hello world');
// const r2 = hasLetterR('just j and s and t etc');
// const r3 = b.filter(hasLetterR, ['rock and roll', 'smooth jazz']);
// const noVowels = b.replace(/[aeiou]/ig);
// const censored = noVowels('*');
// const r4 = censored('Chocolate Rain');
// console.log('curry ', r1, r2, r3, r4);