This file contains hidden or 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
//this wont work as type T is unware of the operation + | |
//value 0 is not a zero value for type T | |
def summ[T](xs:List[T]):T = xs.foldLeft(0)(_ + _) |
This file contains hidden or 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
//We will take a really simple example of summing a list of ints | |
def summ(xs:List[Int]):Int = xs.foldLeft(0)(_ + _) | |
//there is a reason why we have selected foldLeft operation | |
//May be later in the blog it will be clear. |
This file contains hidden or 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
trait Monoid[T] { | |
def associative(t1:T,t2:T): | |
def zero:T | |
} |
NewerOlder