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

102
LINES

< > BotCompany Repo | #1034763 // test_leftArrowScript_lambdas

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

Transpiled version (33503L) is out of date.

!include once #1034831 // Gazelle 22 Function Include for Testing

scope test_leftArrowScript_lambdas

srecord #MyClass(int x) {}

svoid test_leftArrowScript_lambdas() {
  // IF1 with global function (l)
  
  var test = new TestFunctionValues<S, O>(l1 leftArrowVerbose);
  
  test.io([[map (IF1 l) (ll "hey ho" "world")]], ll(6, 5)).runAndClear();
    
  // IF1 with global function & currying (plus)
  
  new TestFunctionValues<S, O>(l1 leftArrowVerbose)
    .io([[map (IF1 plus 5) (ll 2 12)]], ll(7, 17))
    .run();
    
  // IF0 with method on implicit object
  
  class X {
    int i = 1;
    int next() { ret i++; }
  }

  new GazelleV_LeftArrowScriptParser parser;
  parser.allowTheWorld(new X, mc());
  assertEqualsVerbose(ll(1, 2, 3), parser.parse("repF 3 (IF0 next)")!);
  
  // lambda from script-defined function!
  
  test.io([[
    def twice x { joinWithSpace x x }
    map (IF1 twice) (ll "yes" "no")
  ]], ll("yes yes", "no no"));
  
  // ...and once again with curried arguments
  
  test.io([[
    def subFrom100 a b c { minus 100 (intSum a b c) }
    map (IF1 subFrom100 10 5) (ll 2 22)
  ]], ll(83, 63));
  
  test.runAndClear();
  
  // inline lambda def!!
  test.io(
    "map (IF1 a -> new Pair a (neg a)) (ll 1 2)",
    ll(pair(1, -1), pair(2, -2))).runAndClear();
    
  // short version for constructor call
  test.io(
    "map (IF1 " + shortClassName(MyClass) + ") (ll 1 2)",
    ll(MyClass(1), MyClass(2))).runAndClear();
    
  // curly block as lambda body
  test.io(
    "map (IF1 a -> { x <- plus a a; plus x x }) (ll 3 5)",
    ll(3*4, 5*4)).runAndClear();
    
  // lambda body without arrow (when there are no arguments)
  test.io(
    "repF 2 < IF0 { 5 }",
    ll(5, 5)).runAndClear();
    
  // short version for method/field access, e.g.: IF1 _ myMethod
  test
    .io([[ map (IF1 _ length) < splitAtSpace "hello you" ]],
           ll(5, 3))
    .io([[ map (IF1 _ charAt 0) < splitAtSpace "hello you" ]],
           ll('h', 'y'))
    .runAndClear();
    
  // hashCode() of a lambda
  assertInstanceOf(leftArrowVerbose([[
    lambda <- (IF1 a -> plus a a)
    assertEquals (lambda hashCode) (lambda hashCode)
    lambda hashCode
  ]]), Int);
  
  // equals() of a lambda (just identity comparison)
  leftArrowVerbose([[
    lambda1 <- (IF1 a -> plus a a)
    lambda2 <- IF1 neg
    assertEquals lambda1 lambda1
    assertNotEquals lambda1 lambda2
    assertEquals lambda2 lambda2
    assertNotEquals lambda2 lambda1
  ]]);
  
  // lambda arrow binds tighter than assignment
  test.io([[
    action <- IF0 -> "ok"
    action get
  ]], "ok").runAndClear();
  
  // typed interface
  test.io([[
    (IF0<S> -> "ok") get
  ]], "ok").runAndClear();
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034763
Snippet name: test_leftArrowScript_lambdas
Eternal ID of this version: #1034763/26
Text MD5: 71d9a3cc7d2b0747dea98da1c30090e9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-11 22:39:33
Source code size: 2784 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 88 / 318
Version history: 25 change(s)
Referenced in: [show references]