HigherKindSwift

An experimental Higher Kinded Types in Swift.

MIT License

Stars
44

HigherKindSwift

An experimental Higher Kinded Types in Swift, originally from the paper: Lightweight Higher-Kinded Polymorphism.

Slide: Swift - Speaker Deck (in Japanese )

Examples

Monad

let arr = [1, 2, 3].kind
                 .bind { [$0 * 3, $0 * 5 ].kind }.value

arr == [3, 5, 6, 10, 9, 15]


let list = List<Int>.cons(1, .cons(2, .cons(3, .nil))).kind
               .bind { List.cons($0 * 3, .cons($0 * 5, .nil)).kind }
               .value

list == List.cons(3, .cons(5, .cons(6,
                  .cons(10, .cons(9, .cons(15, .nil))))))

Natural Transformation

let list = [1, 2, 3].kind
               .naturalTransform {
                    List($0.value).kind
                }
               .value

list == List.cons(1, .cons(2, .cons(3, .nil)))

Warning

There is a Swift 4.2 compiler bug (?) when swift build -c debug, so please always build with swift build -c release.

References

License

MIT

Related Projects