just checking in

This commit is contained in:
Mark Canlas
2018-05-05 14:23:21 -04:00
parent fcdeb9ffe2
commit e250dfb67e
8 changed files with 376 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
package com.htmlism
trait BitExtractor[A] {
self =>
def length: Int
def unapply(n: Int): Option[A]
def >>[B](that: BitExtractor[B]): BitExtractor[(A, B)] =
new BitExtractor[(A, B)] {
def length: Int = self.length + that.length
def unapply(n: Int): Option[(A, B)] =
for {
b <- that.unapply(n)
shifted = n >> that.length
a <- self.unapply(shifted)
} yield (a, b)
}
}
object AtomExtractor {
def pow(ex: Int, acc: Int = 1): Int =
if (ex == 0)
acc
else
pow(ex - 1, acc * 2)
}
abstract class PrimitiveBitExtractor(val length: Int) extends BitExtractor[Int] {
private lazy val mask = AtomExtractor.pow(length) - 1
def unapply(n: Int): Option[Int] = Some(n & mask)
}
object OneBit extends PrimitiveBitExtractor(1)
object TwoBits extends PrimitiveBitExtractor(2)
object ThreeBits extends PrimitiveBitExtractor(3)