Permalink
Browse files

[feature] syntax,js-like: loop sugar syntax

f(function (a,b){ a+b })

can be rewrite as : (see previous commit)

f({function (a,b) a+b })

which can be rewrite as

f{function (a,b) a+b }

The purpose is to be able to have loop syntax like in usual langage (Javascrip, C ...).

For instance, let's define the functional for loop in opa:

function for(b,e)(acc)(f){
  recursive function aux(i,acc){
    if(i<=e) aux(i+1,f(i,acc))
    else acc
  }
  aux(b,acc)
}

Now we can write a for loop very easily:
v =
for(1,10)(0){ function(i,acc)
  for(1,10)(acc){ function(j,acc)
    acc + i*j
  }
}

// to be compare to

// javascript
var acc = 0
for(var i=1;i<=10;i++){
  for(var i=1;i<=10;i++){
    acc += i*j
  }
}
var v = acc;

// classic syntax
v =
for(1,10)(0)(i,acc->
  for(1,10)(acc)(j,acc->
    acc + i*j
  )
)

//previous js-like situation
v =
for(1,10)(0)(function(i,acc){
  for(1,10)(acc)(function(j,acc){
       acc + i*j
      })
    })
  • Loading branch information...
1 parent 267c9b6 commit 77b026ac94c07914c30505ed2e2ba22dc309d20d @OpaOnWindowsNow OpaOnWindowsNow committed Dec 22, 2011
Showing with 1 addition and 0 deletions.
  1. +1 −0 opalang/js_syntax/opa_parser.trx
@@ -1389,6 +1389,7 @@ function_arguments <- function_arguments_nosp
;function_arguments_dot_method_nosp = careful_deco(just_function_arguments_dot_method_nosp)
;just_function_arguments_dot_method_nosp <-
/ function_arguments_nosp2:args {{ `function_call args }}
+ / (=careful_deco(lambda_as_block)):e {{ `function_call [`expr e] }}
/ "." Opa_lexer.field_identifier_nosp:i {{ `dot i }}
/ ".." Opa_lexer.ml_identifier_nosp:i {{ `double_dot i }}

0 comments on commit 77b026a

Please sign in to comment.