Arc Forumnew | comments | leaders | submitlogin
Python-like generators in arc
8 points by applepie 6156 days ago | 8 comments

  (mac lcc (name . body)
    `(ccc (fn (,name) ,@body)))

  (mac defgen (name args . body)
    (w/uniq (gen ret)
      `(def ,name ,args
          (def ,gen ()
            (lcc ,ret
              (def yield (value)
                (lcc next
                   (= ,gen (fn () (next #f)))
                   (,ret value)))
              ,@body))
          (fn () (,gen)))))

  ;; Example

  (mac iterate (names-values . body)
    `((afn ,names-values ,@body) ,@names-values))
          
  (defgen fib (x0 x1)
    (yield x0)
    (iterate (x0 x1)
      (yield x1)
      (self x1 (+ x0 x1))))

  (= genfibs (fib 0 1))

  (repeat 10
    (prn (genfibs)))


5 points by CatDancer 6156 days ago | link

Do you mean to (re)define a global "yield" function on each call to defgen? In Arc, an internal def doesn't work like an internal Scheme define... it always sets the global top-level variable named as its first argument, it doesn't create a local binding as an fn or let would. (I couldn't tell if that's what you wanted to do or not).

-----

2 points by applepie 6155 days ago | link

Ugh, the code was wrong. I'm starting to think that Arc didn't really learn the lesson (i.e. getting scope right).

IMHO, it would be cleaner if def behaved more like Scheme's internal define (i.e. create a new local binding and set!).

I'd keep the current behaviour of def under the name defglobal or something.

-----

3 points by nex3 6155 days ago | link

I added an "lset" form to Anarki that works just like Scheme's define.

-----

1 point by almkglor 6156 days ago | link

  (from "arc.arc")
  [mac] (point name . body)
   Creates a form which may be exited by calling `name' from within `body'. 

  (mac point (name . body)
    " Creates a form which may be exited by calling `name' from within `body'. "
    (w/uniq g
      `(ccc (fn (,g)
              (let ,name [,g _]
                ,@body)))))

-----

1 point by applepie 6156 days ago | link

I don't understand how it relates to generators (?)

-----

1 point by almkglor 6155 days ago | link

  (mac lcc (name . body)
    `(ccc (fn (,name) ,@body)))
Please use Arc built-ins as much as possible instead of defining your own.

Come to think of it, pg should really make the docstrings mod official, and he most definitely needs to put docstrings in his own code.

-----

2 points by applepie 6155 days ago | link

Why?

I'm programming for myself, not for you. You can use, modify, improve or drop the code if you find it useful, but don't tell me what I should and what I shouldn't do.

-----

3 points by kennytilton 6155 days ago | link

I think the suggestion was just phrased a little awkwardly.

Lisp is so easy to program that it is commonplace to reinvent builtins -- they are as easy to write as they are to find (especially in an, er, mildly documented language).

I went proactive on reinvention thang and invited people to let me know of things I had reinvented. Somewhat tangentially, I also make an effort to use things like [... _ ...] and func.arg to give pg's ideas a fair test and so my Arc code can help other noobs get up to speed on the language. If I stay as close as I can to my preferred Common Lisp, like someone writing C in Common Lisp neither I nor anyone else gets a feel for Arc. But I digress.

-----