Skip to content

Instantly share code, notes, and snippets.

@jrsinclair
jrsinclair / process-row-03.js
Created December 5, 2018 01:40
Elegant Error Handling Code Samples
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
const rowObj = fieldsEither.map(zipRow(headerFields));
// ... But now we have another problem ...
}
@jrsinclair
jrsinclair / zip-row-curried.js
Created December 5, 2018 01:39
Elegant Error Handling Code Samples
function zipRow(headerFields) {
return function zipRowWithHeaderFields(fieldData) {
const lengthMatch = (headerFields.length == fieldData.length);
return (!lengthMatch)
? left(new Error("Row has an unexpected number of fields"))
: right(_.zipObject(headerFields, fieldData));
};
}
@jrsinclair
jrsinclair / process-row-02.js
Created December 5, 2018 01:38
Elegant Error Handling Code Samples
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
const rowObj = fieldsEither.map(zipRow /* wait. this isn't right */);
// ...
}
@jrsinclair
jrsinclair / process-row-01.js
Created December 5, 2018 01:37
Elegant Error Handling Code Samples
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
// …
}
@jrsinclair
jrsinclair / zip-row-add-date-str.js
Created December 5, 2018 01:36
Elegant Error Handling Code Samples
function zipRow(headerFields, fieldData) {
const lengthMatch = (headerFields.length == fieldData.length);
return (!lengthMatch)
? left(new Error("Row has an unexpected number of fields"))
: right(_.zipObject(headerFields, fieldData));
}
function addDateStr(messageObj) {
const errMsg = 'Unable to parse date stamp in message object';
@jrsinclair
jrsinclair / left-right.js
Created December 5, 2018 01:34
Elegant Error Handling Code Samples
function left(x) {
return Left.of(x);
}
function right(x) {
return Right.of(x);
}
@jrsinclair
jrsinclair / left-of.js
Created December 5, 2018 01:33
Elegant Error Handling Code Samples
Left.of = function of(x) {
return new Left(x);
};
@jrsinclair
jrsinclair / left-right-hello-2.js
Created December 5, 2018 00:37
Elegant Error Handling Code Samples
const leftHello = new Left('Hello world');
const rightHello = new Right('Hello world');
const worldToLogRocket = str => str.replace(/world/, 'LogRocket');
leftHello.map(worldToLogRocket).map(trace);
// Doesn't print any thing to the console
// ← Left(Hello world)
rightHello.map(worldToLogRocket).map(trace);
// ⦘ Hello LogRocket
@jrsinclair
jrsinclair / left.js
Created December 4, 2018 07:45
Elegant Error Handling Code Samples
/**
* Left represents the sad path.
*/
class Left {
constructor(val) {
this._value = val;
}
map() {
// Left is the sad path
@jrsinclair
jrsinclair / left-right-hello.js
Created December 4, 2018 07:42
Elegant Error Handling Code Samples
const leftHello = new Left('Hello world');
const rightHello = new Right('Hello world');
leftHello.runFunctionOnlyOnHappyPath(trace);
// does nothing
rightHello.runFunctionOnlyOnHappyPath(trace);
// ⦘ Hello world
// ← "Hello world"