Skip to content

Commit

Permalink
Merge pull request #65 from jechol/fix-scan
Browse files Browse the repository at this point in the history
Fix scan to emit initial value
  • Loading branch information
srdanrasic committed Apr 19, 2016
2 parents 722c69e + 53e9942 commit ee450e2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
6 changes: 2 additions & 4 deletions Sources/RawStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public extension RawStreamType {
public func scan<U: EventType>(initial: U, _ combine: (U, Event) -> U) -> RawStream<U> {
return RawStream<U> { observer in
var accumulator = initial
observer.observer(accumulator)
return self.observe { event in
accumulator = combine(accumulator, event)
observer.observer(accumulator)
Expand Down Expand Up @@ -775,10 +776,7 @@ public extension RawStreamType {
/// Reduce stream events to a single event by applying given function on each emission.
@warn_unused_result
public func reduce<U: EventType>(initial: U, _ combine: (U, Event) -> U) -> RawStream<U> {
return RawStream<U> { observer in
observer.observer(initial)
return self.scan(initial, combine).observe(observer.observer)
}.takeLast()
return scan(initial, combine).takeLast()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/OperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class OperatorsTests: XCTestCase {
func testScan() {
let operation = Operation<Int, TestError>.sequence([1, 2, 3])
let scanned = operation.scan(0, +)
scanned.expectNext([1, 3, 6])
scanned.expectNext([0, 1, 3, 6])
}

func testToOperation() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/StreamTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StreamTests: XCTestCase {
func testScan() {
let stream = Stream.sequence([1, 2, 3])
let scanned = stream.scan(0, +)
scanned.expectNext([1, 3, 6])
scanned.expectNext([0, 1, 3, 6])
}

func testToOperation() {
Expand Down

0 comments on commit ee450e2

Please sign in to comment.