Skip to content

Instantly share code, notes, and snippets.

View fan-tom's full-sized avatar
🤕
Fighting with/against Rust

Alexey Gerasimov fan-tom

🤕
Fighting with/against Rust
View GitHub Profile
@anandabits
anandabits / HKT.swift
Last active December 25, 2023 00:57
Emulating HKT in Swift
// This example shows how higher-kinded types can be emulated in Swift today.
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation.
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
/// `ConstructorTag` represents a type constructor.
/// `Argument` represents an argument to the type constructor.
struct Apply<ConstructorTag, Argument> {
/// An existential containing a value of `Constructor<Argument>`
/// Where `Constructor` is the type constructor represented by `ConstructorTag`
import { right, either, left } from 'fp-ts/lib/Either'
import { HKTAs, HKT2As, HKT2, HKTS, HKT, HKT2S } from 'fp-ts/lib/HKT'
import { Monad } from 'fp-ts/lib/Monad'
import { option, some } from 'fp-ts/lib/Option'
function Do<M extends HKT2S>(m: Monad<M>): <L, A>(generator: () => Iterator<HKT2<M, L, A>>) => HKT2As<M, L, A>
function Do<M extends HKTS>(m: Monad<M>): <A>(generator: () => Iterator<HKT<M, A>>) => HKTAs<M, A>
function Do<M extends HKTS>(m: Monad<M>): <A>(generator: () => Iterator<HKT<M, A>>) => HKT<M, A> {
return <A>(generator: () => Iterator<HKT<M, A>>): HKT<M, A> => {
const iterator = generator()
@NicolasT
NicolasT / core
Created November 3, 2011 16:34
Haskell fold over list of monadic actions
[1 of 1] Compiling IO ( io.hs, io.o )
8 Lets floated to top level; 0 Lets floated elsewhere; from 11 Lambda groups
8 Lets floated to top level; 0 Lets floated elsewhere; from 8 Lambda groups
Total ticks: 507
107 PreInlineUnconditionally
2 eta_B1