oscbyspro / Ultimathnum

Binary arithmetic reimagined in Swift
Apache License 2.0
9 stars 1 forks source link

Add: Fallible.sink(_:performing:) #128

Open oscbyspro opened 1 week ago

oscbyspro commented 1 week ago

I stumbled upon this ceremony while working on composable (#126) algorithms:

func algorithm<T>(_ value: consuming T) -> Fallible<T> {
    var error: Bool = false

    value = foo(value).sink(&error)
    value = bar(value).sink(&error)
    value = baz(value).sink(&error)

    return Fallible(value, error: error)
}

It isn't terrible, but a Fallible.sink(_:performing:) is perhaps more ergonomic:

func algorithm<T>(_ value: consuming T) -> Fallible<T> {
    Fallible.sink(consume  value) { 
        value,  error in
        value = foo(value).sink(&error)
        value = bar(value).sink(&error)
        value = baz(value).sink(&error)
    }
}