3 ms·
As one potential example, it would be useful for implementing something similar to Racket's 'dispatch-rules' (https://docs.racket-lang.org/web-server/dispatch.h
by CamTin 8y ago
As one potential example, it would be useful for implementing something similar to Racket's 'dispatch-rules' (https://docs.racket-lang.org/web-server/dispatch.html#%28form._%28%28lib._web-server%2Fdispatch..rkt%29._dispatch-rules%29%29 https://docs.racket-lang.org/web-server/dispatch.html#%28for...) in JavaScript.
Specifically, the second return value from this macro is a procedure that generates URLs given a handler function. This allows one to go both ways in URL-handler mapping (generating URLs from handlers/controllers) instead of just one-way (mapping requests to specific handlers/controllers) as in a conventional router in Rails or Django.
Once you learn about this pattern, you start to see it in more places. A slightly different example, I am working on a mail router (something akin to procmail) in Racket, and have used a similar pattern to map from predicates (procedures taking an email and returning true or false depending on certain conditions) to symbols representing those predicates in a DSL. A hash table with procedures for keys lets me do this mapping in both directions should I need to.
(Note that I am not actually sure whether dispatch-rules itself uses procedure-keyed dictionaries or hash tables under the hood.)