Arc Forumnew | comments | leaders | submitlogin
4 points by rkts 5932 days ago | link | parent

It seems wrong to allow a negative step if the direction is inferred. How about this:

  (def unfold (f init)
    (aif (f init)
      (cons (car it) (unfold f (cdr it)))
      nil))

  (def range-up (start end (o step 1))
    (unfold [if (<= _ end) (cons _ (+ _ step))] start))

  (def range-down (start end (o step 1))
    (unfold [if (>= _ end) (cons _ (- _ step))] start))

  (def range (start end (o step 1))
    ((if (< start end) range-up range-down) start end step))
Example:

  arc> (range 1 100 10)
  (1 11 21 31 41 51 61 71 81 91)

  arc> (range 1 -100 10)
  (1 -9 -19 -29 -39 -49 -59 -69 -79 -89 -99)