Do "Optional.reduce" and "Bool.reduce" have common names?

SwiftUI is partially an experiment to test those waters, I think. They're nice! :ocean::surfing_man:

But there are other possibilities, if we can get a standard library boxing type. :gift::boxing_glove:

struct Struct {
  @Wrapped var value = "🐈"
}

Struct().$value { "πŸƒ \($0)" }
Struct().$value.if(isSnackTime) { "πŸƒ \($0)" }
@propertyWrapper struct Wrapped<Value> {
  let wrappedValue: Value
  var projectedValue: Self { self }
}

extension Wrapped {
  typealias Transform = (Value) throws -> Value

  func callAsFunction(transform: Transform) rethrows -> Value {
    try transform(wrappedValue)
  }

  func `if`(_ condition: Bool, transform: Transform) rethrows -> Value {
    condition
      ? try transform(wrappedValue)
      : wrappedValue
  }
}

…just make the $ do what the Β’ does here, and remove the need for parentheses.

prefix operator Β’
prefix func Β’<Value>(value: Value) -> Wrapped<Value> {
  .init(wrappedValue: value)
}

(Β’"🐈") { "πŸƒ \($0)" }
(Β’"🐈").if(isSnackTime) { "πŸƒ \($0)" }
1 Like