This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var neurons = LinkedList.start(1).addToEnd(2).addToEnd(3) | |
var exciteNeurons = neurons.map{ $0 * 10 }.addToStart(5) ///// :-( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Functor : Sequence { | |
func fmap<S>(_ transform:(Element) -> S) -> ??? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Functor : Sequence<Element> { | |
func fmap<S>(_ transform:(Element) -> S) -> Self<S> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// unapplied function | |
// think of an 'unapplied type constructor' like this | |
let incrementFunction = { a in a + 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array : Constructible { | |
typealias TypeParameter = Element | |
} | |
extension LinkedList : Constructible { | |
typealias TypeParameter = T | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let aas = [1,2,3]^ // "lifts" a to Construct<ArrayTag,Int> | |
/// the below are all equivalent ways to lower, pick the one that looks best to you | |
let array1 = aas.toArray | |
let array2 = Array.lower(aas) | |
let array3 = aas >>>¬ toArray // "continuation-passing" style. ¬ is ALT-L. >>>¬ is "lower" | |
let array4 = aas.lower | |
let array5 = aas¬ | |
let bbs = LinkedList(array: ["a","b","c"])^ // lifts to Construct<LinkedListTag,String> | |
/// the below are all equivalent ways to lower, pick the one that looks best to you |
OlderNewer