Skip to content

Instantly share code, notes, and snippets.

@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 / permutations_lazy.scala
Last active July 15, 2016 06:58
Show all permutations
// 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)
@sshark
sshark / gist:935fbc103c9bbb14b3bd
Last active June 26, 2016 15:26
A Writer Monad example
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
}
@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 / RunManyIO.scala
Last active October 13, 2019 15:45
How to control the number of Future / threads in a confined memory that only allow, say, 2 Future-s?
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._
@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.