# \-\> for Lightweight Lambda ("Arrow Functions")

**URL:** <https://revolt.metaeducation.com/forum/t/for-lightweight-lambda-arrow-functions/2172>\
**Category:** Functions\
**Created:** [March 10, 2024, 3:21pm UTC](https://revolt.metaeducation.com/forum/t/for-lightweight-lambda-arrow-functions/2172 "2024-03-10T15:21:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![hostilefork](https://revolt.metaeducation.com/forum/user_avatar/revolt.metaeducation.com/hostilefork/32/421_2.png) [@hostilefork](https://revolt.metaeducation.com/forum/u/hostilefork)\
**Post date:** [March 10, 2024, 7:02pm UTC](https://revolt.metaeducation.com/forum/t/for-lightweight-lambda-arrow-functions/2172/2 "2024-03-10T19:02:47Z")

</div>

> [@hostilefork](#):
>
> ```plaintext
> y: case [
> 1 > 2 [<no>]
> 1 < 2 [<yes>]
> ] then (lambda [x] [
> assert [x = <yes>]
> 1000 + 20
> ])
> assert [y = 1020]
> 
> ```

I remembered while writing this that you have to put branches in GROUP!s, because branches are "soft-quoted" slots (or, more accurately "soft literal") slots.

The reason you don't have to put the **`->`** form in groups when you use them as branches is because there's deference to a leftward-literal operation in a soft-literal slot. The left literal beats the right literal.

But this has a cost: _you're paying to generate the function whether it gets called or not_.

Historical Rebol has this problem anywhere functions are passed as handlers. Consider TRY/EXCEPT in R3-Alpha:

```
r3-alpha>> try/except [1 / 0] func [e] [print ["Error Handler:" form e/id]]  
Error Handler: zero-divide

```

Putting it in a GROUP! won't help there, because /EXCEPT doesn't suppress any evaluation from that GROUP!. But in Revolt, branches are literal, so the groups can be used to suppress the evaluation unless the branch runs.

## Is Making Uncalled (Unpacking-) Lambdas A Problem?

That depends...

1. does an unpacking lambda make a deep copy of the block?

2. or does it just make a small structure that pairs the name of the parameter(s) with the body of code?

Today's it's [2]... **`->`** just makes that small structure (while LAMBDA makes the full copy). But this means you get the semantics of a non-copying construct:

```
>> block: [print ["Hello" x]]

>> one: x -> block

>> append block spread [print ["Goodbye" x]]

>> two: x -> block

>> one 1020
Hello 1020
Goodbye 1020

>> two 1020
Hello 1020
Goodbye 1020

```

So you would have to say **`x -> copy/deep block`** to get a unique copy of the body.

Beyond the semantic implications of not copying, there's a performance implication if you call it more than once... because the [preparations that make the body a little faster to call](https://revolt.metaeducation.com/forum/t/semantics-and-optimization-of-copying-function-bodies/2119) that happen during the copy are not done.

There are some other options like going in some strange "branch dialected" direction, and say that BLOCK!s with a certain format were "parameterized", moving the parameter name into the block somehow, not necessarily this but like this:

```
y: case [
    1 > 2 [<no>]
    1 < 2 [<yes>]
] then [x ->
    assert [x = <yes>]
    1000 + 20
]

```

I'd rather put every parameterized branch in a group than do that.

```
y: case [
    1 > 2 [<no>]
    1 < 2 [<yes>]
] then (x -> [
    assert [x = <yes>]
    1000 + 20
])

```

But still, no. It's much better to push on optimizations of the stylized function generation so that it's cheap as can be to make regardless of the branch being taken, _it's the source we want to write_:

```
y: case [
    1 > 2 [<no>]
    1 < 2 [<yes>]
] then x -> [
    assert [x = <yes>]
    1000 + 20
]

```

Just another devil in the details to worry over. But wanted to write up a reminder of why plain LAMBDA has to be in a GROUP! if you're going to use it in a branch, because I'd forgotten you had to do that.

---

_[View the full topic](https://revolt.metaeducation.com/forum/t/for-lightweight-lambda-arrow-functions/2172)._
