computed_entries.md · GitHub
/" data-turbo-transient="true" />/" data-turbo-transient="true" />
Skip to content
-->
Search Gists
Search Gists
Sign in
Sign up
You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
{{ message }}
Instantly share code, notes, and snippets.
chrispsn/computed_entries.md
Secret
Last active<br>March 20, 2024 12:57
Show Gist options
Download ZIP
Star
(1)
You must be signed in to star a gist
Fork
(0)
You must be signed in to fork a gist
Embed
Select an option
Embed<br>Embed this gist in your website.
Share<br>Copy sharable link for this gist.
Clone via HTTPS<br>Clone using the web URL.
No results found
Learn more about clone URLs
Clone this repository at <script src="https://gist.github.com/chrispsn/779ae79088fd6f432804ea33e3bdf74f.js"></script>
" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-1a515ea7-521b-4bb1-9a2f-e60d21b2a720" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-sized-down" />
Save chrispsn/779ae79088fd6f432804ea33e3bdf74f to your computer and use it in GitHub Desktop.
Embed
Select an option
Embed<br>Embed this gist in your website.
Share<br>Copy sharable link for this gist.
Clone via HTTPS<br>Clone using the web URL.
No results found
Learn more about clone URLs
Clone this repository at <script src="https://gist.github.com/chrispsn/779ae79088fd6f432804ea33e3bdf74f.js"></script>
" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-480283b4-7dab-431b-8712-a6f388217bd7" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-original" />
Save chrispsn/779ae79088fd6f432804ea33e3bdf74f to your computer and use it in GitHub Desktop.
Download ZIP
Raw
computed_entries.md
K: Computed entries in dict literals
A quick note on an idea. I'm not sure it's a good one.
Dict literals let us put keys and values next to each other. Compare [k1:"a";k2:"b"] to !/+((`k1;"a");(`k2;"b")).
Unfortunately, [] syntax is used by other k features. For example, k9 2021 had dict literals, but not blocks (like JavaScript's blocks but as [a;b]), and they needed disambiguation from m-exprs via a space (f [a:1] vs f[a:1]) or rounds ([a:1]).
Blocks can be handy in conds; IIFEs aren't a perfect substitute as they don't have local scope access.
Could we shuffle around the language syntax so that we can have it all?
The idea: 'computed dict entries'
Maybe we could merge dict literal and lambda syntax so that both use {}.
Dicts and lambdas are both maps, and they already share indexing syntax and some semantics. But dicts let you specify a finite list of entries, while lambdas let you specify infinite sets of points. We want a literal format that covers both cases, ideally within the same literal instance. We can do this by using []s to wrap a condition that represents a set of keys.
This also includes default dict values as discussed earlier, and we can use computed defaults for simple {xyz} inline lambdas.
The result? No more space-sensitivity of dict literals - and we keep blocks.
Here's how it looks. It superficially resembles JavaScript's computed property names but is closer to Rust's match semantics.
{a:1;b:2} / explicit points. if input is `a, return 1; if input is `b, return 2<br>{[x]:x+1} / computed points. if input is truthy, return input+1, else null (or error?)<br>{a:1;[x]:x+1} / if input is `a, return 1; otherwise same as above (mixes explicit and computed points)<br>{a:1;2} / explicit default value. if input is `a, return 1; otherwise return 2<br>{a:1;[x+1]} / computed default value<br>{0:1;[x*o x-1]} / factorial. recursive default with base case for 0 input. entry order matters!<br>{[~x~*x]:,/o'x;[x]} / if list, recurse; otherwise return the atom
{local} / default value. can see locals.<br>{[x+1]} / computed default value. this is what we'd use instead of a {x+1} lambda. can't see locals.<br>/ disambiguated from a computed domain because there's no colon after ].<br>/ disambiguated from an arg names list in a lambda because the lambda ends after the }.<br>{[x+y*1]} / computed default with two unknowns. equivalent to a {xyz} lambda. 2d, so can be projected.
{[preamble / sometimes we want to calculate some intermediate results before a lookup (like {a:1;a+1}).<br>test x]: x+1} / so, we use a block to compute the keys, putting the preamble first so that it always executes.<br>/ however, because it's in a computed entry, the preamble can't see locals outside the literal.<br>{[x+:1;y+x]} / normal xyz lambda with some preamble...