Created
April 1, 2016 18:47
-
-
Save BBischof/0cd666a4086e0a7eb6da6a99fe5efab7 to your computer and use it in GitHub Desktop.
Computing the amount of beans for the hopper
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
def bean_counter(output_shots: Int) : Int = { | |
if (output_shots >= 0) { | |
return (output_shots/2)*20 + output_shots%2*16 | |
} else { | |
throw new IllegalArgumentException("Not a valid number of shots.") | |
} | |
} |
Considering who we work for, I think we already have many a bean counter we could repurpose.
Don't think for a second that the name's significance escaped me. ;-)
:)
Needs more Scala.
// enterprise scala
class PositiveInt(val int: Int) extends AnyVal {
def ifOdd[A](function: => A): Option[A] = if(odd_?) Some(function) else None
def odd_? = int % 2 == 1
override def toString = int.toString
}
object PositiveInt {
implicit def apply(int: Int): PositiveInt = {
int match {
case positive if positive >= 0 => new PositiveInt(positive)
case negative => throw new IllegalArgumentException(s"Integer $int is not positive")
}
}
implicit def unapply(positiveInt: PositiveInt): Int = positiveInt.int
}
def beanInputForOutput(outputShots: PositiveInt): PositiveInt = {
outputShots / 2 * 20 + outputShots.ifOdd(16).getOrElse(0)
}
The most hilarious part of that is that I tried to simplify the math prematurely and ended up completely skipping the integer division. I couldn't figure out for the life of me why when outputShots
was odd why I was off by 10. Then, duh.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not adhere to the coding standard. Please correct.