Skip to content

Instantly share code, notes, and snippets.

@amb26
Created January 25, 2022 21:00
Show Gist options
  • Select an option

  • Save amb26/14db83be4a3e13b335d26a28922cdb68 to your computer and use it in GitHub Desktop.

Select an option

Save amb26/14db83be4a3e13b335d26a28922cdb68 to your computer and use it in GitHub Desktop.
Jonathan Edwards' example from his Kent talk of reclaiming a value from the interior of a (materialised) function
Glad to see one of my pet peeves strike a chord. I've always wanted functions to have multiple return values like they have multiple arguments, but all of the solutions I've seen break every calling site when you go from one to two return values. Here is the bit I wrote in the doc:
## Associated results
The result of a code block is the final value of the subject. However it is often desirable to return extra results from a function call. For example, integral division would like to return not only the integral ratio but the fractional remainder as well. This is done as follows:
```
integral-divide ()= 1 do{
divisor: 1
numerator = $
:= numerator / divisor floor()
remainder = numerator - ($ * divisor)
}
x = 5 integral-divide(3) // =? 1
y = x~remainder // =? 2
```
The formula `x` calls the function `integral-divide` as follows
1. The subject of the code block is set to `5`
2. The divisor argument is set to `3`
3. `numerator` equals the current subject `$`, which is `5`
4. The subject is set to the integer part of `5/3`, which is `1`
5. `remainder` equals the value of the formula which is `2`
6. `x` equals the final subject value of the code block, which is `1`
7. `y` equals the value of the reference `x~remainder`, which is the `remainder` formula within the execution of `x`’s expression.
The new trick here is the `~` in the reference `x~remainder`. It is pronounced “x’s remainder”. As `x.` follows a path inside the value of `x`, the `x~` follows a path inside the formula of x.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment