Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Easy pipeline debugging with curried console.log
import * as R from 'ramda';
const multiplySides = R.reduce(R.multiply, 1);
const divideByTwo = R.divide(2);
const averageTriangle = R.pipe(
R.map(multiplySides),
R.map(divideByTwo),
R.mean
);
const averageTriangle = R.pipe(
R.map(multiplySides),
R.tap(data => console.log('mult', data)),
R.map(divideByTwo),
R.tap(data => console.log('div', data)),
R.mean
);
const averageTriangle = R.pipe(
R.map(multiplySides),
R.tap(log('mult')),
R.map(divideByTwo),
R.tap(log('div')),
R.mean
);
const averageTriangle = R.pipe(
R.map(multiplySides),
R.tap(console.log),
R.map(divideByTwo),
R.tap(console.log),
R.mean
);
expect(averageTriangle([[2, 4], [3, 3], [4, 8]])).toBeCloseTo(8.167, 3);
const multiplySides = R.reduce(R.multiply, 1);
const divideByTwo = R.divide(2);
const averageTriangle = R.pipe(
R.map(multiplySides),
log('mult'),
R.map(divideByTwo),
log('div'),
R.mean
);
// Javascript
export const log = (...args) => (data) => {
console.log.apply(null, args.concat([data]));
return data;
};
// Typescript
export const log = (...args: any[]) => <T>(data: T): T => {
console.log.apply(null, args.concat([data]));
return data;
};
// RxJS
clickObservable
.filter(isGood)
.map(toThing)
.flatMap(saveThing(apiService))
.delay(100)
.subscribe(setThing)
// Ramda
R.pipe(
R.filter(isGood),
R.map(toThing),
R.groupBy(thingType(types))
)(data)
// lodash/fp
_.flow(
_.filter(isGood),
_.map(toThing),
_.groupBy(thingType(types))
)(data)
triangleObservable
.do(log('triangles'))
.map(averageTriangle)
.do(log('average'))
LOG: [8, 9, 32]
LOG: [0.25, 0.2222222222222222, 0.0625]
Chrome 62.0.3202 (Mac OS X 10.12.6) averageTriangle calculates the average area FAILED
LOG: 'mult', [8, 9, 32]
LOG: 'div', [4, 4.5, 16]
Chrome 62.0.3202 (Mac OS X 10.12.6): Executed 4 of 4 SUCCESS (0.124 secs / 0.115 secs)
LOG: 'mul', [8, 9, 32]
LOG: 'div', [0.25, 0.2222222222222222, 0.0625]
Chrome 62.0.3202 (Mac OS X 10.12.6) averageTriangle calculates the average area FAILED
Expected 0.17824074074074073 to be close to 8.167, 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment