Created
June 5, 2015 14:53
-
-
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
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 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