Last active
August 22, 2020 14:54
-
-
Save anthonyjoeseph/4eefa31ae329b00f5a6490e45d481d06 to your computer and use it in GitHub Desktop.
Fun(ctional) Programming in JS
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 O from 'fp-ts/lib/Option' | |
import * as A from 'fp-ts/lib/Array' | |
import { sequenceS } from 'fp-ts/lib/Apply' | |
import { pipe } from 'fp-ts/lib/pipeable' | |
interface Money { currency: Currency; amount: number } | |
type PricingInfo = { effectiveDate: Date; price: Money; ticker: string } | |
enum Currency { USD = "USD", EUR = "EUR", JPY = "JPY", CHF = "CHF", GBP = "GBP", CAD = "CAD", AUD = "AUD", } | |
const parseRowOption = (row: string[]): O.Option<PricingInfo> => sequenceS(O.option)({ | |
effectiveDate: pipe( | |
row, | |
A.lookup(0), | |
O.chain(parseDateOption), | |
), | |
price: pipe( | |
row, | |
A.lookup(1), | |
O.chain(parseMoneyOption), | |
), | |
ticker: pipe( | |
row, | |
A.lookup(2), | |
O.chain(parseTickerOption), | |
) | |
}) | |
// Assumes we have (implement them as an exercise): | |
// parseDateOption = (s: string) : O.Option<Date> | |
// parseMoneyOption = (s: string) : O.Option<Money> | |
// it's nitpicky but 'string' should be lowercase here | |
// vvvvvv | |
// parseTickerOption = (s: string) : O.Option<string> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original post: https://funprogramming.substack.com/p/functional-programming-in-the-javascript-41b