Skip to content

Instantly share code, notes, and snippets.

// 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
}
@sshark
sshark / map-options.sc
Last active August 29, 2015 14:27
Using monoids with Map and Option as value. Does it make sense to have Option as value insteadof the actual value because the Map can return Option. This code does not compile
// 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
}
@sshark
sshark / mirror.sc
Last active August 29, 2015 07:45
Nodes are mirrored and pretty print. It does not work beyond 3 levels. Fixing it...
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))
@sshark
sshark / All.java
Last active October 27, 2015 06:26
Containers with generics
package acme.jelly;
public class Apple extends Fruit {}
package acme.jelly;
public class Box<T extends Fruit> {
private T item;
public Box(T item) {
@sshark
sshark / quicksort-variations.scala
Last active November 21, 2015 08:59
Simple quicksort using various implementations to understand view bound, context bound and implicits
// 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 {
@sshark
sshark / fizzbuzz.scala
Last active May 12, 2016 10:41
Inspired by a video on Introduction to Haskell for Scala developers, https://skillsmatter.com/skillscasts/7017-intro-to-haskell-for-scala-devs
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
@sshark
sshark / cryptic-foobar.scala
Last active November 10, 2015 13:31
Exhibit how "type" can be used with "implicit"
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
@sshark
sshark / ducks.scala
Last active July 5, 2016 16:27
Retroactive polymorphism using ducks as example
case class WildDuck() {
def swim = "It swims"
def quack = "Quack quack"
def fly = "If flies"
}
case class RubberDuck() {
def quacky = "Quackkkkk...."
}
@sshark
sshark / bubblesort.scala
Last active January 2, 2016 18:43
Bubble sort functional. Stop if list is sorted without go through all possible combinations
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)
@sshark
sshark / cons.scala
Last active December 19, 2015 18:35
Oversimplified List and reverse functions implementations for learning purpose
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
}