21GramConsulting/Beton

Revenge of the Splat

Opened this issue · 0 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)