Skip to content

Instantly share code, notes, and snippets.

@jrsonline
jrsonline / constructibles.swift
Last active January 27, 2018 14:45
Higher Kinded Type
extension Array : Constructible {
typealias TypeParameter = Element
}
extension LinkedList : Constructible {
typealias TypeParameter = T
}
@jrsonline
jrsonline / unapplied_fn_example.swift
Created January 26, 2018 23:13
Unapplied function
// unapplied function
// think of an 'unapplied type constructor' like this
let incrementFunction = { a in a + 1 }
@jrsonline
jrsonline / construct.swift
Last active January 26, 2018 23:08
Construct type
protocol HKTTag { /* a type to represent a Higher Kinded Type tag */ }
/// `ConstructorTag` represents a type constructor.
/// `TypeParameter` represents an argument to the type constructor.
struct Construct<ConstructorTag : HKTTag, TypeParameter> {
let tag: ConstructorTag
}
// The below is like Array<Int>
let intArrayConstructor = Construct<ArrayTag, Int>
@jrsonline
jrsonline / functorTag.swift
Last active January 28, 2018 03:20
Functor In Swift
protocol FunctorTag : HKTTag {
typealias F = Self
static func fmap<A,B>(_ transform: (A) -> B ) -> (Construct<F,A>) -> Construct<F,B>
// fmap will take a "transform A's to B's" -> and an "F<A>" -> and return an "F<B>"
}
@jrsonline
jrsonline / not_my_functor_either.swift
Created January 26, 2018 23:00
Not My Functor either
protocol Functor : Sequence<Element> {
func fmap<S>(_ transform:(Element) -> S) -> Self<S>
}
@jrsonline
jrsonline / not_my_functor.swift
Created January 26, 2018 22:59
Not my functor
protocol Functor : Sequence {
func fmap<S>(_ transform:(Element) -> S) -> ???
}
@jrsonline
jrsonline / lmap_is_great.swift
Created January 26, 2018 22:58
why no lmap
/// lmap works fine for linked list. Why can't Sequence define a generic one?
func lmap<S>(_ transform:(T) -> S) -> LinkedList<S> {
let l = LinkedList<S>()
for a in self {
l.addToEnd(transform(a))
}
return l
}
@jrsonline
jrsonline / functor_sequence_demos.swift
Last active January 26, 2018 22:57
functor_sequences
print( linkedList^.fmap { $0 % 2 == 0 }.fmap { $0 ? "yes" : "no" }.toLinkedList )
print( regularArray^.fmap { $0 % 2 == 0 }.fmap { $0 ? "yes" : "no" }.toArray )
print( tree^.fmap { $0 % 2 == 0 }.fmap { $0 ? "yes" : "no" }.toTree )
var neurons = LinkedList.start(1).addToEnd(2).addToEnd(3)
var exciteNeurons = neurons.map{ $0 * 10 }.addToStart(5) ///// :-(