How does var work?
var — this is a function for getting the value of a variable, assigning a value to a variable, and creating a variable with a default value.
Return value — always when calling var.
To assign a value to a variable — you need to pass the new value in the value parameter:
=: var(name: "count"; value: $#lc)
// Assigned an empty list to count
// Since the return value always occurs when calling var, the result of this code will be an empty list
To get the value of a variable in the code using a quick variable:
=: $#count
But if such a variable has not been defined previously — there has been no action recording a value to it, then such code will complain about a non-existent variable!
To prevent this, use the default parameter:
=: var(name: "count"; default: $#lc)
// The code will try to get the value of count, see that the variable does not exist, and return the default value
Within what scope does a variable defined in var work?
The variable is only accessible in the current code during the evaluation of a specific cell.
Tell us more about while
while can also be executed in codes, returning the last action as the result.
The full form of while is as follows:
=: while(preaction: ; condition: ; limit: ; action: ; postaction: ; iterator: )
preaction – actions that are executed once before all conditions and subsequent actions.
condition — conditions.
limit — the limit of operations to be performed — we will explain this in more detail now.
action — actions that are executed in a loop while condition evaluates to true.
postaction — actions that are executed once when condition evaluates to false or the limit is reached. But only if at least one action was executed.
iterator — the name of the variable to which the iteration number is assigned. The numbers start from 0.
In general, it is more efficient to use listReplace in totum for iterating over lists. Let's consider the following example:
=: while(preaction: $zero; limit: 30; action: $set; iterator: "iter")
zero: var(name: "count"; value: $#lc)
set: var(name: "count"; value: $plus)
plus: listAdd(list: $#count; item: $i)
i: dateAdd(date: $#nd; days: $days)
days: $#iter * -1
Here we calculate a list of dates from the current date going back 30 days.
How it works: while starts executing and sees preaction, which needs to be executed before all conditions and actions.
In this preaction, we create a variable with an empty list.
Next, there is a limit equal to 30 — meaning we need to repeat the action 30 times. The iteration number will be assigned to the variable "iter" — as specified in the iterator parameter.
The action starts executing, it calls the variable count for writing and records in it the updated list from the list that it already contained and the new date.
The date is calculated by the method — the current date minus the number of days according to the iteration number. On the first cycle, minus 0 days, on the second minus 1 day, and so on for 30 times.
When the limit is reached, the function will return the last executed action — this will be a list of 30 date values.
Let's supplement this example with a condition – stop if we reach the first day of the month when counting back:
=: while(preaction: $zero; condition: $dateformat != 1; limit: 30; action: $set; iterator: "iter")
zero: var(name: "count"; value: $#lc)
dateformat: dateFormat(date: $i; format: "j")
// j — this is the day of the month without a leading 0
set: var(name: "count"; value: $plus)
plus: listAdd(list: $#count; item: $i)
i: dateAdd(date: $#nd; days: $days)
days: $#iter * -1
while will execute iteration after iteration until the limit is reached or until the condition evaluates to false. As soon as the date being added to the list has a day equal to 1, the loop will stop.
Note that the first day of the month will not be included in the final result. To include it, we need to complicate the conditions in condition:
=: while(preaction: $zero; condition: $dateformat != $lastday; limit: 30; action: $set; iterator: "iter")
zero: var(name: "count"; value: $#lc)
dateformat: dateFormat(date: $i; format: "j")
~lastdaynumber: dateFormat(date: $lastday; format: "j")
lastday: dateAdd(date: $firstday; days: -1)
firstday: dateFormat(date: $#nd; format: "Y-m-01")
// j — this is the day of the month without a leading 0
set: var(name: "count"; value: $plus)
plus: listAdd(list: $#count; item: $i)
i: dateAdd(date: $#nd; days: $days)
days: $#iter * -1
We get the number of the last day of the previous month by adding minus 1 day to the first day of the current month. And since we need to do this only once — we fix it with ~.
If you place the tilde anywhere else in this code (except zero) — it will break!
What value does while return in the codes?
The value of the last action or postaction, if they exist.
Why is it better that everything that can be done through listReplace should be done through listReplace?
listReplace is significantly easier to read in the related code.
What listReplace cannot do is stop based on a condition; it will always go to the end of the list.