Skip to content

Instantly share code, notes, and snippets.

View gcanti's full-sized avatar

Giulio Canti gcanti

View GitHub Profile
import { Option } from '@fp-ts/data/Option'
import * as O from '@fp-ts/data/Option'
import * as S from '@fp-ts/data/String'
import produce from 'immer'
import * as Optic from '@fp-ts/optic'
import * as StringOptic from '@fp-ts/optic/data/String'
import { pipe } from '@fp-ts/data/Function'
// let's say we want to uppercase the second item of the array
interface S {
@gcanti
gcanti / GADT.ts
Last active September 23, 2022 10:55
Approximating GADTs in TypeScript
// Adapted from http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/
import { Kind, URIS } from 'fp-ts/lib/HKT'
import { URI } from 'fp-ts/lib/Identity'
import { identity } from 'fp-ts/lib/function'
// ------------------------------------------
// Leibniz
// ------------------------------------------
@gcanti
gcanti / io-ts2form.ts
Created August 25, 2018 13:37
Automatically building a form from a `io-ts` type
import * as t from 'io-ts'
import * as React from 'react'
import { render } from 'react-dom'
type Field = t.StringType | t.NumberType | t.BooleanType
interface Form extends t.InterfaceType<{ [key: string]: Field }> {}
const toReactElement = (f: Form | Field): React.ReactElement<any> => {
// f is a tagged union
switch (f._tag) {
@gcanti
gcanti / fp-ts-to-the-max-II.ts
Last active January 16, 2024 12:58
TypeScript port of the second half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
import { log } from 'fp-ts/lib/Console'
import { Type, URIS } from 'fp-ts/lib/HKT'
import { none, Option, some } from 'fp-ts/lib/Option'
import { randomInt } from 'fp-ts/lib/Random'
import { fromIO, Task, task, URI as TaskURI } from 'fp-ts/lib/Task'
import { createInterface } from 'readline'
//
// helpers
//
@gcanti
gcanti / fp-ts-to-the-max-I.ts
Created August 8, 2018 14:11
TypeScript port of the first half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
import { log } from 'fp-ts/lib/Console'
import { none, Option, some } from 'fp-ts/lib/Option'
import { randomInt } from 'fp-ts/lib/Random'
import { fromIO, Task, task } from 'fp-ts/lib/Task'
import { createInterface } from 'readline'
//
// helpers
//
  • fp-ts ( Functional programming in TypeScript)
  • io-ts (IO decoders/encoders)
  • io-ts-types (A collection of runtime types and combinators for use with io-ts)
  • io-ts-codegen (Code generation for io-ts)
  • monocle-ts (Functional optics: a (partial) porting of scala monocle to TypeScript)
  • typelevel-ts (Type level programming in TypeScript)
  • fp-ts-routing (A type-safe bidirectional routing library for TypeScript)
  • money-ts (TypeScript library for type-safe and lossless encoding and manipulation of world currencies and precious metals)
  • hyper-ts (Type safe middleware architecture for HTTP servers)
// Adapted from http://lukajcb.github.io/blog/functional/2018/01/03/optimizing-tagless-final.html
import { Applicative, Applicative1 } from 'fp-ts/lib/Applicative'
import { Apply, Apply1, Apply2C, applySecond, liftA4 } from 'fp-ts/lib/Apply'
import * as array from 'fp-ts/lib/Array'
import * as const_ from 'fp-ts/lib/Const'
import { HKT, Type, Type2, URIS, URIS2 } from 'fp-ts/lib/HKT'
import { IO, io, URI as IOURI } from 'fp-ts/lib/IO'
import { Option, some } from 'fp-ts/lib/Option'
import { getProductSemigroup, Semigroup } from 'fp-ts/lib/Semigroup'
@gcanti
gcanti / mtl.js
Last active December 18, 2017 22:02
MTL-style example, Flow and fp-ts, npm install fp-ts
// @flow
import type { Monad } from 'fp-ts/lib/Monad'
import type { HKT } from 'fp-ts/lib/HKT'
import { liftA2 } from 'fp-ts/lib/Apply'
import { flatten } from 'fp-ts/lib/Chain'
// Adapted from https://tech.iheart.com/why-fp-its-the-composition-f585d17b01d3
interface MonadUser<M> extends Monad<M> {
validateUser<U, L>(token: string): HKT<M, U, L, string>;
@gcanti
gcanti / optionT.js
Last active May 16, 2021 09:20
Monad transformer example (OptionT), Flow and fp-ts, npm install fp-ts
// @flow
import type { Option } from 'fp-ts/lib/Option'
import type { Lazy } from 'fp-ts/lib/function'
import type { Monad } from 'fp-ts/lib/Monad'
import * as optionT from 'fp-ts/lib/OptionT'
import { array } from 'fp-ts/lib/Array'
// If M<A> is a monad then M<Option<A>> is a monad
@gcanti
gcanti / free.js
Last active December 1, 2020 08:04
Free Monad example, Flow and fp-ts, npm install fp-ts
// @flow
import type { Free } from 'fp-ts/lib/Free'
import { liftF } from 'fp-ts/lib/Free'
import { Identity, identity } from 'fp-ts/lib/Identity'
// Creating set of instructions (AST)
export class Write<A> {
+_tag: 'Write' = 'Write';