Last active
December 20, 2017 21:36
-
-
Save will-wow/8f3b96c10c7fab0373eac2bcfb11a32d to your computer and use it in GitHub Desktop.
Easy pipeline debugging with curried console.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const averageTriangle = R.pipe( | |
R.map(multiplySides), | |
R.tap(log('mult')), | |
R.map(divideByTwo), | |
R.tap(log('div')), | |
R.mean | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const averageTriangle = R.pipe( | |
R.map(multiplySides), | |
R.tap(console.log), | |
R.map(divideByTwo), | |
R.tap(console.log), | |
R.mean | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expect(averageTriangle([[2, 4], [3, 3], [4, 8]])).toBeCloseTo(8.167, 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript | |
export const log = (...args) => (data) => { | |
console.log.apply(null, args.concat([data])); | |
return data; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Typescript | |
export const log = (...args: any[]) => <T>(data: T): T => { | |
console.log.apply(null, args.concat([data])); | |
return data; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
triangleObservable | |
.do(log('triangles')) | |
.map(averageTriangle) | |
.do(log('average')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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