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
| // returns a lazy sequence | |
| def fillAndArrange(xs: Seq[Int], ys: Seq[Int] = 1 to 9): Stream[Seq[Int]] = ys.diff(xs) match { | |
| case Seq() => Stream(xs) | |
| case zs => zs.flatten(z => fillAndArrange(xs.updated(xs.indexOf(0),z),zs)).toStream | |
| } | |
| assert(fillAndArrange(List(0, 0, 1, 5, 0, 0, 8, 0, 0)).size == 720) |
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 Monoid[A] { | |
| def append(a1: A, a2: A): A | |
| def empty: A | |
| } | |
| object Monoid { | |
| implicit def ListMonoid[A]: Monoid[List[A]] = new Monoid[List[A]] { | |
| def append(a1: List[A], a2: List[A]) = a1 ::: a2 | |
| def empty = Nil | |
| } |
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 org.teckhooi | |
| import java.util.concurrent.{Executors, ThreadFactory} | |
| import java.util.concurrent.atomic.AtomicInteger | |
| import cats.effect.{ExitCode, IO, IOApp} | |
| import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} | |
| import cats.effect._ | |
| import cats.implicits._ |
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. |