Arc Forumnew | comments | leaders | submitlogin
3 points by fallintothis 6175 days ago | link | parent

range is useful, though I kept making the goof-up of trying to pass only one arg as in Python. So, I went ahead and changed it:

  (let orig range
    (def range (x (o y))
      (if y
          (orig x y)
          (orig 0 x)))) ;could be (orig 0 (- x 1)) to be even more Python-like
Come to think of it, you could instead do something like this to be the most like Python (whether or not that's a good thing):

  (def range (start (o end) (o step 1))
    (let test (if (positive step) >= <=)
      (when (no end)
        (= end start 
           start 0))
      (if (test start end)
          nil
          (cons start (range (+ start step) end step)))))

  arc> (range 10)
  (0 1 2 3 4 5 6 7 8 9)
  arc> (range 10 1)
  nil
  arc> (range 1 10)
  (1 2 3 4 5 6 7 8 9)
  arc> (range 1 10 2)
  (1 3 5 7 9)
  arc> (range 1 10 -2)
  nil
  arc> (range 10 1 -2)
  (10 8 6 4 2)
  arc> (range 10 1 -1)
  (10 9 8 7 6 5 4 3 2)
This same idea of having the "step" parameter maps nicely into subseq syntax in Python, as I noted in this thread: http://www.arclanguage.org/item?id=479


1 point by icemaze 6174 days ago | link

Everybody seems to be in love with the step parameter. Would it be such an improvement over (for instance):

  (reverse (range 10)), or
  (map [* 2 _] (range 5)) ?
Do python guys use it that often?

-----

1 point by Xichekolas 6174 days ago | link

Yeah I'd much rather compose functions than have extra parameters to remember... but I'm not a Python guy either.

-----

1 point by apgwoz 6174 days ago | link

It's pretty inefficient to use reverse, if instead you can add an extra parameter, though I guess it's often negligible.

-----