question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better notation to re-use `&`?

See original GitHub issue

Currently, if you want a quick function that uses the input twice, e.g. ($) => $+$, you can write &+$. I find this syntactically inappropriate, as $ doesn’t appear until you use it the second time; I don’t find it at all intuitive that & binds $. And it’s especially bad if you use $ yourself or nest & functions inside of each other.

I would rather find a notation that is similar in the first and second uses, or that doesn’t use an existing identifier. Obviously &+& has a meaning though, namely, ($) => $ + ($$) => $$. And it could get more ambiguous with e.g. &(&).

Some possible proposals (which I think I mentioned previously in Discord):

  1. A second & refers to the same variable as the first, so &+& would mean ($) => $+$. This would forbid nesting a & function shorthand inside another one, but we can use alternatives to just &. For example, if you want the current meaning of &+&, you could use something like &+&& or &+&2 or &1+&2.
  2. Almost the opposite: && or &1 could mean “re-use the last & variable”, similar to how $ behaves now. We could extend to &&& or &2 referring to two levels up, etc.
  3. Similar to 2, we could use a special notation for the current & argument, like ^, or ^^ for two levels up etc. Or &^, &^^, etc.

It occurs to me that && is probably syntactically ambiguous with the and operator, so the above suggestions are only really serious with the &n notation for an integer n, or something else like &^.

FWIW, Ruby, Crystal, and Elm don’t seem to have anything for this. So another option (other than leaving things as is) would be:

  1. Forbid accessing the argument a second time. &+$ would become ($$) => $$ + $. If you want ($) => $+$, write that.

Also possibly related to pipe operator and https://github.com/DanielXMoore/Civet/issues/75#issuecomment-1365340146

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
STRd6commented, Dec 26, 2022

I think forbidding access to the function variable is probably correct. It just sort of happens to work by chance of implementation currently.

I think in cases where you need to reference the parameter using an explicit arrow function is the simplest and most direct way to do it.

0reactions
edemainecommented, Dec 27, 2022

Huh, I was wondering about wrapping & expressions (or some other notation) in braces to indicate where the function wrapper goes (especially for placeholders). But yeah, not much more concise than an arrow function. I guess the nice thing is not having to spend time naming the arguments and not having to repeat them more than once… So I think it still might be interesting to think about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I re-use already introduced notation differently?
I would advise against it : You have so many possibilities for a clear notation, why make it harder for someone who tries...
Read more >
Write It Tight: Tips for Writing More Successful Notation
Write It Tight: Tips for Writing More Successful Notation · Proofreading · Double-Check for Errors · Spelling and Rhythm · Know Your Range...
Read more >
Reusing metamodels and notation with Diagram Definition
2. Reusing the same visual appearance for similar concepts in multiple languages. This is useful when multiple languages about the same topics ...
Read more >
Dragoon: an object-oriented notation supporting the reuse ...
increasingly important areas: reuse and distribution. The first is essential for reducing development costs and improving general software quality, ...
Read more >
Reusable DRD in Decision Modem and Notation DMN
Reusable DRD - Decision Requirement Diagram in DMN allows sharing a DRD across multiple decision models. This is called Sub DRD, reusable component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found