Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

116
LINES

< > BotCompany Repo | #1034763 // test_leftArrowScript_lambdas

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (33503L) is out of date.

1  
!include once #1034831 // Gazelle 22 Function Include for Testing
2  
3  
scope test_leftArrowScript_lambdas
4  
5  
srecord #MyClass(int x) {}
6  
7  
svoid test_leftArrowScript_lambdas() {
8  
  // IF1 with global function (l)
9  
  
10  
  var test = new TestFunctionValues<S, O>(l1 leftArrowVerbose);
11  
  
12  
  test.io([[map (IF1 l) (ll "hey ho" "world")]], ll(6, 5)).runAndClear();
13  
  
14  
  // IF0 with global function and primitive widening
15  
  
16  
  test.io([[IF0 mul 2 5L, get]], 10L).runAndClear();
17  
    
18  
  // IF1 with global function & currying (plus)
19  
  
20  
  new TestFunctionValues<S, O>(l1 leftArrowVerbose)
21  
    .io([[map (IF1 plus 5) (ll 2 12)]], ll(7, 17))
22  
    .run();
23  
    
24  
  // IF0 with method on implicit object
25  
  
26  
  class X {
27  
    int i = 1;
28  
    int next() { ret i++; }
29  
  }
30  
31  
  new GazelleV_LeftArrowScriptParser parser;
32  
  parser.allowTheWorld(new X, mc());
33  
  assertEqualsVerbose(ll(1, 2, 3), parser.parse("repF 3 (IF0 next)")!);
34  
  
35  
  // lambda from script-defined function!
36  
  
37  
  test.io([[
38  
    def twice x { joinWithSpace x x }
39  
    map (IF1 twice) (ll "yes" "no")
40  
  ]], ll("yes yes", "no no"));
41  
  
42  
  // ...and once again with curried arguments
43  
  
44  
  test.io([[
45  
    def subFrom100 a b c { minus 100 (intSum a b c) }
46  
    map (IF1 subFrom100 10 5) (ll 2 22)
47  
  ]], ll(83, 63));
48  
  
49  
  test.runAndClear();
50  
  
51  
  // inline lambda def!!
52  
  test.io(
53  
    "map (IF1 a -> new Pair a (neg a)) (ll 1 2)",
54  
    ll(pair(1, -1), pair(2, -2))).runAndClear();
55  
    
56  
  // short version for constructor call
57  
  test.io(
58  
    "map (IF1 " + shortClassName(MyClass) + ") (ll 1 2)",
59  
    ll(MyClass(1), MyClass(2))).runAndClear();
60  
    
61  
  // curly block as lambda body
62  
  test.io(
63  
    "map (IF1 a -> { x <- plus a a; plus x x }) (ll 3 5)",
64  
    ll(3*4, 5*4)).runAndClear();
65  
    
66  
  // lambda body without arrow (when there are no arguments)
67  
  test.io(
68  
    "repF 2 < IF0 { 5 }",
69  
    ll(5, 5)).runAndClear();
70  
    
71  
  // short version for method/field access, e.g.: IF1 _ myMethod
72  
  test
73  
    .io([[ map (IF1 _ length) < splitAtSpace "hello you" ]],
74  
           ll(5, 3))
75  
    .io([[ map (IF1 _ charAt 0) < splitAtSpace "hello you" ]],
76  
           ll('h', 'y'))
77  
    .runAndClear();
78  
    
79  
  // hashCode() of a lambda
80  
  assertInstanceOf(leftArrowVerbose([[
81  
    lambda <- (IF1 a -> plus a a)
82  
    assertEquals (lambda hashCode) (lambda hashCode)
83  
    lambda hashCode
84  
  ]]), Int);
85  
  
86  
  // equals() of a lambda (just identity comparison)
87  
  leftArrowVerbose([[
88  
    lambda1 <- (IF1 a -> plus a a)
89  
    lambda2 <- IF1 neg
90  
    assertEquals lambda1 lambda1
91  
    assertNotEquals lambda1 lambda2
92  
    assertEquals lambda2 lambda2
93  
    assertNotEquals lambda2 lambda1
94  
  ]]);
95  
  
96  
  // lambda arrow binds tighter than assignment
97  
  test.io([[
98  
    action <- IF0 -> "ok"
99  
    action get
100  
  ]], "ok").runAndClear();
101  
  
102  
  // typed interface
103  
  test.io([[
104  
    (IF0<S> -> "ok") get
105  
  ]], "ok").runAndClear();
106  
  
107  
  // typed parameter
108  
  test.io(
109  
    [[(IF1 a: S -> new Pair a a) get "x"]],
110  
    pair("x")).runAndClear();
111  
    
112  
  // implement Comparator (special case because Comparator redeclares equals()
113  
  test.io([[
114  
    (Comparator a b -> cmp a b) compare 1 2
115  
  ]], -1).runAndClear();
116  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1034763
Snippet name: test_leftArrowScript_lambdas
Eternal ID of this version: #1034763/31
Text MD5: a2260ee663e322abd76c878e0998b5f2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2024-02-18 11:07:16
Source code size: 3185 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 429
Version history: 30 change(s)
Referenced in: [show references]