Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Last active April 14, 2021 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytyubox/583b73eece7885713813fc18d5f5c1e0 to your computer and use it in GitHub Desktop.
Save ytyubox/583b73eece7885713813fc18d5f5c1e0 to your computer and use it in GitHub Desktop.

From

var fib_6 = {
  v: Int -> Int  in
  if v <= 1  { return v }
  return ???(v - 1) + ???(v - 2)
}(6)

to

  🤫 { (v, fib) -> Int in
      if v <= 1 { return v }
      return fib(v - 1) + fib(v - 2)
  }(6)
struct 🤫<IN, OUT> {
init(function: @escaping (IN, 🤫<IN, OUT>) -> OUT) {
self.closure = {
(_in: IN, `self`: 🤫<IN, OUT>) in
try function(_in, self)
}
}
let closure: (IN, 🤫<IN, OUT>) -> OUT
func callAsFunction(_ in: IN) -> OUT {
closure(`in`, self)
}
}
class 🤫Tests: XCTestCase {
func test() throws {
XCTAssertEqual(
8,
🤫 { (v, fib) -> Int in
if v <= 1 { return v }
return fib(v - 1) + fib(v - 2)
}(6))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment