Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for lambdas. #498

Merged
merged 2 commits into from
Aug 23, 2024
Merged

Add documentation for lambdas. #498

merged 2 commits into from
Aug 23, 2024

Conversation

floitsch
Copy link
Member

No description provided.

@floitsch floitsch requested a review from kasperl August 23, 2024 06:36
Copy link
Contributor

@kasperl kasperl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.


```
## Introduction to Blocks and Lambdas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Introduction to Blocks and Lambdas
## Introduction to blocks and lambdas

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

[1, 2, 3].do: printer.call it
```

## Key Differences
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Key Differences
## Key differences

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

4. **Flexibility**: Lambdas can be stored in variables, fields, and collections, and returned from functions. Blocks cannot.
5. **Efficiency**: Blocks are significantly more efficient than lambdas.

## Blocks in Detail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Blocks in Detail
## Blocks in detail

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


## Blocks in Detail

### Efficiency of Blocks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Efficiency of Blocks
### Efficiency of blocks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


- [Lambdas](../tasks) cannot capture blocks.
## Lambdas in Detail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Lambdas in Detail
## Lambdas in detail

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

callbacks_.do: it.call new-value

add-printer box/WatchedBox:
box.watch:: print "Was changed to $it."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
box.watch:: print "Was changed to $it."
box.watch:: print "was changed to $it."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would drop the trailing period here (and in the comments). It just seems misplaced and we don't typically have that in debug prints.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

This example demonstrates how lambdas can be stored in a list
(`callbacks_`) and called later.

### Lambda Arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Lambda Arguments
### Lambda arguments

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

main:
// Both of these will print 42.
my-function:: print it
my-function: print it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels more logical to start with the block-variant call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

- Consider providing both block and lambda versions if you need to support both
use cases. The lambda version can always redirect to the block version:
```
my-function callback:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe flip the order of these two functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

A block can return a value each time it is run. This is used for example in the `filter` method on lists.
```
// Returns a function that adds n to its argument.
add-n n:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add-n n:
add-n n -> Lambda:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@floitsch floitsch enabled auto-merge (squash) August 23, 2024 07:34
@floitsch floitsch merged commit 4ee3bf8 into master Aug 23, 2024
4 checks passed
@floitsch floitsch deleted the floitsch/block-closure branch August 23, 2024 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants