On Wed, Aug 8, 2012 at 11:48 AM, Brian Marick <mar...@exampler.com> wrote: > I'm looking for medium-scale examples of using function-generating functions. > > Such examples might be ones that ... use closures to avoid the need to have > some particular argument passed from function to function (which looks like > the `this` in an instance method).
I try and use a greedy parser combinator as the next jump, and as example of hiding arguments. String parsing is a small, yet non-trivial example, that doesn't require domain knowledge. Something like: (defn result [value] (fn [string] [value string])) (defn pred [predicate] (fn [string] (if (predicate (first string)) [(first string) (rest string)]))) (defn orp [f g] (fn [string] (or (f string) (g string)))) (defn bind [parser f] (fn [string] (if-let [[result s2] (parser string)] ((f result) s2)))) (defn many [parser] (let [f (bind parser (fn [h] (bind (many parser) (fn [rst] (result (cons h rst))))))] (orp f (result [])))) (def letter (pred #(if % (Character/isLetter %)))) (def word (bind (many letter) (fn [w] (result (apply str w))))) (word "foo") ;=> ["foo" ()] The closest I see to an implicit this is: ((bind word (fn [w1] (bind (pred #(= % \space)) (fn [_] (bind word (fn [w2] (result [w1 w2]))))))) "foo bar baz") ;=> [["foo" "bar"] (\space \b \a \z)] Here word and the space predicate are called on the string, but its only ever mentioned as the argument. However, it is kinda ugly without a macro to hide all the bind/fn pairs. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en