Skip to content

Instantly share code, notes, and snippets.

@BBischof
Created April 1, 2016 18:47
Show Gist options
  • Save BBischof/0cd666a4086e0a7eb6da6a99fe5efab7 to your computer and use it in GitHub Desktop.
Save BBischof/0cd666a4086e0a7eb6da6a99fe5efab7 to your computer and use it in GitHub Desktop.
Computing the amount of beans for the hopper
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.")
}
}
@cmarkle
Copy link

cmarkle commented Apr 1, 2016

Does not adhere to the coding standard. Please correct.

@john-heaton
Copy link

Considering who we work for, I think we already have many a bean counter we could repurpose.

@BBischof
Copy link
Author

BBischof commented Apr 1, 2016

Don't think for a second that the name's significance escaped me. ;-)

@john-heaton
Copy link

:)

@colindean
Copy link

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)
  }

@colindean
Copy link

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