Use case or problem
Introduce a constant inside a function scope (for reusing/naming a result).
If I am working with a more complex formula in bases, I have only one easy way to simplify the process and that is to break it down into multiple formulae, and use the output of one in another. E.g. file.backlinks.contains(formula.other_formula)
However, this is not directly possible when inside a map
. If I am mapping e.g. over the backlinks of a file, and for each of those backlinks computing something further, it might be nice to be able to assign the further computation to a name.
It is possible in to refactor as a series of maps and filters (also see the workaround below) but this is what I normally end up with
file.backlinks.filter(
value.asFile().backlinks.filter(another_function_etc).length > 0
/* Exact same function of the backlink written below */
&& value.asFile().backlinks.filter(another_function_etc).length < 10
)
which could be
file.backlinks.filter(
/*result defined somewhere here?*/
result > 0 && result < 10
)
Proposed solution
A couple of ideas:
1. Naming variables
Taking inspiration from the ~somewhat similar pipelines in jq, we could write something like this
file.backlinks.filter(
with value.asFile().backlinks.filter(another_function_etc).length as result;
result > 0 && result < 10
)
which could be extended for multiple definitions like e.g.:
file.backlinks.filter(
with <function_1_of_value> as result, <function_2_of_value> as result_2;
result > 0 && result < 10
)
let
… in
is another syntax that is often used.
2. Defining objects
Make it possible to create objects, which could be used to store intermediate results, e.g.:
file.backlinks
.map(
/* Make an intermediate object, storing the result alongside the backlink */
{"link": value, "func_result": value.asFile.backlinks...}
).filter(
value["result"] > 0 && value["result"] < 10
).map(value["link"])
Current workaround - using lists
While I don’t believe you can create objects, you can create lists, and store intermediate values in the list, e.g:
file.backlinks
.map(
/* Make a list, storing the result alongside the backlink */
[value, value.asFile.backlinks...]
).filter(
value[1] > 0 && value[1] < 10
).map(value[0])