seq.stateMap(_:_:)

extension Sequence {
/// `seq.stateMap(_:_:)`:
/// map a sequence with a local state.
public func stateMap<State, Result>(
// intial local state
_ initialState: State,
// calculate the result from current element & state,
// state may be updated upon each call.
_ result: (inout State, Element) -> Result
) -> [Result] {
var state = initialState
return map { elem in
return result(&state, elem)
}
}
}
Last updated
Was this helpful?