Backslash escaping operator sections
sullyj3 opened this issue · comments
Would it be possible to allow something like:
>>> [1,2,3,4,5] \(+ 1) map
[2,3,4,5,6]
This seems much nicer than the current way to do it:
[1,2,3,4,5] {(+ 1)} map
As well as being more consistent.
Good idea. I think it’s safe to relax \
to work on any term, so \term
should always be equivalent to { term }
. A bit strangely, it would make \(foo bar baz)
perfectly legal syntax for { foo bar baz }
, but I think that’s acceptable, because people would prefer the latter anyway.
One minor problem with this IIRC is that Emacs treats \(
as matching \)
, not )
, because of some regexp dialects. So we’d have to account for that in kitten-mode
. (Editor integrations need a lot of love anyway.)
Is the difference b/c it would otherwise have to be written, { 1 (+) }
? Also, is \( + 2 + 1 )
valid?
Operator sections are sugar to avoid some stack operations:
(+ 1)
→1 (+)
(1 +)
→1 swap (+)
This change would just make e.g. \(+ 1)
valid and equivalent to { (+ 1) }
, because \foo
is already valid and equivalent to { foo }
.
(+ 2 + 1)
is currently valid, and equivalent to (+ (2 + 1))
or (+ 3)
, but that’s kind of a bug because it can lead to misleading precedence.
I've given it some thought, but I don't see the common pattern.
(+ 1) → 1 (+)
(1 +) → 1 swap (+)
What's the rule of translation? How does the swap
come into it?
Operator sections are just partially applied operators. For some operator @
:
(a @ b)
is fully applied, and desugared intoa b (@)
.(@ b)
is desugared intob (@)
because(@ b)
=-> x; x @ b
=-> x; x b (@)
=b (@)
(a @)
is desugared intoa swap (@)
because(a @)
=-> x; a @ x
=-> x; a x (@)
=-> x; x a swap (@)
=a swap (@)
So while (+ 1)
is a function that adds 1 to its argument, { (+ 1) }
(or, with this proposal, \(+ 1)
) is a function that pushes that function to the stack.