Lambda.k 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. invoke {
  2. // inline lambda
  3. let f = .{ .x*.x }
  4. assert f is Function
  5. assert f is Arity<1>
  6. assert f(5) == 25
  7. let f1 = .{ .x*.y }
  8. assert f1 is Function
  9. assert f1 is Arity<2>
  10. assert f1(7,7) == 49
  11. let f2 = .{ x,y --> x^y }
  12. assert f2 is Arity<2>
  13. assert f2(2,10) == 1024
  14. let f3 = .{ _,y --> y }
  15. assert f3 is Arity<2>
  16. assert f3(7,11) == 11
  17. let f4 = .{ 59 }
  18. assert f4 is Arity<0>
  19. assert f4() == 59
  20. let f5 = .{ _ --> 59 }
  21. assert f5({}) == 59
  22. let f6 = .{ .{ .x + 1 } }
  23. assert f6()(4) == 5
  24. let f7 = .{ (.{ .y*5 })(.x) }
  25. assert f7(9) == 45
  26. let f8 = .{ x --> .{ x + .y } }
  27. assert f8('3')('5') == '35'
  28. assert f8('5')('3') == '53'
  29. let f9 = .{ .x + .y }
  30. assert f9('3', '5') == '35'
  31. assert f9('5', '3') == '53'
  32. // block lambda
  33. let g = lambda { return 59 }
  34. assert g is Function
  35. assert g is Arity<0>
  36. assert g() == 59
  37. let g1 = lambda -> Int { return 59 }
  38. assert g1() == 59
  39. let g2 = lambda() -> Int { return 59 }
  40. assert g2() == 59
  41. let g3 = lambda() { return 59 }
  42. assert g3() == 59
  43. let g4 = lambda -> { return 59 }
  44. assert g4() == 59
  45. let h = lambda(x: Int, y: Int) { return x + y }
  46. assert h is Function
  47. assert h is Arity<2>
  48. assert h(4,5) == 9
  49. let h1 = lambda (x: Int, y: Int) -> Int { return x * y }
  50. assert h is Arity<2>
  51. assert h1(4,5) == 20
  52. let h2 = lambda(x: String) -> { return x + '1' }
  53. assert h2 is Arity<1>
  54. assert h2('3') == '31'
  55. }