Skip to content

Instantly share code, notes, and snippets.

View gcanti's full-sized avatar

Giulio Canti gcanti

View GitHub Profile
@gcanti
gcanti / fp-ts-technical-overview.md
Last active March 11, 2024 02:40
fp-ts technical overview

Technical overview

A basic Option type

// Option.ts

// definition
export class None {
  readonly tag: 'None' = 'None'
// 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 / 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
//
@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 / type_safe_event_emitter.md
Created November 5, 2016 08:38
Type safe event emitter with Flow
// A is a phantom type that ties an event instance...
class Event<A> {}
// ...to its handler
type Handler<A> = (a: A, ...rest: Array<void>) => void;

declare class EventEmitter {
  on<A, F: Handler<A>>(event: Event<A>, handler: F): void;
  emit<A>(event: Event<A>, a: A): void;
}
@gcanti
gcanti / beginnerProgram.js
Created July 3, 2016 10:13
Elm architecture with Flow + react + redux
// @flow
import ReactDOM from 'react-dom'
import React from 'react'
import type { Component, Element } from 'react'
import { createStore } from 'redux'
type ReduxInitAction = { type: '@@redux/INIT' };
type MsgShape = { type: any };
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 / type-safe-ops.ts
Created September 21, 2017 15:23
Type-safe get, set, remove, pick, insert with TypeScript
import { ObjectOmit } from 'typelevel-ts'
const get = <O, K extends keyof O>(k: K, o: O): O[K] => o[k]
const set = <O, K extends keyof O>(k: K, v: O[K], o: O): O => Object.assign({}, o, { [k as any]: v })
const remove = <O, K extends keyof O>(k: K, o: O): ObjectOmit<O, K> => {
const copy: any = Object.assign({}, o)
delete copy[k]
return copy
@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 / 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