From 5de49b1d4d690c70beefe22823fc54872e67f22f Mon Sep 17 00:00:00 2001 From: Daniel Tartaglia Date: Sun, 24 May 2015 20:36:28 -0400 Subject: [PATCH] Add 'aggregate' function and spelling corrections. --- OSXPlayground.playground/Contents.swift | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/OSXPlayground.playground/Contents.swift b/OSXPlayground.playground/Contents.swift index e2afda94..a3dc32e2 100644 --- a/OSXPlayground.playground/Contents.swift +++ b/OSXPlayground.playground/Contents.swift @@ -135,7 +135,7 @@ var onlyEvensSubscriber = multipleObservable This filter tracks the last value emitted and removes like values. This function is good for reducing noise in a sequence. */ -let debugSubscriber = returnElements(1, 2, 3, 1, 1, 4) +let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 1, 1, 4) >- distinctUntilChanged >- subscribeNext { value in println("\(value)") @@ -143,8 +143,22 @@ let debugSubscriber = returnElements(1, 2, 3, 1, 1, 4) /*: In the example above, the values 1, 2, 3, 1, 4 will be printed. The extra 1 will be filtered out. +There are several different versions of `distinctUntilChanged`. Have a look in the file Observable+Single.swift to review them. */ +/*: +## Aggregating a sequence + +### `aggregate` +This function will perform a function on each element in the sequence until it is completed, then send a message with the aggregate value. It works much like the Swift `reduce` function works on sequences. +*/ + +let aggregateSubscriber = multipleObservable + >- aggregate(0, +) + >- subscribeNext { value in + println("\(value)") +} + /*: To be continued... */