Advertisement
Revolucent
Jan 31st, 2023
83
0
Never
This is comment for paste Prefix sum - Swift
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public extension Collection {
  2.   func inits<T>(_ transform: (SubSequence) throws -> T) rethrows -> [T] {
  3.     try (0...count).map { try transform(prefix($0)) }
  4.   }
  5.  
  6.   func inits() -> [SubSequence] {
  7.     inits { $0 }
  8.   }
  9. }
  10.  
  11. print([1, 7, 14, 9, 11].inits { $0.reduce(0, +) })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement