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
def eq(first, *rest): | |
for r in rest: | |
assert type(first) is type(r), "Different types %s, %s for inputs (%s,%s)" % (type(first), type(r), first, r) | |
if first != r: | |
return False | |
return True | |
# eq(1) - True | |
# eq(1, 1) - True | |
# eq(1, 1, 1) - True |
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
lol = log --graph --decorate --pretty=oneline --abbrev-commit | |
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all | |
lolan = log --graph --decorate --pretty=oneline --abbrev-commit --all --show-notes |
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
head -c 32 /dev/random | base64 | |
# e.g. | |
# Xv1FnaWlNmMJxs2UYGawn24h4+ueyigZQJEN43hcF44= | |
# Can also trim the trailing '=' by using head again | |
# and use echo so we get a newline: | |
echo $(head -c 32 /dev/random | base64 | head -c 32) |
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.*; | |
public class Tails { | |
/** Not including the empty list */ | |
public static <A> List<List<A>> tails(List<A> xs) { | |
ArrayList<List<A>> tails = new ArrayList<List<A>>(xs.size()); | |
for (int i = 0; i < xs.size(); i++) { | |
ArrayList<A> tail = new ArrayList<A>(xs.size() - i); | |
tails.add(tail); |
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
Thread t = new Thread() { | |
public void run() { | |
// do something useful | |
} | |
}; | |
t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { | |
public void uncaughtException(Thread exThread, Throwable ex) { | |
// handle it here | |
} |
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 scala.collection.mutable.ListBuffer | |
// See also http://stackoverflow.com/questions/22633598/creating-an-iterator-of-subsequences-from-an-iterator-of-items/ | |
/** Lazily generate all subsequences of an Iterator or Iterable, up to the specified length, returning a new Iterator. */ | |
class Subseqs[A](n: Int)(i: Iterator[A]) extends Iterator[List[A]] { | |
val buffer = new ListBuffer[A]() | |
//buffer ++= i.take(n) // NO - inconsistent behaviour of take() with iterators | |
// see http://stackoverflow.com/questions/7619642/consume-items-from-a-scala-iterator | |
(1 to n).foreach(_ => buffer += i.next()) |
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
object Timer { | |
/** Time a block of code, print the duration in millis, and return the value of the block*/ | |
def time[A](f: => A): A = time2(f) match { | |
case (a, nanos) => | |
println("time: " + nanos / 1000000L + "ms") | |
a | |
} | |
/** Time a block of code, return a tuple of the value of the block and the duration in nanos */ |
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
implicit class Crossable[X](xs: Traversable[X]) { | |
def cross[Y](ys: Traversable[Y]) = for { x <- xs; y <- ys } yield (x, y) | |
} |
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 f = Map("a" -> 1, "b" -> 2) //> f : scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2) | |
val g = Map(1 -> 'c', 3 -> 'd') //> g : scala.collection.immutable.Map[Int,Char] = Map(1 -> c, 3 -> d) | |
//def pandThen[A, B, C](pf1: PartialFunction[A, B], pf2: PartialFunction[B, C]): PartialFunction[A, C] = { | |
// Function.unlift((a: A) => pf1.lift(a) flatMap pf2.lift) | |
//} | |
implicit class ComposePartial[B, C](pf: PartialFunction[B, C]) { | |
def andThenPartial[D](that: PartialFunction[C, D]): PartialFunction[B, D] = | |
Function.unlift(pf.lift(_) flatMap that.lift) |
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
-- Using pattern match - adapted from http://rosettacode.org/wiki/FizzBuzz#Scala | |
-- (Naturally, this can be formatted sensibly rather than as a gratuitous one-liner!) | |
fizzbuzz :: Integer -> String | |
fizzbuzz n=case map(mod n)[3,5]of[0,0]->"FizzBuzz";[0,_]->"Fizz";[_,0]->"Buzz";_->show n | |
-- The canonical version at http://rosettacode.org/wiki/FizzBuzz#Haskell using guards | |
-- can be packed into an even shorter one-liner: |