oftenpaper.net

Coroutines in Arc

One of the nice things about Lisp is that you can color the parentheses pink, which looks pretty:

  1. ; simple iterator
    
    (= f (co
        (let x 0
          (while t
              (yield (++ x))))))
    
    
    (f) ;returns 1
    (f) ;returns 2
    (f) ;returns 3
            
  2. ;passing yield into a tree-traversal function
    
    (= f (co
        (co:treewise
         (fn (a b)) ;empty function given as the join function    
         yield   ;yield given as the leaf function
         '(a . ((b . c) . d)))))
    
    (f) ;returns 'a
    (f) ;returns 'b
    (f) ;returns 'c
    (f) ;returns 'd
    (f) ;returns nil
    
  3. ;passing values into the coroutine
    
    (= x 1)
    (= f (co
        (while t
            (if (= in (yield x)) ;if the call supplied an argument
                (do (= x in) ;reassign x and print message
                    (prn (string "you gave me: " x)))))))
    
    (f) ;returns 1
    (f) ;returns 1
    (f 0) ;prints "you gave me: 0" and returns 0
    (f) ;returns 0
    (f) ;returns 0
    (f 'hello) ;prints "you gave me: hello" and returns 'hello
    (f) ;returns 'hello
    
  4. (mac co body
      (w/uniq (con1 con2 argl)
    
        `(withs (,con1 nil ,con2 nil
                 yield (fn ((o val nil))
                          (ccc [do (= ,con2 _) (,con1 val)])))
    
          (def ,con2 ,argl
              ;(while t ;make generation cyclic
              ,@body
              ;)
              (def ,con2 args nil)
              (,con1 nil))
    
          (fn args
            (ccc [do (= ,con1 _) (apply ,con2 (or args (list nil)))]))
    )))
    

Speaking of pink, just the other day I saw this awesome Hello Kitty folder at Target, and even though she didn't ask, I let the cashier know that I was buying it for myself. That's how secure I am in my manliness.

On a serious note, the design of Arc is very pretty on many levels. My own language that I'm currently designing has a similar kind of aesthetic, although it's a completely different language.