red / REP

Red Enhancement Process
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

WISH: add backwards paths #110

Open Dry-Leaf opened 2 years ago

Dry-Leaf commented 2 years ago

Using black slashes to access items starting from the tail of a series.

>> f: [1 2 3]
>> f\1
3
>> f\3
1
>> f\1 : f\1 + 1
>> f
[1 2 4]
>> f: [a: 1 a: 2]
>> f\a
2
>> f: [[1 0] 2 3]
>> f\3/1
1
>> f/1\1
0
hiiamboris commented 2 years ago

For context: this idea was born out of discussion, basically about how ugly Red becomes when one needs to access last items in a list.

Example: change back back tail list 1 + pick tail list -2 in Red vs list[-2] += 1 in Python

This area clearly needs some improvement, and this is one of the options for further consideration/development.

greggirwin commented 2 years ago

It's a clever idea, but raises questions. Is \x a new kind of refinement!? Is \1 the negation of /1? Was \ avoided by Carl in Rebol's design because it's such a common escape char in other langs? For both that reason and reading paths, how confusing will it be, and will it lead to subtle errors?

For this particular wish and its syntax, my gut says No. For the general idea of some way to make last-item access clearer, that's a good idea. Let's list example use cases, think about incr, and brainstorm from there.

loziniak commented 2 years ago

I see a disadvantage of \ being visually very similar to /, could be difficult to read code properly.

Dry-Leaf commented 2 years ago

negative index is relative to the head of a series, not its tail that works, but first you need to set the head at the tail

qtxie commented 2 years ago

@Dry-Leaf Ah, yes. Forgot that. 😅

rebolek commented 2 years ago

I’m strongly against it. How common is it to access paths from the end? Do we really need to waste one of the few remaining sigils on it? @greggirwin asks good questions, is it going to be a new type? And as negative indexes are relative to the head of the series, you need just to do list: tail list to use them. It’s not that hard.

I’ll better save \ for something else.