Created
July 1, 2019 01:30
-
-
Save kitasuna/9730d21ad3114953f72ce055221a6491 to your computer and use it in GitHub Desktop.
Trying out fp-ts ReaderT
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 readerT from "fp-ts/lib/ReaderT" | |
import { future } from "fp-ts-fluture/lib/Future" | |
import { of, reject, FutureInstance as Future } from "fluture" | |
import { identity } from "fp-ts/lib/function"; | |
type Env = { | |
locale: string | |
} | |
const env$: Env = { | |
locale: "en" | |
} | |
type Product = { | |
productId: number, | |
price?: number, | |
names: { | |
en: string, | |
fr: string | |
} | |
} | |
type LocalizedProduct = { | |
productId: number, | |
price?: number, | |
name: string | |
} | |
const getProduct: | |
(id: number) => | |
Future<never, Product> = | |
(productId: number) => of({ productId, names: { en: "English shirt", fr: "French shirt" } }) | |
const getProductPrices: | |
(product: Product) => | |
Future<never, Product> = | |
(product: Product) => of({ ...product, price: 12.00 }) | |
const chooseLocale: | |
(locale: string) => | |
(product: Product) => | |
LocalizedProduct = | |
(locale: string) => (product: Product) => ({ productId: product.productId, price: product.price, name: product.names[locale] }) | |
const getLocalizedProduct: | |
(productId: number) => | |
(env: Env) => | |
Future<never, LocalizedProduct> = | |
(productId: number) => (env: Env) => getProduct(productId).chain(getProductPrices).map(chooseLocale(env.locale)) | |
const productId = 1234 | |
const readerFluture = readerT.getReaderT2v(future) | |
const envR = readerFluture.map(getLocalizedProduct(productId), identity) | |
const f = envR(env$) | |
f.fork( | |
err => console.dir(err), | |
res => { | |
console.dir(res) | |
console.dir('^^^ res') | |
} | |
) |
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
{ | |
"name": "readert-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"fluture": "^11.0.2", | |
"fp-ts": "^1.19.3", | |
"fp-ts-fluture": "^0.4.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment