Functions/variables in Object is similar to the static function and variable in Java.
What is ought to be highligted is the use of "apply". SomeObject.apply(v:Int) equals SomeObject(v:Int)
Finally, we should know the usage of code block.
We can create a code block anywhere, and the last line is the result of this block.
For example,
We can use the follow lines instead:
A better practice here is:
Case Study of some Common Types
Option, Some, None
We can use null in Scala as null pointer, but it is not recommended. We are supposed to use Option[SomeType] to indicate this variable is optional.
We can assueme every variable without Option are not null pointer if we are not calling java code. This help us reduce a lot of code.
If we wanna to get a variable with Option, here are two method
Besides, we can also use method "isDefined/isEmpty".
What should be highlighted is Option(null) returns None,
but Some(null) is Some(null) which is not equals None.
match is a useful reserved words, we can use it in various of situations
Firstly, we can use it as "switch" & "case" in some other programming languages.
Secondly, we can find it by the type of the data,
Given a case class B, but we only wish to retrievee the value B.a.j, we can use "_" as placeholder.
Common methods in List, Array, Set, and so on
In scala, we always transfer the List( Array, Set, Map etc.) from one status to another status. the methods of
toList, toArray, toSet -- convert each other
par -- Parallelize List, Array and Map, the result of Seq[Int]().par is ParSeq[Int], you will able to process each element in parallel when you are using foreach, map etc., and unable to call "sort" before you are using "toList".
distinct -- Removes duplicate elements
foreach -- Process each element and return nothing
It wil print 1, 2, 3 in order
It will print 1, 2, 3, but the order is not guaranteed.
map -- Process each element and construct a List using return value
It will return List[Int](2,3,4)
The result of List[A]().map(some-oper-return-type-B) is List[B], while the result of Array[A]().map map is Array[B].
flatten -- The flatten method takes a list of lists and flattens it out to a single list:
flatMap -- The flatMap is similar to map, but it takes a function returning a list of elements as its right operand. It applies the function to each list element and returns the concatenation of all function results.
The result equals to map + flatten
collect -- The iterator obtained from applying the partial function to every element in it for which it is defined and collecting the results.
The function match elements in Int and Double, process them and return the value, but ignore string elements.
filter -- Filter this list
filterNot -- Similar to filter
forall -- Return true if All elements are return true by the partial function. It will immediately return once one element returns false, and ignore the rest elements.
exists -- Return true if there are at least One element returns true.
find -- Return the first element returns true by the partial function. Return None if no elemet found.
sortWith -- sort the elements
zipWithIndex --
It will rebuild a List with index
for - Scala's keyword for can be used in various of situations.
Basically,
It equals:
Besides,
We will get the cartesian product of List(1,2,3) and List(4,5,6): List((1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6))
We can add a filter in condition:
the result is : List((3,5))
Another usage of for is as follow:
Let's define variables as follow:
We can execute like this:
The response is:
Let's define b as None:
while - Similar to while in java
to, until — (1 to 10) will generate a Seq, with the content of (1,2,3,4…10), (0 until 10) will generate a sequence from 0 to 9. With some test, (0 until 1000).map(xxx) appears to be slower than var i=0; while( i < 1000) { i += 1; sth. else}, but if the body of map is pretty heavy, this cost can be ignored.
headOption - Get the head of one list, return None if this list is empty
head - Get the head of one list, throw exception if this list is empty
take -- Get first at most N elements. (from left to right)
drop -- Drop first at most N elements.
dropRight will drop elements from right to left.
slice -- Return list in [start-offset, end-offset)
If the end-offset is greater than the length of this list, it will not throw exception.
splitAt -- Split this list into two from offset i
groupBy -- Partitions a list into a map of collections according to a discriminator function
partition -- Splits a list into a pair of collections; one with elements that satisfy the predicate, the other with elements that do not, giving the pair of collections (xs filter p, xs.filterNot p).
grouped -- The grouped method chunks its elements into increments.
Credit to Yu Jing PDF(with copyright issue): https://doc.argcv.com/scala/Programming.in.Scala.2ed.pdf