Skip to content

Instantly share code, notes, and snippets.

@fernandomora
Created June 5, 2015 14:53
Show Gist options
  • Save fernandomora/e318dc388993160e9562 to your computer and use it in GitHub Desktop.
Save fernandomora/e318dc388993160e9562 to your computer and use it in GitHub Desktop.
Generic function to square all the elements of a Iterable returning the original collection
def square[Element, IT[_] <: Iterable[Element], That](it: IT[Element])
(implicit bf: CanBuildFrom[IT[Element], Element, That],
num: Numeric[Element]): That = {
val b = bf.apply(it)
b.sizeHint(it)
it.foreach(elem => b += num.times(elem,elem) )
b.result()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment