This file contains hidden or 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
| // following http://inoio.de/blog/2014/07/19/type-class-101-semigroup/ | |
| trait Semigroup[A] { | |
| def append(a: A, b: A): A | |
| } | |
| implicit val intSemigroup = new Semigroup[Int] { | |
| def append(a: Int, b: Int) = a + b | |
| } |
This file contains hidden or 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
| // following http://inoio.de/blog/2014/07/19/type-class-101-semigroup/ | |
| trait Semigroup[A] { | |
| def append(a: A, b: A): A | |
| } | |
| trait Monoid[A] extends Semigroup[A] { | |
| def zero: A | |
| } |
This file contains hidden or 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
| trait Tree[+A] | |
| case class Node[A](i: A, left: Tree[A], right: Tree[A]) extends Tree[A] | |
| case class Leaf[A](i: A) extends Tree[A] | |
| val tree = Node(4, Leaf(1), Node(7, Node(6, Leaf(3), Leaf(4)), Node(8, Leaf(1), Leaf(1)))) | |
| def mirror[A](node: Tree[A]): Tree[A] = { | |
| node match { | |
| case n: Node[A] => Node(n.i, mirror(n.right), mirror(n.left)) |
This file contains hidden or 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
| package acme.jelly; | |
| public class Apple extends Fruit {} | |
| package acme.jelly; | |
| public class Box<T extends Fruit> { | |
| private T item; | |
| public Box(T item) { |
This file contains hidden or 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
| // use Ordering for sorting | |
| def quicksort[A](l: Seq[A])(implicit ordering: Ordering[A]): Seq[A] = l match { | |
| case Nil => Nil | |
| case x :: xs => quicksort(xs.filter(ordering.compare(_, x) < 0)) ++ | |
| Seq(x) ++ | |
| quicksort(xs.filter(ordering.compare(_, x) >= 0)) | |
| } | |
| object DescOrdering extends Ordering[Int] { | |
| override def compare(x: Int, y: Int): Int = x - y match { |
This file contains hidden or 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
| val fizz: Stream[Option[String]] = List(None,None,Some("Fizz")).toStream #::: fizz | |
| val buzz: Stream[Option[String]] = List(None,None,None,None,Some("Buzz")).toStream #::: buzz | |
| // Stream or laziness is not supported by tupleN's zipped method. Therefore, | |
| // a fix range must be specified i.e. (1 to 30) | |
| ((1 to 30).toList, fizz, buzz).zipped.map { | |
| case (n, None, None) => n.toString | |
| case (_, Some(x), None) => x | |
| case (_, None, Some(x)) => x | |
| case (_, Some(x), Some(y)) => x + y |
This file contains hidden or 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
| implicit val f1 = (y: Int) => y.toString | |
| type L[X] = X => String | |
| def foo[T : L](x: T):String = x | |
| foo(121) | |
| def bar[E, T : ({type L[X] = X => E})#L](x: T):E = x | |
| //desugar => def goo2[E, T](x: T)(implicit ev: T => E):E = x | |
| bar[String, Int](1231) | |
| bar(1231)(x => s"** $x ** ") // remove [String, Int] because the types are explicitly provided |
This file contains hidden or 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
| case class WildDuck() { | |
| def swim = "It swims" | |
| def quack = "Quack quack" | |
| def fly = "If flies" | |
| } | |
| case class RubberDuck() { | |
| def quacky = "Quackkkkk...." | |
| } |
This file contains hidden or 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
| import scala.annotation.tailrec | |
| def bubbleSort(l: Seq[Int]): Seq[Int] = { | |
| def isSorted(s: Seq[Int]) = ((false, Int.MinValue) /: s){ | |
| case ((flag, prev), current) => | |
| if (prev <= current) (true, current) else (false, Int.MaxValue) | |
| }._1 | |
| def _bubbleSort(m: Seq[Int]): Seq[Int] = m match { | |
| case x +: y +: Nil => if (x > y) y +: x +: Nil else x +: y +: Nil | |
| case x +: y +: zs => | |
| if (x > y) y +: _bubbleSort(x +: zs) |
This file contains hidden or 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
| sealed trait LList[+A] { | |
| def ::[B >: A](b: B): LList[B] | |
| def ++[B >: A](b: LList[B]): LList[B] | |
| } | |
| case object End extends LList[Nothing] { | |
| def ::[B](b: B) = Cons(b, End) | |
| def ++[B](b: LList[B]) = b | |
| } |