Arc Forumnew | comments | leaders | submit | oddbod's commentslogin
7 points by oddbod 6115 days ago | link | parent | on: Updated range with step

    (def range (start end (o step (if (< start end) 1 -1)))
         (if (or (and (> step 0) (~> start end))
	         (and (< step 0) (~< start end)))
	     (cons start (range (+ start step) end step))))

  arc> (range 0 2)
  (0 1 2)

  arc> (range 2 0)
  (2 1 0)

  arc> (range 0 -2)
  (0 -1 -2)

  arc> (range 0 -9 -2)
  (0 -2 -4 -6 -8)

-----

4 points by rkts 6115 days ago | link

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)

-----

1 point by bgutierrez 6115 days ago | link

Ah, I like that better.

-----

3 points by oddbod 6117 days ago | link | parent | on: Unquoting

[] could be quasiquote, $ unquote, and $$ unquote-splicing:

    arc> (with (x 1  y '(2 3))
           [a b c $x $$y])
    (a b c 1 2 3) 
[] more strongly implies 'sequence'. Lambda would become {}, which implies 'block':

    arc> (map {prn _} '(1 2 3))
    1
    2
    3

-----

1 point by nex3 6116 days ago | link

Or we could avoid the whole syntax round-robin and make quasiquote {}.

-----

1 point by oddbod 6116 days ago | link

Or <>, which implies 'template' (to a younger audience).

-----


http://haml.hamptoncatlin.com/

    arc> (tag (a #some-id .some-class))
    <a id="some-id" class="some-class"></a>

    arc> (tag (#box (tag #menu)))
    <div id="box"><div id="menu"></div></div>

    arc> (tag (#box .fancy .hmm))
    <div id="box" class="fancy hmm"></div>

-----


Tcl lrange: http://tcl.tk/man/tcl8.5/TclCmd/lrange.htm

    (subseq "hello" 0 'end)
    "hello"
    (subseq "hello" 0 -1)
    "hell"

-----