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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * | |
| * Add VM option -ea to enable assertion. | |
| * | |
| * @author Lim, Teck Hooi | |
| * |
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
| // 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) |
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
| (defn sumAll [x] | |
| (if (= x 1) | |
| 1 | |
| (+ x (sumAll (dec x))) | |
| )) | |
| (defn accSumAll [x] | |
| ((fn [y acc] | |
| (if (zero? y) | |
| acc |
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 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] { |
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 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>(); |
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 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) { |
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
| /** | |
| * 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. |
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 | |
| } |
OlderNewer