about summary refs log blame commit diff stats
path: root/js/b.js
blob: 7e222010976798435d05557cdf8f6d3010527d56 (plain) (tree)





















                                           





                                                             



                                                                                                           













                                                                                       
























                                                                                               
                                        
/*
//
//  # 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);