Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 8.10 KB | None | 0 0
  1. import language.{higherKinds, implicitConversions}
  2. import scala.collection.mutable
  3. import scala.annotation.tailrec
  4.  
  5. object Main extends App {
  6.  
  7.   trait Type[T]
  8.  
  9.   trait Val[T] extends Type[T]
  10.  
  11.   trait Ref[T] extends AnyRef with Type[T]
  12.  
  13.   implicit def anyIsType[T] = new Type[T] {}
  14.   implicit def refIsAnyRef[T <: AnyRef] = new Ref[T] {}
  15.  
  16.   trait ~>[Constraint[_], Coll[_]] {
  17.     def build[T: Constraint](iter: Iter[T]): Coll[T]
  18.   }
  19.  
  20.   trait Iter[A] {
  21.     def hasNext(): Boolean
  22.     def next(): A
  23.   }
  24.  
  25.   trait Iterable[I[_]] extends Type[I[_]] {
  26.     def iterator[A](col: I[A]): Iter[A]
  27.     def reversedIterator[A](col: I[A]): Iter[A]
  28.     def fold[A, B](col: I[A])(init: B)(f: (A, B) => B): B = {
  29.       var buffer = init
  30.       val it = iterator(col)
  31.       while (it.hasNext()) buffer = f(it.next(), buffer)
  32.       buffer
  33.     }
  34.   }
  35.  
  36.   trait |>[Constraint[_], M[_]] {
  37.  
  38.     def map[A, B: Constraint](m: M[A])(fn: A => B)
  39.                            (implicit E: Constraint ~> M): M[B]
  40.   }
  41.  
  42.   trait ->[Constraint[_], F[_]] extends |>[Constraint, F] {
  43.     def applicate[A, B: Constraint](fns: F[A => B])(fa: F[A])
  44.                (implicit E: Constraint ~> F): F[B]
  45.   }
  46.  
  47.   trait +>[Constraint[_], F[_]] extends ->[Constraint, F] {
  48.  
  49.     def flatMap[A, B: Constraint](fa: F[A])(fn: A => F[B])
  50.                                (implicit E: Constraint ~> F): F[B]
  51.  
  52.     def applicate[A, B: Constraint](fns: F[A => B])(fa: F[A])
  53.                (implicit E: Constraint ~> F): F[B] =
  54.       flatMap(fns)((f: A => B) => map(fa)(f))
  55.   }
  56.  
  57. // Error origin
  58.   final implicit class FlatmapOps[A, Constraint[_], F[_]](fa: F[A])
  59.                                  (implicit F: Constraint +> F) {
  60.  
  61.     def flatMap[B: Constraint](fn: A => F[B])
  62.                               (implicit E: Constraint ~> F): F[B] =
  63.       F.flatMap(fa)(fn)
  64.  
  65.     def +>[B: Constraint](fn: A => F[B])
  66.                          (implicit E: Constraint ~> F): F[B] =
  67.       F.flatMap(fa)(fn)
  68.   }
  69.  
  70.   trait Appendable[T[_]] extends Type[T[_]] {
  71.     def append[A](a: T[A], b: T[A]): T[A]
  72.   }
  73.  
  74.   trait Empty[E[_]] extends Type[E[_]] {
  75.     def empty[A]: E[A]
  76.   }
  77.  
  78.   trait Monoidal[M[_]] extends Appendable[M] with Empty[M] {
  79.     def flatten[A](m: M[M[A]]): M[A]
  80.   }
  81.  
  82.   trait Collection[Constraint[_], S[_]] extends Iterable[S]
  83.                                          with +>[Constraint, S]
  84.                                          with Monoidal[S] {
  85.  
  86.     def flatten[A](coll: S[S[A]]): S[A] = fold(coll)(empty[A])(append(_, _))
  87.   }
  88.  
  89.   trait Sequence[S[_]] extends Collection[Type, S]
  90.                                with ~>[Type, S] {
  91.  
  92.     def flatMap[A, B: Type](fa: S[A])(fn: (A) => S[B])
  93.                            (implicit E: Type ~> S): S[B] =
  94.       flatten(map(fa)(fn))
  95.   }
  96.  
  97.  
  98. sealed trait EList[T] { self =>
  99.  
  100.   def head(): Option[T] = None
  101.   def tail(): EList[T] = Nil()
  102.  
  103.   def insert(elem: T): EList[T] =
  104.     if(this == Nil[T]()) Cons(elem, Nil())
  105.     else {
  106.       def rec(ls: EList[T]): EList[T] =
  107.         ls match {
  108.           case Cons(hd, tl) => Cons(hd, rec(tl))
  109.           case Nil()        => Cons(elem, Nil())
  110.         }
  111.       rec(this)
  112.     }
  113.  
  114.   def + = insert _
  115.  
  116.   def iterator() = new Iter[T] {
  117.     private var ls: EList[T] = self
  118.     def hasNext() = ls != Nil()
  119.     def next() = {
  120.       val value = ls.asInstanceOf[Cons[T]].head().get
  121.       ls = ls.tail()
  122.       value
  123.     }
  124.   }
  125.  
  126.   def reversedIterator() = new Iter[T] {
  127.     private var ls: EList[T] = reverse()
  128.     def hasNext() = ls != Nil()
  129.     def next() = {
  130.       val value = ls.asInstanceOf[Cons[T]].head().get
  131.       ls = ls.tail()
  132.       value
  133.     }
  134.   }
  135.  
  136.   def reverse(): EList[T] = {
  137.     @tailrec def rec(buffer: EList[T], ls: EList[T]): EList[T] =
  138.       ls match {
  139.         case Cons(hd, tl) => rec(Cons(hd, buffer), tl)
  140.         case Nil()        => buffer
  141.       }
  142.     rec(Nil(), this)
  143.   }
  144.  
  145.   def filter(pred: T => Boolean): EList[T] =
  146.     if(this == Nil[T]()) Nil()
  147.     else {
  148.       def rec(ls: EList[T]): EList[T] =
  149.         ls match {
  150.           case Cons(hd, tl) =>
  151.             if(pred(hd)) Cons(hd, rec(tl)) else rec(tl)
  152.           case Nil()        => ls
  153.         }
  154.       rec(this)
  155.     }
  156.  
  157.   def size(): Int = {
  158.     @tailrec def rec(EList: EList[T], n: Int): Int =
  159.       EList match {
  160.         case Cons(hd, tl) => rec(tl, n + 1)
  161.         case Nil()        => n
  162.       }
  163.     rec(this, 0)
  164.   }
  165.  
  166.   def apply(n: Int): Option[T] =
  167.     if(n < 0) None
  168.     else {
  169.       @tailrec def rec(EList: EList[T], i: Int): Option[T] =
  170.         if(i > n) None
  171.         else EList match {
  172.           case Cons(hd, tl) => if(i == n) Some(hd) else rec(tl, i + 1)
  173.           case Nil()        => None
  174.         }
  175.       rec(this, 0)
  176.     }
  177.  
  178.   def isEmpty() = this == Nil()
  179.  
  180.   def append(EList2: EList[T]): EList[T] =
  181.    if(isEmpty()) EList2
  182.    else if(EList2.isEmpty()) this
  183.    else {
  184.      def rec(EList: EList[T]): EList[T] =
  185.        EList match {
  186.          case Cons(hd, tl) => Cons(hd, rec(tl))
  187.          case Nil()        => EList2
  188.        }
  189.      rec(this)
  190.    }
  191.  
  192.   def map[B](fn: T => B): EList[B] =
  193.     if(isEmpty()) Nil()
  194.     else {
  195.       var buffer: EList[B] = Nil()
  196.       var bufferTail: EList[B] = Nil()
  197.  
  198.       @tailrec def rec(EList: EList[T]): Unit =
  199.         EList match {
  200.           case Cons(hd, tl) => {
  201.             (buffer, bufferTail) match {
  202.               case (Cons(h0, t0), Cons(h1, Nil())) => {
  203.                 bufferTail.asInstanceOf[Cons[B]].tl = Cons(fn(hd), Nil())
  204.                 bufferTail = bufferTail.tail()
  205.               }
  206.               case (Cons(h, Nil()), Nil()) => {
  207.                 buffer.asInstanceOf[Cons[B]].tl = Cons(fn(hd), Nil())
  208.                 bufferTail = buffer.tail()
  209.               }
  210.               case (Nil(), Nil()) => buffer = Cons(fn(hd), Nil())
  211.               case _ => ()
  212.             }
  213.             rec(tl)
  214.           }
  215.           case Nil()        => ()
  216.         }
  217.       rec(this)
  218.       buffer
  219.     }
  220.  
  221.   def push(elem: T): EList[T] = Cons(elem, this)
  222.  
  223.   private def fromMutableHashSet(hs: mutable.HashSet[T]): EList[T] = {
  224.     val it = hs.iterator
  225.  
  226.     def buildEList(): EList[T] =
  227.       if(!it.hasNext) Nil()
  228.       else Cons(it.next, buildEList())
  229.  
  230.     buildEList()
  231.   }
  232.  
  233.   private def addToMutableHashSet(hs: mutable.HashSet[T], it: Iter[T]): Unit = {
  234.     @tailrec def rec(): Unit =
  235.       if(it.hasNext()) {
  236.         hs.add(it.next)
  237.         rec()
  238.       }
  239.     rec()
  240.   }
  241.  
  242. }
  243.  
  244. final case class Cons[T](hd: T, var tl: EList[T]) extends EList[T] {
  245.   override def head() = Some(hd)
  246.   override def tail() = tl
  247. }
  248.  
  249. final case class Nil[T]() extends EList[T]
  250.  
  251. object EList {
  252.   def apply[T](elems: T*): EList[T] = {
  253.     var (ls, i): (EList[T], Int) =(Nil(), elems.size - 1)
  254.  
  255.     while(i >= 0) {
  256.       ls = Cons(elems(i), ls)
  257.       i -= 1
  258.     }
  259.  
  260.     ls
  261.   }
  262.  
  263.   def fill[T](n: Int)(f: => T): EList[T] = {
  264.     def rec(i: Int): EList[T] =
  265.       if(i > n) Nil() else Cons(f, rec(i + 1))
  266.     rec(1)
  267.   }
  268.  
  269.   def build[I: Type](iter: Iter[I]): EList[I] = {
  270.     def rec(): EList[I] =
  271.       if(iter.hasNext()) Cons(iter.next(), rec()) else Nil()
  272.     rec()
  273.   }
  274.  
  275. }
  276.  
  277. implicit val EListIsQueueAndStack = new Sequence[EList] {
  278.  
  279.     def iterator[T](coll: EList[T]): Iter[T] = coll.iterator
  280.     def reversedIterator[T](coll: EList[T]): Iter[T] = coll.reversedIterator
  281.  
  282.     def append[T](a: EList[T], b: EList[T]): EList[T] = a.append(b)
  283.  
  284.     def empty[T]: EList[T] = EList()
  285.  
  286.     def build[I: Type](iter: Iter[I]): EList[I] = EList.build(iter)
  287.  
  288.     def index[T](coll: EList[T])(n: Int): Option[T] = coll(n)
  289.  
  290.     def tail[T](coll: EList[T]): EList[T] = coll.tail()
  291.     def head[T](coll: EList[T]): Option[T] = coll.head()
  292.  
  293.     def filter[T](coll: EList[T])(pred: (T) => Boolean): EList[T] =
  294.       coll.filter(pred)
  295.  
  296.     def insert[T](coll: EList[T], elem: T): EList[T] = coll.insert(elem)
  297.  
  298.     def size[T](coll: EList[T]): Int = coll.size()
  299.  
  300.     def fill[T](n: Int)(f: => T): EList[T] = EList.fill(n)(f)
  301.  
  302.     def map[T, B: Type](m: EList[T])(fn: (T) => B)
  303.                        (implicit E: ~>[Type, EList]): EList[B] =
  304.       m.map(fn)
  305.   }
  306.  
  307.   println(EList(1, 2, 3).flatMap(x => EList(x - 1, x + 1)))
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement