tategakibunko/jingoo

Anonymous functions

sagotch opened this issue · 1 comments

I often need to define custom compare functions while sorting data in templates, for instance:

{%- function cmp_fn (a, b) -%}
  {{ alphabetic (a.first_name, b.first_name) }}
{%- endfunction -%}
{%- set list = list | sort (compare = cmp_fn) -%}

In some case, it would be nice to be able to define function on the fly, like this:

 list | sort (compare = ((a, b) => alphabetic (a.first_name, b.first_name)))

Would you agree with this kind of feature?

Do you have a better syntax in mind? I picked

LPAREN separated_list(COMMA, ident) RPAREN => expr

because it should interact nicely with current parser.

Also, it is a form used by some fat arrows functions in javascript.

Note that it would restrict anonymous function to very simple functions (no instruction, only one expression as function body), which is probably better anyway.

Anonymous functions are a great idea!

LPAREN separated_list(COMMA, ident) RPAREN => expr

I think the grammar is natural and easy to read.