Skip to content

Instantly share code, notes, and snippets.

@jrsinclair
jrsinclair / process-rows-curried.js
Created December 5, 2018 22:06
Elegant Error Handling Code Samples
function processRows(headerFields) {
return function processRowsWithHeaderFields(dataRows) {
// Note this is Array map, not Either map.
return dataRows.map(row => processRow(headerFields, row));
};
}
@jrsinclair
jrsinclair / csv-to-messages-01.js
Created December 5, 2018 06:54
Elegant Error Handling Code Samples
function csvToMessages(csvData) {
const csvRows = splitCSVToRows(csvData);
const headerFields = csvRows.map(_.head).map(splitFields);
const dataRows = csvRows.map(_.tail);
// What’s next?
}
@jrsinclair
jrsinclair / process-csv-data-helpers.js
Created December 5, 2018 06:53
Elegant Error Handling Code Samples
function splitCSVToRows(csvData) {
// There should always be a header row... so if there's no
// newline character, something is wrong.
return (csvData.indexOf('\n') < 0)
? left('No header row found in CSV data')
: right(csvData.split('\n'));
}
function processRows(headerFields, dataRows) {
// Note this is Array map, not Either map.
@jrsinclair
jrsinclair / process-row-fluent.js
Created December 5, 2018 06:52
Elegant Error Handling Code Samples
function processRow(headerFields, row) {
const rowObjWithDate = right(row)
.map(splitFields)
.chain(zipRow(headerFields))
.chain(addDateStr);
return either(showError, rowToMessage, rowObjWithDate);
}
@jrsinclair
jrsinclair / process-row-06.js
Created December 5, 2018 06:51
Elegant Error Handling Code Samples
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
const rowObj = fieldsEither.chain(zipRow(headerFields));
const rowObjWithDate = rowObj.chain(addDateStr);
return either(showError, rowToMessage, rowObjWithDate);
}
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
const rowObj = fieldsEither.chain(zipRow(headerFields));
const rowObjWithDate = rowObj.chain(addDateStr);
// Slowly getting better... but what do we return?
}
@jrsinclair
jrsinclair / either.js
Created December 5, 2018 06:47
Elegant Error Handling Code Samples
function either(leftFunc, rightFunc, e) {
return (e instanceof Left) ? leftFunc(e._value) : rightFunc(e._value);
}
@jrsinclair
jrsinclair / left-with-chain.js
Created December 5, 2018 06:43
Elegant Error Handling Code Samples
/**
*Left represents the sad path.
*/
class Left {
constructor(val) {
this._value = val;
}
map() {
// Left is the sad path
function processRow(headerFields, row) {
const fieldsEither = right(row).map(splitFields);
const rowObj = fieldsEither.map(zipRow(headerFields)).join();
const rowObjWithDate = rowObj.map(addDateStr).join();
// Slowly getting better... but what do we return?
}
@jrsinclair
jrsinclair / left-with-join.js
Created December 5, 2018 06:38
Elegant Error Handling Code Samples
/**
*Left represents the sad path.
*/
class Left {
constructor(val) {
this._value = val;
}
map() {
// Left is the sad path