Skip to content

Instantly share code, notes, and snippets.

View Czajnikowski's full-sized avatar
🚀

Maciek Czarnik Czajnikowski

🚀
View GitHub Profile
extension Sequence {
func reduce<Result>(
_ initialResult: Result,
_ nextPartialResult: (Result, Self.Iterator.Element) throws -> Result,
until conditionPassForResult: (Result) -> Bool
) rethrows -> Result {
return try reduce(
initialResult,
{
(1 ... 5).reduce(
0,
+,
until: { partialSum in partialSum > 5 }
) // Returns 6
extension Sequence {
func reduce<Result>(
_ initialResult: Result,
_ nextPartialResult: (Result, Self.Iterator.Element) throws -> Result,
until conditionPassFor: (Result, Self.Iterator.Element) -> Bool
) rethrows -> Result {
do {
return try reduce(
initialResult,
extension Sequence {
func reduce<Result>(
_ initialResult: Result,
_ nextPartialResult: (Result, Self.Iterator.Element) throws -> Result,
while conditionPassFor: (Result, Self.Iterator.Element) -> Bool
) rethrows -> Result {
do {
return try reduce(
initialResult,
(1 ... 5).reduce(
0,
+,
while: { partialSum, _ in partialSum < 5 }
) // Returns 3
private struct BreakConditionError<Result>: Error {
let result: Result
}
func show(withAnimation animation: Animation?)
object.show(withAnimation: nil)
enum WrappedAnimation {
case fromBottom // Anonymous nil before
case specific(Animation)
}
object.show(withWrappedAnimation: .fromBottom)