Beton icon indicating copy to clipboard operation
Beton copied to clipboard

Revenge of the Splat

Open adam-rocska opened this issue 1 year ago • 1 comments

Swift had tuple Splatting, which was awesome. Now we don't have it, which sux.

I've been missing it for years now as I have it on my standard toolbelt / programming logic coming from different languages.

I have no clue who this genius is on Swift Forums, but this comment gave me the idea below:

import Foundation

infix operator |> : MultiplicationPrecedence
func |> <Input, Output>(
  input: Input,
  lambda: (Input) -> Output
) -> Output { lambda(input) }

func call<Args, Result>(with args: Args, function: (Args) -> Result) -> Result {
  function(args)
}

func add(_ a: Int, _ b: Int) -> Int { a + b }
// this still compiles today
let input = (40, 2)
print(call(with: input, function: add))

print(input |> add)

adam-rocska avatar Nov 07 '23 19:11 adam-rocska