scala - Explanation of List[_] in pattern matching used in flatten function -
scala - Explanation of List[_] in pattern matching used in flatten function -
i new scala , can not understand next function
val l = list(list(1, 1), 2, list(3, list(5, 8))) def flatten(l: list[any]): list[any] = l flatmap { case ms:list[_] => flatten(ms) case l => list(l) } flatten(l) // res2: list[any] = list(1, 1, 2, 3, 5, 8) in particular not understand combination of flatmap , pattern matching , meaning of first case ms:list[_]
can explain , maybe provide simpler illustration clarify concept?
map , flatmap
first of flatmap higher-order function.
map higher order function convert list new list applying function on elements of list. illustration if have
val l = list(1,2,3) you can map new list using
val doubled = l.map(_ * 2) // list(2,4,6) so flatmap? flatmap useful when function gets int returns list[int]. in case if pass function map list[list[int]] want list[int] instead.
what can utilize flatmap instead. mathematically equivalent first map , flatten result in action flatten function defined based on flatmap not other way around.
technical details aside, flatten means if have list[list[list...[int]]], if flatten it, list[int].
when @
def flatten(l: list[any]): list[any] = l flatmap { case ms:list[_] => flatten(ms) case l => list(l) } we first need know mean. know have pass function flatmap. in case function passing
{ case ms:list[_] => flatten(ms) case l => list(l) } which @ first not seem function is! partial function can consider function nuances!
what partial function?we accomplish (almost) same result with:
def flatten(l: list[any]): list[any] = l flatmap { _ match { case ms:list[_] => flatten(ms) case l => list(l) } } the difference between ordinary function , partial function partial functions may undefined specific inputs. compiler automatically generate partial function body starts case keyword (it's not simple sake of simplicity skip details here).
a pattern ms:list[_] called type pattern. means type of ms checked in runtime against list[_]. _ wild card , means type ms:list[_] literally means list of type (due type erasure can not utilize pattern ms:list[int], refer this thread more info).
so whole pattern match on code snippets means
ifms list result flatten(ms), other wise result list(l). the flatten function defined recursive function unwraps lists , until there no more list, wrap result 1 time again in list! way list[list[list.......[_]]] converted list[_].
an additional illustration type-patterns:
def hello(o: any) = o match { case _:int => "hi, int , i'm proud of it!" case _:list[_] => "i have many things tell you! list after all!" case b:boolean => s"i'm flag , value $b" case _ => "i'm else can imagine :d" } disambiguation by way partial function totally different partially applied function , partial application!
the concept of partial function in scala same partial function in mathematics: function may undefined of values of domain.
scala pattern-matching flatmap partial-functions
Comments
Post a Comment