Skip to content

Instantly share code, notes, and snippets.

@kitasuna
Created July 1, 2019 01:30
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 kitasuna/9730d21ad3114953f72ce055221a6491 to your computer and use it in GitHub Desktop.
Save kitasuna/9730d21ad3114953f72ce055221a6491 to your computer and use it in GitHub Desktop.
Trying out fp-ts ReaderT
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')
}
)
{
"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