Arc Forumnew | comments | leaders | submitlogin
1 point by rincewind 5931 days ago | link | parent

I used this in my m-expr reader, as a generalisation of do1:

  (mac w/res body
   " Define the result of this expression by calling the 'result' (captured!) 
     function inside. Calling result does not change the control flow, like in
     Delphi/Pascal."
     (w/uniq return
        `(withs (,return nil result [= ,return _])
                 ,@body 
                 ,return)))

  (def call-reader-w/delimiters (port start end fun)
   " Reads from port with fun, ensures that given delimiters are read before and 
     after, otherwise an error is raised"
     (w/res (expect port start 'delim)
            (result (fun port))
            (expect port end 'delim)))
Your odd-number example could be written:

  (best (fn (a b) (and odd.a (> a b))) *list-of-numbers*)
without an intermediate list


1 point by drcode 5930 days ago | link

Yup, looks like 'w/res is very close to 'focus.

Your 'best alternative for my example is extremely clever, but doesn't work:

  > (= *list-of-numbers* (6 2 4 5))
  > (best (fn (a b) (and odd.a (> a b))) *list-of-numbers*)

  6
I knew something was wrong with it, but it took me like half an hour to realize the flaw :)

...it can of course be fixed with extra flagging (well, it'll still fail on only lists of even numbers by not returning nil) but then you'll lose the brevity advantage over my solution.

-----

1 point by rincewind 5930 days ago | link

sorry, I hadn't had access to an Arc then.

  (best (fn (a b) (and odd.a (or even.b (> a b)))) '(6 2 4 5))
as mexpr:

  best[[a;b]-> odd[a] and (even[b] or a > b); list[6;2;4;5]];

-----

1 point by drcode 5930 days ago | link

yes, still a pretty good solution.

-----