Arc Forumnew | comments | leaders | submitlogin
6 points by bogomipz 6114 days ago | link | parent

Surprisingly, nobody in this thread has suggested that lists/strings in functional position could accept two arguments. This would replace the current cut function. Additionally, negative numbers should mean index from the end, and 0 as second argument could mean "to the end". Here's Python, Ruby, arc1, and lastly my suggested semantics:

The i'th item of s

    s[i]
    s[i]
    (s i)
    (s i)
The i'th item of s from the end

    s[-i]
    s[-i]
    (s (- (len s) i))
    (s (- i))
The first x items of s

    s[:x]
    s[0,x]
    (cut s 0 x)
    (s 0 x)
The last x items of s

    s[-x:]
    s[-x..-1]
    (cut s (- (len s) x))
    (s (- x) 0)
The items from position i to the end

    s[i:]
    s[i .. -1]
    (cut s i)
    (s i 0)
The items from position i to position j (inclusive)

    s[i:j+1]
    s[i .. j]
    (cut s i (+ 1 j))
    (s i (+ 1 j))
The items from position i to position j (exclusive)

    s[i:j]
    s[i ... j]
    (cut s i j)
    (s i j)
i items beginning at position j.

    s[j:j+i]
    s[j,i]
    (cut s j (+ i j))
    (s j (+ i j))
The items from position i to the end minus the last j items

    s[i:-j]
    s[i ... -j]
    (cut s i (- -1 j))
    (s i (- j))
i items beginning at the j'th position from the end

    s[-j:-j+i]
    s[-j,i]
    (cut s (- (len s) j) (+ (- (len s) j) i))
    (s (- j) (+ i (- j)))


0 points by pg 6114 days ago | link

http://arclanguage.org/item?id=2257

-----

4 points by bogomipz 6114 days ago | link

I'm honored by you replying to my comment, Paul, but that link points to this thread?

-----

5 points by pg 6114 days ago | link

Sorry, meant http://arclanguage.org/item?id=2301

-----

1 point by jc 6106 days ago | link

I second this. It seems easier to ape Python's syntax for slicing. For example, given s as "abcde,"

  s[1:4]
  (s 1 4)
gives "bcd,"

  s[1:-2]
  (s 1 (- 2))
gives "bc,"

  s[-4:]
  (s (- 4) nil)
gives "bcde," and

  s[:]
  (s nil nil)
gives "abcde."

-----