Skip to content

Instantly share code, notes, and snippets.

@AKST
Last active December 28, 2015 12:19
Show Gist options
  • Save AKST/7500114 to your computer and use it in GitHub Desktop.
Save AKST/7500114 to your computer and use it in GitHub Desktop.
package object example {
/**
* Tranverse a seqence of booleans (which it interprets as seqence of bits),
* and these booleans are transfromed into a binary value. Eg.
*
* scala> boolsToBin(List(true, true, false, true))
* resXX: Int = 11
*/
def boolsToBin(bs: Seq[Boolean]): Int = {
def iter(binSeq: Seq[Boolean], binIndex: Int, acc: Int): Int = binSeq match {
case Seq() => acc
case Seq(x, xs@_*) => iter(xs, binIndex+1, acc | (if (x) 1 << binIndex else 0))
}
iter(bs, 0, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment