[Initial feedback] Potential proposal for user-defined function
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 20/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- python, typescript
- Ambito
- compilers
Direzione di ricerca
Inizia dalla discussione JEP collegata e dal testo della proposta, poi esamina gli esempi referenziati in Lexer.type.ts, Parser.ts, Runtime.ts, TreeInterpreter.ts e test/jmespath-interpreter.spec.ts. L’issue non definisce un obiettivo di implementazione né criteri di accettazione; sarebbe necessario decidere in merito alla grammatica, al contesto di valutazione, alla ricorsione e al riutilizzo delle funzioni prima di poter considerare concluso il lavoro.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I’m investigating user-defined lambda-like functions as discussed here.
Is this something of interest to discuss here?
User-defined functions
JMESPath already supports expression-type which are, conceptually, anonymous functions without arguments.
I have toyed with the idea of extending exprefs to support named arguments.
An updated grammar that works looks like this:
expression-type = "&" expression / "<" arguments ">" "=>" expression
arguments = variable-ref *( "," variable-ref
It requires a new token => which is even not absolutely necessary. Parsing involves a new entry in the nud() function to match on token < (less-than-sign).
Reduce function
As expression-type are only supported as function arguments, a reduce()
function could work like this:
reduce(array $array, any $seed, expr [any, any]->any)
Example:
[1, 3, 5, 7]
reduce(@, `1`, <$acc, $cur> => $acc × $cur)
The reduce() function knows how to iterate over its first array argument
and repeatedly create bindings for both function arguments $acc and $cur.
Funnily enough, the lambda function does not support natively the @ current node.
An argument can be made that it could be similar to the $cur argument. In that case,
a choice must be made as to which "context" is specified when evaluating the expref.
Reusing user-defined functions
I then investigated what reusing user-defined functions could look like.
This requires a mechanism to store or hold functions, possibly by name,
as well as a way to call those functions with appropriate parameters.
By taking advantage of the hopefully upcoming let-expression design, one
could bind the anonymous function to a variable reference. Then, syntax for
calling the function using this variable must be extended and supported in
the grammar.
In the following expression:
let $concat = <$lhs, $rhs> => join('-', [$lhs, $rhs])
in $concat('4', '2')
$concat is bound to the <$lhs, $rhs> => join('-', [$lhs, $rhs]) lambda expression-type. Then, $concat('4', '2') invokes the function indirectly using the $concat variable reference.
The following grammar changes is required:
function-expression = ( unquoted-string / variable-ref ) no-args / one-or-more-args )
Again, implementation is quite easy.
Funnily enough, having also implemented arithmetic-expression grammar rules, I was then able to implement the recursive Fibonacci sequence using the following expression:
let $fib = <$n> => (
($n == `0` && `0`) ||
(($n == `1` && `1`) ||
( $fib($n - `1`) + $fib($n - `2`) )
)
) in
$fib(@)
- Lingua principale
- Python
- Stelle
- 10
- Fork
- 3
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di jmespath/jmespath.jep
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
jmespath/jmespath.jep#36 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#35 ·
-
Root node accessAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#34 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#33 ·
-
Allow string comparisonAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#32 ·
Tutte le issue di jmespath/jmespath.jep
Issue simili
-
test_real_pandoc_failure_is_one_path_free_line fails on macOS CI with current Homebrew pandocApertabug TAG: unit tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
learningequality/ricecooker#898 ·
-
task
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
freelawproject/litigant-portal#1285 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
jmespath/jmespath.py#401 ·
-
least_squares returns a nonfinite objective for an invalid initial residualForse già presa @sylvesterkaczmarek l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
google-deepmind/mujoco#4023 ·
I maintainer di solito rispondono entro 11 giorni
-
Five Australian birds: Red-capped Parrot, Red-winged Fairywren, Gilbert’s Honeyeater, Western Spinebill, Western WattlebirdForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
arnegiacomo/fugleramme#427 ·
I maintainer di solito rispondono entro 1 giorno