2 ms·
The problem with first class functions in a shell is that the main duty of a shell is to fork processes. Once the shell is forked, the only communication avail
by injinj 7y ago
The problem with first class functions in a shell is that the main duty of a shell is to fork processes. Once the shell is forked, the only communication available are inter process. All of the environment is no longer shared.
For example, in es:
; let (i = 0) { fn hello {echo $i; i = <={%sum $i 1}}}
; hello
0
; hello
1
The closure variable i is incremented after every invocation of hello, but if hello in a subprocess, then i doesn't change:
; hello | wc -c
2
; hello
2
This is because hello is a subprocess of the main shell when it is in a pipeline. When it is used without the pipe, then it is executed within the shell environment, not a child.
I have been using a fork of es since Dec, mine has the %sum builtin above, along with some other things: https://github.com/injinj/desh https://github.com/injinj/desh