Skip to content

Instantly share code, notes, and snippets.

def accum[X,Y](seed: X, seq: Seq[Y])(fun: (X,Y) => X) =
seq.foldLeft(List[List[X]]()) {
case (Nil, y) => (fun(seed, y) :: Nil) :: Nil
case (buf, y) => (fun(buf.head.head, y) :: buf.head ) :: buf
}
(accum(0, (0 to 10)) { _ + _ }) foreach println
@hugobenichi
hugobenichi / $ javap -c SeqMatch$.class
Created July 31, 2013 02:52
checking if Seq() will match any empty sequence-like object or not
Compiled from "SeqMatch.scala"
public final class SeqMatch$ {
public static final SeqMatch$ MODULE$;
public static {};
Code:
0: new #2 // class SeqMatch$
3: invokespecial #12 // Method "<init>":()V
6: return
@hugobenichi
hugobenichi / imperative
Created June 4, 2013 12:53
reorder: imperative vs functional
type l_str = List[String]
type extract = (String, l_str)
def reorder(list: l_str, before: Int, after: Int): l_str = {
val (value, temp_list) = delete_at(before, list)
insert_at(value, after, temp_list)
}
def delete_at(at: Int, down: l_str, top: l_str = Nil): extract =
if (at <= 0 || down.tail.isEmpty) (down.head, rewind(top, down.tail))
/*
* Copyright 2011 Midokura KK
*/
package org.midonet.midolman.rules;
import java.util.Set;
import java.util.UUID;
import org.midonet.packets.IPAddr;
#!/bin/bash
ip=$1
(for i in {1..10}
do
iperf -f m -c $ip | egrep -o "[0-9]* Mbits/sec"
done) | awk '
{ SUM+=$1; VAR += $1 * $1; print $1, "Mbits/sec" }
END { MEAN = SUM/NR; print MEAN, sqrt(VAR/NR - MEAN*MEAN) }