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
function processRow(headerFields, row) { | |
const fieldsEither = right(row).map(splitFields); | |
const rowObj = fieldsEither.map(zipRow(headerFields)); | |
// ... But now we have another problem ... | |
} |
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
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)); | |
}; | |
} |
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
function processRow(headerFields, row) { | |
const fieldsEither = right(row).map(splitFields); | |
const rowObj = fieldsEither.map(zipRow /* wait. this isn't right */); | |
// ... | |
} |
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
function processRow(headerFields, row) { | |
const fieldsEither = right(row).map(splitFields); | |
// … | |
} |
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
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'; |
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
function left(x) { | |
return Left.of(x); | |
} | |
function right(x) { | |
return Right.of(x); | |
} |
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
Left.of = function of(x) { | |
return new Left(x); | |
}; |
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 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 |
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
/** | |
* Left represents the sad path. | |
*/ | |
class Left { | |
constructor(val) { | |
this._value = val; | |
} | |
map() { | |
// Left is the sad path |
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 leftHello = new Left('Hello world'); | |
const rightHello = new Right('Hello world'); | |
leftHello.runFunctionOnlyOnHappyPath(trace); | |
// does nothing | |
rightHello.runFunctionOnlyOnHappyPath(trace); | |
// ⦘ Hello world | |
// ← "Hello world" |