https://github.com/scala/scala/graphs/contributors?from=2014-11-01&to=2016-10-29&type=c
val x : T = ???
def m (a: T): U = ???
type T >: L <: U
class C (a: T) extends A { ... }
(Methods and Types)
(Primitive types are types too)
: Unit
The language is not opinionated, but we should be.
f(x)
= f.apply(x)
(1, 2)
= Tuple2(1, 2)
, val (x, y) = (1, 2)
object
member<:
rulesdef map[R](mapper: T => R): Stream[R]
<R> Stream<R> map(Function<? super T, ? extends R> mapper)
(But there's a JEP for that.)
type inference to refactor with ease
composed into different solutions
not obscure it
OO's forte
mutability exposes order of execution
synchrony exposes distance/failure
-Dscala.color
Either
is now right-biasedwithFilter
for for
trait
compiles to Java 8 interface
FunctionN
compiles to SAM type (Java 8 λ)trait extends class
super
call ⟹ accessorclass C[V] {
def sort(cmp: (V, V) => Int): C[V] = ???
def sort(cmp: java.util.Comparator[V]): C[V] = ???
}
val c: C[Int] = ???
c sort (_ - _)
c sort ((a: Int, b: Int) => a - b)
c sort ((_ - _): java.util.Comparator[Int])
scala> import java.util.{Arrays, List, ArrayList}
scala> import java.util.stream.{Stream, IntStream}
scala> val myList = Arrays.asList("a1", "a2", "b1", "c2", "c1")
scala> myList.stream.filter(s => s.startsWith("c")).
map(_.toUpperCase).
sorted.
forEach(println)
scala> Arrays.asList("a1", "a2", "a3").stream.
findFirst.
ifPresent(println)
scala> Stream.of("a1", "a2", "a3").
findFirst.
ifPresent(println)
scala> IntStream.range(1, 4).forEach(println)
scala> Arrays.stream(Array(1, 2, 3)).
map(n => 2 * n + 1).average.
ifPresent(println)
scala> Stream.of("a1", "a2", "a3").
map(_.substring(1)).
mapToInt(Integer.parseInt).max.ifPresent(println)
<console>:14: error: type mismatch;
found : (x$1: String)Int <and> (x$1: String, x$2: Int)Int
required: java.util.function.ToIntFunction[? >: ?0]
Stream.of("a1", "a2", "a3").map(_.substring(1)).mapToInt(Integer.parseInt).max.ifPresent(println)
^
scala> Stream.of("a1", "a2", "a3").
map[String](_.substring(1)).
mapToInt(Integer.parseInt).max.ifPresent(println)
3
implicit
without signature