counter: Create and manipulate counters

Description Usage Arguments Details Value Author(s) Examples

Description

Functions to create and manipulate counters.

Usage

1
2
3

Arguments

start

counter starting value.

x

counter stored value.

step

value to increment/decrement from the counter stored value.

Details

A counter is a function that takes another function as an argument. When no function is supplied, the counter just returns its stored value.

The increment and decrement functions are provided because they are the most likely to be of any use, but any function may be used.

Copied counters created from the same original countre share the same stored value. This may be used to share states between objects.

Value

A counter returns its value when called without any argument, or the return value of the supplied function applied to the stored value.

Author(s)

Antoine Filipovic Pierucci

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# counter creation
counter <- makeCounter()

# return stored value
counter()

# incrementing/decrementing
counter(increment)
counter(increment, 5)
counter(decrement, 2)

# arbitrary function
counter(function(x) x * x)

# shared memory
counter2 <- counter

counter()
counter2(increment)

counter()

# starting value may be anything
counter3 <- makeCounter(start = "a")
counter3()

counter3(function(x) c(x, "b"))

rmngb documentation built on May 29, 2017, 9:22 p.m.

Related to counter in rmngb...