Arc Forumnew | comments | leaders | submitlogin
Rubylike slice for strings and lists (dzone.com)
5 points by mdemare 6133 days ago | 2 comments


2 points by mdemare 6133 days ago | link

I've also written my first macro!

    (mac ! (a b . body) (cons b (cons a body)))
It allows you to use a OO-syntax, with the receiver first, and the function name second:

    (! "0123test" slice 1)
I've aliased my slice function as "@", so now I can write:

    (! "hello world!!!" @ 0 -3)  ; => "hello world"

-----

4 points by babo 6133 days ago | link

This patch against arc.arc gives you similar functionality:

  (def subseq (seq start (o end (len seq)))
  -  (if (isa seq 'string)
  -      (let s2 (newstring (- end start))
  -        (for i 0 (- end start 1)
  -          (= (s2 i) (seq (+ start i))))
  -        s2)
  -      (firstn (- end start) (nthcdr start seq))))
  +  (with (
  +    start (if (< start 0) (+ (len seq) start) start)
  +    end (if (< end 0) (+ (len seq) end) end)) 
  +    (if (isa seq 'string)
  +        (let s2 (newstring (- end start))
  +          (for i 0 (- end start 1)
  +            (= (s2 i) (seq (+ start i))))
  +          s2)
  +        (firstn (- end start) (nthcdr start seq)))))

  (subseq "hello" 0 -1) => "hell"
  (subseq "hello world" -5) => "world"
  (subseq "hello world" -4 -2" => "or"
Interpreting the second parameter as an offset if it's greater than 0 probably much more useful, I'll try it out later today.

-----