Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created May 19, 2012 13:46
Show Gist options
  • Save nicerobot/2730897 to your computer and use it in GitHub Desktop.
Save nicerobot/2730897 to your computer and use it in GitHub Desktop.
Answers to Scala for the Impatient
def signum[N](i:N)(implicit n:Numeric[N]):Int = n.compare(i,n.fromInt(0))
def signum[N](i:N)(implicit n:Numeric[N]):Int = n.signum(i)
def product(s: String) = { var p = BigInt(1); for (c <- s) p*=c; p }
def product(s: String) = s map (i => BigInt(i)) product
def product(s: String):BigInt = if (s.length == 0) 1 else product(s.tail)*BigInt(s(0))
def randomArray(n:Int) = 1 to (scala.util.Random.self.nextDouble * n).toInt toArray
def randomArray(n:Int) = 1 to (scala.util.Random.self.nextInt.abs % n + 1) toArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment