Last active
December 28, 2015 12:19
-
-
Save AKST/7500114 to your computer and use it in GitHub Desktop.
This file contains 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 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