Scala-from merging two maps-Foldleft and foldright also have pattern matching

Source: Internet
Author: User

Requirements encountered in development: merging two Map collection objects (adding the values of two corresponding keys)

First say the solution:

/  Case = +  - + 0))}

What the Hell (╯‵-′) ╯ "" ┻━┻☆)) >0<) ... Do not worry, and listen to my slow way ...

First of all:

The existing merge collection operations in Scala do not meet this requirement.

Note that the G02 value of the merged result a is actually overwritten.

And then:

Say that in the expression (a /: b) (...) what the hell is this part of? This is actually a Scala simplified foldleft function.

See Foldleft first.

List (foldleft) (0) ((sum,i) =>sum+i)//Red part is the initial value, the blue part is the operation function

List (Foldleft) (0) ((sum,i) =>sum+i) can be written (List (all in a) Foldleft 0) ((sum,i) =>sum+i) syntax sugar (0 /: List (+)) (_+_)  

Operator designer's brain hole is enough--the developer is definitely the expression Emperor.

If you accept this setting ... Foldright can also be brain-mended. Must be ((1 to 5) : \ N) ((i,sum) = Sum-i) ....

Other than that. An example illustrates Foldleft and foldright:

finally:

Speaking of the case in the operation function, this ghost is called pattern matching (oh yes, the pattern match is to have a curly brace wrapped up so that the final result of the operation function is wrapped with curly braces . )

The map's collapse function is a key-value pair that passes in the map sequentially. So the operation function expects the operand to be passed in as (k,v) Form: Then use the CASE expression: (map, (K,V))

Exactly what the pattern match is. Simply put, Scala will try to match the incoming value to the look of the expression after the case (which will certainly match, so there is no extra branch to write it) specifically what is a "pattern match", the current understanding is still shallow here to speculate in depth.

One Try wins thousand words:

Reference

1) Http://stackoverflow.com/questions/7076128/best-way-to-merge-two-maps-and-sum-the-values-of-same-key

2) http://blog.csdn.net/wsscy2004/article/details/37698013

3) http://my.oschina.net/sulliy/blog/58266

Scala-from merging two maps-Foldleft and foldright also have pattern matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.