You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deffilter(list: List[Int], predicate: Int=>Boolean):List[Int] = list match {
case x :: xs if predicate(x) => x :: filter(xs, predicate)
caseNil=>Nil
}
Multiple parameters:
defcombine(emptyValue: Int, list: List[Int], combiner: (Int, Int) =>Int):Int= list match {
case x :: y :: xs => combine(combiner(x, y) :: xs)
case x ::Nil=> x
caseNil=> emptyValue
}
Curried parameters:
defmap(list: List[Int], adder: Int=>Int=>Int, amount: Int):List[Int] = list.map(adder(amount))
map(List(1, 2, 3), a => i => i + a, 10) // = List(11, 12, 13)
ADTs
sealedtraitMessagecaseclassHello(name: String) extendsMessagecaseobjectGoodbyeextendsMessagedeflog(msg: Message):Unit= msg match {
caseHello(name) => println(s"Hello $name")
} // On compilation will warn of non-exhaustive match unless Goodbye is also matched
Type aliases
typeMyType=Stringdefping(msg: MyType) =???
To change the kind of a type:
typeErrorOr[A] =Either[Throwable, A]
// Can be used where * -> * required
More ideas:
Type lambdas
Self types
Type classes
Context bounds
Higher-kinded types
The text was updated successfully, but these errors were encountered:
Perhaps things like:
Passing lambdas as values
Anonymous parameter syntax:
Named parameter/expanded syntax, inferred type:
Named parameter, explicit type:
Receiving lambdas and using them
Single parameter:
Multiple parameters:
Curried parameters:
ADTs
Type aliases
To change the kind of a type:
More ideas:
The text was updated successfully, but these errors were encountered: