3 ms·
It would be nice if put 1 2 3 4 5 | filter {|x| > $x 2} | map {|x| * 2 $x} could be written just as put 1 2 3 4 5 | filter > $_ 2 | map * 2 $_
by zokier 12y ago
It would be nice if
put 1 2 3 4 5 | filter {|x| > $x 2} | map {|x| * 2 $x}
could be written just as
put 1 2 3 4 5 | filter > $_ 2 | map * 2 $_
- tinco 12y agocan you express that grammar in ebnf? I think the parser would need to be at least ll(C) where C is bigger than 1. Would the presence of $_ denote a lambda? how would nested lambda's work? Why not: put 1 2 3 4 5 | filter -> > $1 2 | map -> * 2 $1 I think that would be easier to parse, and not much typing overhead, also $_ is a weird thing.
- Dewie 12y agoSome languages have a sort-of default variable name for cases like this, namely "it": > put 1 2 3 4 5 | filter > $it 2 | map * 2 $it
- JadeNB 12y agoAs zokier was presumably pointing out, other languages have a different sort-of-default variable name, namely `$_` (http://c2.com/cgi/wiki?DollarUnderscore http://c2.com/cgi/wiki?DollarUnderscore). :-)
- e12e 12y agoThis is equivalent to the following bash incantation?: cat <<eof\ | while read a;do if [[ $a -gt 2 ]];then echo $a;fi; done \ |while read b;do echo $((b*2));done 1 2 3 4 5 eof (Better expressed with seq): seq 5 \ | while read a;do if [[ $a -gt 2 ]];then echo $a;fi; done \ | while read b;do echo $((b*2));done But we can do (a little) better: # Ok, this is a bit odd, the filter is actually inverse... filter() { while read it do #NOT SAFE!! if eval ${*} then echo "${it}" fi done } map() { while read it do #NOT SAFE!! eval ${*} echo "${it}" done } #Valid bash: seq 5 \ | filter test '$it' -gt 2 \ | map 'it=$((it*2))' Sorry, didn't mean to turn this into stack overflow, the "simple" syntax just tickled me...