There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

sudo ,

Very standard use case for a fold or reduce function with an immutable Map as the accumulator


<span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">ints </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">List</span><span style="color:#323232;">(</span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="color:#0086b3;">2</span><span style="color:#323232;">, </span><span style="color:#0086b3;">2</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">, </span><span style="color:#0086b3;">3</span><span style="color:#323232;">)
</span><span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">sum </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> ints.foldLeft(</span><span style="color:#0086b3;">0</span><span style="color:#323232;">)(_ + _) </span><span style="font-style:italic;color:#969896;">// 14
</span><span style="font-weight:bold;color:#a71d5d;">val </span><span style="color:#323232;">counts </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> ints.foldLeft(</span><span style="color:#0086b3;">Map</span><span style="color:#323232;">.empty[</span><span style="font-weight:bold;color:#a71d5d;">Int</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">Int</span><span style="color:#323232;">])((c, x) </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">{
</span><span style="color:#323232;">  c.updated(x , c.getOrElse(x, </span><span style="color:#0086b3;">0</span><span style="color:#323232;">) + </span><span style="color:#0086b3;">1</span><span style="color:#323232;">)
</span><span style="color:#323232;">})
</span>

foldLeft is a classic higher order function. Every functional programming language will have this plus multiple variants of it in their standard library. Newer non-functional programing languages will have it too. Writing implementations of foldLeft and foldRight is standard for learning recursive functions.

The lambda is applied to the initial value (0 or Map.empty[Int, Int]) and the first item in the list. The return type of the lambda must be the same type as the initial value. It then repeats the processes on the second value in the list, but using the previous result, and so on until theres no more items.

In the example above, c will change like you’d expect a mutable solution would but its a new Map each time. This might sound inefficient but its not really. Because each Map is immutable it can be optimized to share memory of the past Maps it was constructed from. Thats something you absolutely cannot do if your structures are mutable.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines