Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Last active December 10, 2016 02:02
Show Gist options
  • Save mgtitimoli/f8dfaedfb3207e845b86dfcb02434a1e to your computer and use it in GitHub Desktop.
Save mgtitimoli/f8dfaedfb3207e845b86dfcb02434a1e to your computer and use it in GitHub Desktop.
import compose from "lodash/fp/compose";
import curry from "lodash/fp/curry";
const mappableWith = curry((compose, apply) => Object.assign(
map => mappableWith(compose, compose(map, apply)),
{valueOf: () => apply()}
));
const mappable = mappableWith(compose);
export {
mappable as default,
mappableWith
};
import mappable from './mappable';
const logAndReturn = (msg, value) => (console.log(msg, value), value);
const m1 = mappable(() => logAndReturn('m1', 10)); // no evaluation here
const m2 = m1(n => logAndReturn('m2', n / 2)); // no evaluation here
const m3 = m2(n => logAndReturn('m3', n * 5)); // no evaluation here
console.log('m1.valueOf()', m1.valueOf()); // we evaluate here (.valueOf)
// m1 10
// m1.valueOf() 10
console.log('m2.valueOf()', m2.valueOf()); // we evaluate here (.valueOf)
// m1 10
// m2 5
// m2.valueOf() 5
console.log('m3.valueOf()', m3.valueOf()); // we evaluate here (.valueOf)
// m1 10
// m2 5
// m3 25
// m2.valueOf() 25
// it can be used as a primitive due the implementation of valueOf
console.log(m1 + m2);
// m1 10
// m1 10
// m2 5
// 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment