Skip to content

Latest commit

 

History

History
58 lines (37 loc) · 2.35 KB

accumulator.md

File metadata and controls

58 lines (37 loc) · 2.35 KB
id lesson title layout class preview_image preview_image_alt
accumulator
25
Reactive Programming - Operators and functions (Part 2)
default
post
accumulator/content_preview.jpg
accumulator function

In Part 1, you've learned:

  • Some operators may accept functions as arguments. These functions don't have to work with the input or the output stream. The operators do.
  • How you label a function (eg. "project" or "predicate" function) depends on how you use it. A function is not a project or predicate function in itself.

Accumulate values

Today we focus on functions used as accumulator functions.

{:.w450}

A function that:

  • takes one value 1 and one value 2
  • and returns one new value
  • where value 1 and new value are of the same type

may be used as an accumulator function with the scan and reduce operators. In this case, value 1 and new value are often called "accumulation" or "acc".

Despite the names accumulator and accumulation, accumulator functions don't store the result "for later use". The operators do save the result. They will eventually give it back to their accumulator function. This is why accumulator functions need to take an accumulation as an argument.

ord wword

This function has no memory

Ignore the value

Even if a function is used as an accumulator function, it doesn't have to "accumulate" the two input values. It can ignore the second argument:

{:.w450}

count valuecount + 1

This function only increments the
accumulation count

Summary

  • An accumulator function returns a value of the same type as its first argument.
  • It can ignore its second argument or accumulate it with the first one.
  • It doesn’t have to remember the result.

See also

{:.w300}
project vs predicate

{:.w300}
reduce vs scan