Skip to content

Instantly share code, notes, and snippets.

@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 / Combinations.java
Last active August 29, 2015 14:01
Find all the combinations of a given elements list using both for-loop and recursion.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* Add VM option -ea to enable assertion.
*
* @author Lim, Teck Hooi
*
@sshark
sshark / combinations.txt
Last active August 29, 2015 14:01
Combinations polygots
// Ruby
[-15,10,4,-3,5,-7,1,-5].reduce([[]]) {|ll, l| ll + ll.map{|a| a + [l]}}.select{|l| l.inject(:+) == 0}
// (Scala (functions)
List(-15,10,4,-3,5,-7,1,-5).foldLeft(List(List[Int]())){ (ll,l) => ll ++ ll.map{l :: _}}.filter(!_.isEmpty).filter(_.sum == 0)
// Scala (recursion)
@sshark
sshark / scratchpad.clj
Last active August 29, 2015 14:11
Solutions to some of the simple problems like BSF walking down a tree and TCO recursion.
(defn sumAll [x]
(if (= x 1)
1
(+ x (sumAll (dec x)))
))
(defn accSumAll [x]
((fn [y acc]
(if (zero? y)
acc
@sshark
sshark / pimp-my-libs.scala
Last active August 29, 2015 14:14
Examples of implicit usages
val sampleInts = List(1,2,3,4)
val sampleStrings = List("AA", "BB", "CC")
import annotation.implicitNotFound
@implicitNotFound("Member of type class ${T} not found.")
trait Joinable[T] {
def join(l: List[T]): T
}
implicit object JoinStrings extends Joinable[String] {
@sshark
sshark / words-warp.scala
Last active August 29, 2015 14:14
Java vs Scala code length and style comparison
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class RunMe {
public static void main(String[] args) {
List<String> sentences = new ArrayList<String>();
@sshark
sshark / FindZeroSumCombi.java
Created March 10, 2015 03:54
List combinations with the sum of zero
package com.test.sample;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class FindZeroSumCombi {
public static void main(String[] args) {
@sshark
sshark / temps-anyval.scala
Last active August 29, 2015 14:26
Using temperatures Celcius, Fahrenheit and Kelvin as an AnyVal example
/**
* To be a valid value class, the following rules must be followed (taken from Programming Scala 2nd Ed.):
* 1. The value class has one and only one public val argument (as of Scala 2.11,
* the argument can also be nonpublic).
* 2. The type of the argument must not be a value class itself.
* 3. If the value class is parameterized, the @specialized annotation can’t be used.
* 4. The value class doesn’t define secondary constructors.
* 5. The value class defines only methods, but no other vals and no vars.
* 6. However, the value class can’t override equals and hashCode.
* 7. The value class defines no nested traits, classes, or objects.
// 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
}