rakitzis / rc

rc shell -- independent re-implementation for Unix of the Plan 9 shell (from circa 1992)
Other
250 stars 23 forks source link

join operator #105

Closed BourgeoisBear closed 3 weeks ago

BourgeoisBear commented 3 weeks ago

The default space-separated behavior of $^ is very limiting. It would be nice to have a join operator that lets us specify the glue string.

> var = (one two three)
> echo '---'JOIN$var
one---two---three

I don't have an opinion on the syntax, so whatever you can fit into the grammar.

BourgeoisBear commented 3 weeks ago

Another useful one would be an explode function to convert a string to a list with a given split string.

codemac commented 3 weeks ago

Both of these exist today, albeit not as native operators:

A join operator (lflat) is covered in the manual here: https://github.com/rakitzis/rc/blob/43a03c298b42486f6c5833cd72cfae3bdca614a9/rc.1#L1972

fn lflat {
  lflat=$*; *=$$1
  while () {
    echo -n $1; shift
    ~ $#* 0 && break
    echo -n $lflat(2)
  }
}

And explode is just using the ` syntax for setting$ifs`:

; echo `` jj { echo 'ajjbjjc' }
a b c
; arr=(`` jj { echo 'ajjbjjc' })
; echo $#arr
3
;

This is documented in the manpage here: https://github.com/rakitzis/rc/blob/43a03c298b42486f6c5833cd72cfae3bdca614a9/rc.1#L1218

BourgeoisBear commented 3 weeks ago

The explode is a decent one-liner, but having to type or import your own lflat every time seems like batteries that should have been included. Not pushing for it; just a suggestion.

Not being able to slice lists also seems like an oversight. When I googled "rc slice syntax", I discovered this guy is working on an rc clone, and added a slice syntax to it while he was at it:

https://drewdevault.com/2023/04/18/2023-04-18-A-new-shell-for-Unix.html

Tangentially related: is the source script command (.) in the manpage? It seems to work, but I can't find an entry for it.

Another tangent: es shell syntax is near-identical, but with additions. Is rakitzis/rc maintained primarily for historical purposes (with es being the true heir), or is it more of a schism between two active user bases?

codemac commented 3 weeks ago

rc has a tiny user base. Byron's rc has an even smaller one. es has maybe the smallest. I've only heard of a single person who uses es daily. It is not historical however, it is just small relative to something abhorrent like bash. I use Byron's rc every day, as does Byron :) Glad for Drew Devault to implement their own rc in Hare, writing your own shell is something everyone should do once in their life.

Note that not even bash has a standard "join" operator like lflat, just an abuse of various environment variables along with ${arr[*]}, which even then can't do multiple letters - like jj above.

In regards to "slicing" arrays, usually I'm removing the first or last element in a list, and I just use the $myarr(`{seq 2 $#myarr}) syntax. it's extremely uncommon for me to need to do though, so the ergonomics don't really matter to me.

codemac commented 3 weeks ago

Oh, and the source command is the first command listed under BUILTIN COMMANDS in the manpage:

https://github.com/rakitzis/rc/blob/43a03c298b42486f6c5833cd72cfae3bdca614a9/rc.1#L1627

BourgeoisBear commented 3 weeks ago

So even after writing es, he prefers the classic? I started digging into both of them yesterday. After looking at things like fish and nushell, I thought to myself, "No, no, no... time to go in the opposite direction.", and here I am.

codemac commented 3 weeks ago

@rakitzis can answer questions about the choice between es and rc for himself.

my personal experience is that instead of using es I'd rather using something like guile or scsh, and get even more functional/lisp-y.

rc is amazing for day to day ergonomics along with a syntax and consistency that makes sense. It's parsing of single quotes alone is enough to keep me here forever.

rakitzis commented 3 weeks ago

Hi, as you have probably seen, rc is old and stable at this point. I don't think we should change the list processing primitives.

As for es, it was a fork of this project way back and Paul and I are friends to this day. Maybe you would prefer using it, you should try it.

BourgeoisBear commented 3 weeks ago

I did! The additions didn't seem like things I would use, but just about everyone has had the experience of needing the widget right after throwing it away. Mostly checking that rc hasn't been abandoned before I start porting scripts to it. Thank you both for taking time to answer.