Skip to content

Instantly share code, notes, and snippets.

@anthonyjoeseph
Last active August 22, 2020 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyjoeseph/4eefa31ae329b00f5a6490e45d481d06 to your computer and use it in GitHub Desktop.
Save anthonyjoeseph/4eefa31ae329b00f5a6490e45d481d06 to your computer and use it in GitHub Desktop.
Fun(ctional) Programming in JS
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>
@anthonyjoeseph
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment