Skip to content

Instantly share code, notes, and snippets.

@danielt1263
Last active October 14, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielt1263/0be3431c880dbbfaa254298850453df3 to your computer and use it in GitHub Desktop.
Save danielt1263/0be3431c880dbbfaa254298850453df3 to your computer and use it in GitHub Desktop.
//
// Sequence+Scan.swift
//
// Created by Daniel Tartaglia on 10/13/2018.
// Copyright © 2018 Daniel Tartaglia. MIT License.
//
extension Sequence {
public func scan<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> [Result] {
return try reduce([Result]()) { $0 + [try nextPartialResult($0.last ?? initialResult, $1)] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment