Skip to content

Instantly share code, notes, and snippets.

@paulp
Last active December 30, 2015 15:18
Show Gist options
  • Save paulp/7847178 to your computer and use it in GitHub Desktop.
Save paulp/7847178 to your computer and use it in GitHub Desktop.
trait Collections {
type CC[+X] // the overarching container type (in scala: any covariant collection, e.g. List, Vector)
type Min[+X] // the minimum type constructor which can be reconstituted to CC[X] (in scala: GenTraversableOnce)
type Opt[+X] // the container type for optional results (in scala: Option)
type CCPair[+X] // some representation of a divided CC[A] (at simplest, (CC[A], CC[A]))
type ~>[-V1, +V2] // some means of composing operations (at simplest, Function1)
type Iso[A] = CC[A] ~> CC[A] // e.g. filter, take, drop, reverse, etc.
type Map[-A, +B] = CC[A] ~> CC[B] // e.g. map, collect
type FlatMap[-A, +B] = CC[A] ~> Min[B] // e.g. flatMap
type Grouped[A, DD[X]] = CC[A] ~> CC[DD[A]] // e.g. sliding
type Fold[-A, +R] = CC[A] ~> R // e.g. fold, but also subsumes all operations on CC[A]
type Flatten[A] = CC[Min[A]] ~> CC[A] // e.g. flatten
type Build[A] = Min[A] ~> CC[A] // for use in e.g. sliding, flatMap
type Pure[A] = A ~> CC[A] // we may not need
trait Relations[A] {
type MapTo[+B] = Map[A, B] // an alias incorporating the known A
type FoldTo[+R] = Fold[A, R] // another one
type This = CC[A] // the CC[A] under consideration
type Twosome = CCPair[A] // a (CC[A], CC[A]) representation
type Self = Iso[A] // a.k.a. CC[A] => CC[A], e.g. tail, filter, reverse
type Select = FoldTo[A] // a.k.a. CC[A] => A, e.g. head, reduce, max
type Find = FoldTo[Opt[A]] // a.k.a. CC[A] => Opt[A], e.g. find
type Split = FoldTo[Twosome] // a.k.a. CC[A] => (CC[A], CC[A]), e.g. partition, span
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment