Re: Attractive examples of function-generating functions

2012-08-16 Thread Roberto Mannai
typo, I meant ring *middleware*, not ring handlers

On Thu, Aug 16, 2012 at 1:01 PM, Roberto Mannai  wrote:

>
> So if you are searching for practical examples I'd suggest to look for
> such use cases, although their best application IMHO has to be found not in
> business code but in utility libraries/frameworks, such as ring handlers.
>
> [...]
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-16 Thread Roberto Mannai
I don't have my own examples, anyway being a daily object-oriented
programmer I'm feeling compelled to say that where "function-generating
functions" prove their main practical power is in what I'd label "man in
the middle functions".

Whenever I need to add behaviour to an existing OO method I have to
subclass or introduce an indirection by interface, applying a decorator
pattern et similia, or to apply some aspect oriented programming. Instead
in Clojure you can more easily "replace" that function by an hooked-one (
https://github.com/technomancy/robert-hooke/) or by hand-written wrapping
functions (http://vimeo.com/channels/fulldisclojure/38507385).

If a Java programmer find a little weird the typical decorator usage of the
java.io.InputStream hierarchy (based on class inheritance) to the point
it's been proposed a different hierarchy designed on interfaces (see the
PragPub "Interface Oriented Design" book), I guess that the possibility of
writing compoundable chains of functions can further improve the general
design of what we write day-by-day.

So if you are searching for practical examples I'd suggest to look for such
use cases, although their best application IMHO has to be found not in
business code but in utility libraries/frameworks, such as ring handlers.

On Wed, Aug 8, 2012 at 6:48 PM, Brian Marick  wrote:

> I'm looking for medium-scale examples of using function-generating
> functions. I'm doing it because examples like this:
>
> (def make-incrementer
>  (fn [increment]
>(fn [x] (+ increment x
>
> ... or this:
>
> (def incish (partial map + [100 200 300]))
>
> ... show the mechanics, but I'm looking for examples that would resonate
> more with an object-oriented programmer. Such examples might be ones that
> close over a number of values (which looks more like an object), or
> generate multiple functions that all close over a shared value (which looks
> more like an object), or 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).
>
> Note: please put the flamethrower down. I'm not saying that "looking like
> objects" is the point of higher-order functions.
>
> I'll give full credit.
>
> -
> Brian Marick, Artisanal Labrador
> Contract programming in Ruby and Clojure
> Occasional consulting on Agile
>
>
> --
> 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
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-12 Thread Malcolm Sparks


> On Wednesday, August 8, 2012 7:48:23 PM UTC+3, Brian Marick wrote:
>>>
>>> I'm looking for medium-scale examples of using function-generating 
>>> functions.
>>>
>>>
>>>
Brian, when I saw this I was reminded of ring middleware - eg. 
http://jgre.org/2010/10/04/ring-middleware/

-- 
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

Re: Attractive examples of function-generating functions

2012-08-10 Thread Brian Rowe
That's a good call. +1

On Friday, August 10, 2012 8:36:25 AM UTC-4, Jonas wrote:
>
> How about the new reducers library:
>
>
> http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html
> http://clojure.com/blog/2012/05/15/anatomy-of-reducer.html
>
> Jonas
>
> On Wednesday, August 8, 2012 7:48:23 PM UTC+3, Brian Marick wrote:
>>
>> I'm looking for medium-scale examples of using function-generating 
>> functions. I'm doing it because examples like this: 
>>
>> (def make-incrementer 
>>  (fn [increment] 
>>(fn [x] (+ increment x 
>>
>> ... or this: 
>>
>> (def incish (partial map + [100 200 300])) 
>>
>> ... show the mechanics, but I'm looking for examples that would resonate 
>> more with an object-oriented programmer. Such examples might be ones that 
>> close over a number of values (which looks more like an object), or 
>> generate multiple functions that all close over a shared value (which looks 
>> more like an object), or 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). 
>>
>> Note: please put the flamethrower down. I'm not saying that "looking like 
>> objects" is the point of higher-order functions. 
>>
>> I'll give full credit. 
>>
>> - 
>> Brian Marick, Artisanal Labrador 
>> Contract programming in Ruby and Clojure 
>> Occasional consulting on Agile 
>>
>>
>>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-10 Thread Maik Schünemann
I think enlives transformersare a very good example of functions returning
functions. Maybe you should have a look at the getting started guide and
the tutorials
Send from Android
Am 09.08.2012 23:13 schrieb "Jonah Benton" :

> You've probably seen these, but if not, Doug Crockford's video series
> on javascript walks through a number of interesting information
> sharing examples like the ones you're looking for using
> fn-generating-fns-
>
> http://yuiblog.com/crockford/
>
> They're all great but "act 3 - function the ultimate" is especially juicy.
>
> The motivation for his examples is a little different than it would be
> for clojure, because that pattern is basically javascript's only
> abstraction trick. And certainly the semantics are different too.
>
> But if for whatever reason you haven't seen these videos, they're
> terrific and will probably spur some ideas.
>
>
> On Wed, Aug 8, 2012 at 12:48 PM, Brian Marick  wrote:
> > I'm looking for medium-scale examples of using function-generating
> functions. I'm doing it because examples like this:
> >
> > (def make-incrementer
> >  (fn [increment]
> >(fn [x] (+ increment x
> >
> > ... or this:
> >
> > (def incish (partial map + [100 200 300]))
> >
> > ... show the mechanics, but I'm looking for examples that would resonate
> more with an object-oriented programmer. Such examples might be ones that
> close over a number of values (which looks more like an object), or
> generate multiple functions that all close over a shared value (which looks
> more like an object), or 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).
> >
> > Note: please put the flamethrower down. I'm not saying that "looking
> like objects" is the point of higher-order functions.
> >
> > I'll give full credit.
> >
> > -
> > Brian Marick, Artisanal Labrador
> > Contract programming in Ruby and Clojure
> > Occasional consulting on Agile
> >
> >
> > --
> > 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
>
> --
> 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
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-10 Thread Alex Baranosky
While admittedly neat, and educational, this kind of code is fancy for
production use in my opinion:

(defn make-point [x y]
  (fn [member]
(cond (= member :x) x
 (= member :y) y)))

On Fri, Aug 10, 2012 at 6:06 PM, Jonas  wrote:

> How about the new reducers library:
>
>
> http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html
> http://clojure.com/blog/2012/05/15/anatomy-of-reducer.html
>
> Jonas
>
> On Wednesday, August 8, 2012 7:48:23 PM UTC+3, Brian Marick wrote:
>>
>> I'm looking for medium-scale examples of using function-generating
>> functions. I'm doing it because examples like this:
>>
>> (def make-incrementer
>>  (fn [increment]
>>(fn [x] (+ increment x
>>
>> ... or this:
>>
>> (def incish (partial map + [100 200 300]))
>>
>> ... show the mechanics, but I'm looking for examples that would resonate
>> more with an object-oriented programmer. Such examples might be ones that
>> close over a number of values (which looks more like an object), or
>> generate multiple functions that all close over a shared value (which looks
>> more like an object), or 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).
>>
>> Note: please put the flamethrower down. I'm not saying that "looking like
>> objects" is the point of higher-order functions.
>>
>> I'll give full credit.
>>
>> -
>> Brian Marick, Artisanal Labrador
>> Contract programming in Ruby and Clojure
>> Occasional consulting on Agile
>>
>>
>>  --
> 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
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-10 Thread Jonas
How about the new reducers library:

http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html
http://clojure.com/blog/2012/05/15/anatomy-of-reducer.html

Jonas

On Wednesday, August 8, 2012 7:48:23 PM UTC+3, Brian Marick wrote:
>
> I'm looking for medium-scale examples of using function-generating 
> functions. I'm doing it because examples like this: 
>
> (def make-incrementer 
>  (fn [increment] 
>(fn [x] (+ increment x 
>
> ... or this: 
>
> (def incish (partial map + [100 200 300])) 
>
> ... show the mechanics, but I'm looking for examples that would resonate 
> more with an object-oriented programmer. Such examples might be ones that 
> close over a number of values (which looks more like an object), or 
> generate multiple functions that all close over a shared value (which looks 
> more like an object), or 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). 
>
> Note: please put the flamethrower down. I'm not saying that "looking like 
> objects" is the point of higher-order functions. 
>
> I'll give full credit. 
>
> - 
> Brian Marick, Artisanal Labrador 
> Contract programming in Ruby and Clojure 
> Occasional consulting on Agile 
>
>
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-09 Thread Jonah Benton
You've probably seen these, but if not, Doug Crockford's video series
on javascript walks through a number of interesting information
sharing examples like the ones you're looking for using
fn-generating-fns-

http://yuiblog.com/crockford/

They're all great but "act 3 - function the ultimate" is especially juicy.

The motivation for his examples is a little different than it would be
for clojure, because that pattern is basically javascript's only
abstraction trick. And certainly the semantics are different too.

But if for whatever reason you haven't seen these videos, they're
terrific and will probably spur some ideas.


On Wed, Aug 8, 2012 at 12:48 PM, Brian Marick  wrote:
> I'm looking for medium-scale examples of using function-generating functions. 
> I'm doing it because examples like this:
>
> (def make-incrementer
>  (fn [increment]
>(fn [x] (+ increment x
>
> ... or this:
>
> (def incish (partial map + [100 200 300]))
>
> ... show the mechanics, but I'm looking for examples that would resonate more 
> with an object-oriented programmer. Such examples might be ones that close 
> over a number of values (which looks more like an object), or generate 
> multiple functions that all close over a shared value (which looks more like 
> an object), or 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).
>
> Note: please put the flamethrower down. I'm not saying that "looking like 
> objects" is the point of higher-order functions.
>
> I'll give full credit.
>
> -
> Brian Marick, Artisanal Labrador
> Contract programming in Ruby and Clojure
> Occasional consulting on Agile
>
>
> --
> 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

-- 
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


Re: Attractive examples of function-generating functions

2012-08-09 Thread hyPiRion

On Wednesday, August 8, 2012 6:48:23 PM UTC+2, Brian Marick wrote:
>
> ... show the mechanics, but I'm looking for examples that would resonate 
> more with an object-oriented programmer. Such examples might be ones that 
> close over a number of values (which looks more like an object), or 
> generate multiple functions that all close over a shared value (which looks 
> more like an object), or 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). 
>

You can make objects with its state hidden in a closure. E.g. if we want to 
make a stack where people cannot look at any other element than the first 
one, we can do that like this:

(letfn [(stack-fns [stack]
  {:push (fn [elt] (stack-fns (conj stack elt)))
   :pop (fn [] (stack-fns (pop stack)))
   :peek (fn [] (peek stack))
   :empty? (fn [] (empty? stack))})]
  (defn new-stack []
(stack-fns [])))

Usage would be like this:

(let [stack (reduce
 #((%1 :push) %2)
 (new-stack)
 (range 1 10))] ; To fill it with data
  (loop [stack stack]
(if ((stack :empty?))
  nil
  (let [h ((stack :peek))
t ((stack :pop))]
(prn h)
(recur t)

And this will print the numbers 9 to 1, before returning nil.

The Joy of Clojure looks at this from p. 138 to 141, so I take no credit 
for the idea - give that to the authors.

-- 
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

Re: Attractive examples of function-generating functions

2012-08-09 Thread Chris Ford
I use quite a few of these in my Overtone rendering of
Bach
:

; Defining a scale function from intervals

(defn sum-n [series n] (reduce + (take n series)))

(defn scale [intervals]
  (fn [degree]
(if-not (neg? degree)
  (sum-n (cycle intervals) degree)
  ((comp - (scale (reverse intervals)) -) degree

(def major (scale [2 2 1 2 2 2 1]))

(major 2)


; Defining a time function

(defn bpm [beats] (fn [beat] (-> beat (/ beats) (* 60) (* 1000

((bpm 90) 3)


; Defining a simple canon (like row, row, row your boat)

(defn canon [f] (fn [notes] (concat notes (f notes

(defn shift [point] (fn [notes] (map #(->> % (map + point) vec) notes)))
(defn simple [wait] (shift [wait 0]))

(canon (simple 4))

Code's on 
github
.

Cheers,

Chris

On 9 August 2012 05:44, Ben Mabey  wrote:

> On 8/8/12 10:48 AM, Brian Marick wrote:
>
>> I'm looking for medium-scale examples of using function-generating
>> functions. I'm doing it because examples like this:
>>
>> (def make-incrementer
>>   (fn [increment]
>> (fn [x] (+ increment x
>>
>> ... or this:
>>
>> (def incish (partial map + [100 200 300]))
>>
>> ... show the mechanics, but I'm looking for examples that would resonate
>> more with an object-oriented programmer. Such examples might be ones that
>> close over a number of values (which looks more like an object), or
>> generate multiple functions that all close over a shared value (which looks
>> more like an object), or 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).
>>
>> Note: please put the flamethrower down. I'm not saying that "looking like
>> objects" is the point of higher-order functions.
>>
>> I'll give full credit.
>>
>>
> Oh, I have the perfect one that I actually had to write the other day.
>  (The funny thing was that I wrote the exact same functionality in Ruby
> several years ago.. I like the clojure version much better).  I'll let the
> code and midje facts speak for themselves:
>
> ;; some context: 
> http://en.wikipedia.org/wiki/**Urn_problem
> (defn urn
>   "Takes a coll of pairs representing a distribution with keys being the
> probability of the corresponding values.
>
> Returns a function that when called will return a random value based on
> that distribution.
>
> Example:
>
>  (def multimnomial-urn (urn [[0.3 :red] [0.5 :black] [0.2 :green]]))
>
>  (take 5 (repeatedly multimnomial-urn)) => [:red :black :black :red :green]
> "
>   [dist]
>   {:pre [(= 1.0 (reduce + (map first dist)))]}
>   (let [range-dist (last (reduce (fn [[total pseudo-cdf] [percent val]]
>(let [new-total (+ percent total)]
>  [new-total (assoc pseudo-cdf
> new-total val)]))
>  [0.0 (sorted-map)]
>  dist))]
> (fn []
>   ;; TODO: use a better PRNG
>   (let [rn (rand)]
> (val (find-first #(< rn  (key %)) range-dist))
>
> ;;; test code
> (ns foo.core-test
>   (:use midje.sweet
> foo.core
> [useful.map :only [map-vals]]))
>
> (defn ratios [m]
>   (let [freqs (frequencies m)
> total (reduce + (vals freqs))]
> (map-vals freqs #(/ % total
>
> (defn percentages [m]
>   (-> m ratios (map-vals double)))
>
> (facts "'#urn"
>   (let [rand-key (urn [[0.3 :foo] [0.7 :bar]])]
> (percentages (repeatedly 100 rand-key)) => (just {:foo (roughly 0.3
> 0.1)
>   :bar (roughly 0.7
> 0.1)})))
>
>
> ;;; end code
>
>
> Hopefully I understood the question and this helps some.  For an example
> in a book you could make it a bit simpler where the urn could only contain
> two potential values (binomial urn).
>
>
> -Ben
>
>
> --
> 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+unsubscribe@**googlegroups.com
> For more options, visit this group at
> http://groups.google.com/**group/clojure?hl=en
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-08 Thread Ben Mabey

On 8/8/12 10:48 AM, Brian Marick wrote:

I'm looking for medium-scale examples of using function-generating functions. 
I'm doing it because examples like this:

(def make-incrementer
  (fn [increment]
(fn [x] (+ increment x

... or this:

(def incish (partial map + [100 200 300]))

... show the mechanics, but I'm looking for examples that would resonate more 
with an object-oriented programmer. Such examples might be ones that close over 
a number of values (which looks more like an object), or generate multiple 
functions that all close over a shared value (which looks more like an object), 
or 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).

Note: please put the flamethrower down. I'm not saying that "looking like 
objects" is the point of higher-order functions.

I'll give full credit.



Oh, I have the perfect one that I actually had to write the other day.  
(The funny thing was that I wrote the exact same functionality in Ruby 
several years ago.. I like the clojure version much better).  I'll let 
the code and midje facts speak for themselves:


;; some context: http://en.wikipedia.org/wiki/Urn_problem
(defn urn
  "Takes a coll of pairs representing a distribution with keys being 
the probability of the corresponding values.


Returns a function that when called will return a random value based on 
that distribution.


Example:

 (def multimnomial-urn (urn [[0.3 :red] [0.5 :black] [0.2 :green]]))

 (take 5 (repeatedly multimnomial-urn)) => [:red :black :black :red :green]
"
  [dist]
  {:pre [(= 1.0 (reduce + (map first dist)))]}
  (let [range-dist (last (reduce (fn [[total pseudo-cdf] [percent val]]
   (let [new-total (+ percent total)]
 [new-total (assoc pseudo-cdf 
new-total val)]))

 [0.0 (sorted-map)]
 dist))]
(fn []
  ;; TODO: use a better PRNG
  (let [rn (rand)]
(val (find-first #(< rn  (key %)) range-dist))

;;; test code
(ns foo.core-test
  (:use midje.sweet
foo.core
[useful.map :only [map-vals]]))

(defn ratios [m]
  (let [freqs (frequencies m)
total (reduce + (vals freqs))]
(map-vals freqs #(/ % total

(defn percentages [m]
  (-> m ratios (map-vals double)))

(facts "'#urn"
  (let [rand-key (urn [[0.3 :foo] [0.7 :bar]])]
(percentages (repeatedly 100 rand-key)) => (just {:foo (roughly 0.3 
0.1)
  :bar (roughly 0.7 
0.1)})))



;;; end code


Hopefully I understood the question and this helps some.  For an example 
in a book you could make it a bit simpler where the urn could only 
contain two potential values (binomial urn).



-Ben

--
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


Re: Attractive examples of function-generating functions

2012-08-08 Thread Robert Marianski
On Wed, Aug 08, 2012 at 08:00:26PM -0600, Jim Weirich wrote:
> 
> On Aug 8, 2012, at 1:50 PM, Timothy Baldridge  wrote:
> 
> > 
> > >>I'm looking for medium-scale examples of using function-generating 
> > >>functions.
> > 
> > 
> > I'm not sure if this is exactly what you're looking for, but this sort of 
> > thing is pretty cool: 
> > 
> > (defn make-point [x y]
> >   (fn [member]
> > (cond (= member :x) x
> >  (= member :y) y)))
> 
> Nice.  I always enjoyed this variation on the whole make-point theme:
> 
> (defn make-point [x y]
>  (fn [f] (f x y)))
> 
> (defn point-x [pt]
>  (pt (fn [x y] x)))
> 
> (defn point-y [pt]
>  (pt (fn [x y] y)))
> 
> (def pt (make-point 1 2))
> 
> (println  [(point-x pt)
>   (point-y pt)])
> 
> -- 
> -- Jim Weirich
> -- jim.weir...@gmail.com

Very nice. Similarly, I thought it was slick when I saw this idea used
as an example implementation for cons. Funny enough, it's exactly the
same except for the names.

(defn cons [a b]
  (fn [f] (f a b)))

(defn car [xs]
  (xs (fn [a b] a)))

(defn cdr [xs]
  (xs (fn [a b] b)))

Robert

-- 
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


Re: Attractive examples of function-generating functions

2012-08-08 Thread Brian Rowe
Maybe SICP's simulator of digital circuits will provide some inspiration. I 
know when I read this I was deeply awed by what HOFs can do. Maybe 
Clojure's zippers would be good too?

On Wednesday, August 8, 2012 12:48:23 PM UTC-4, Brian Marick wrote:
>
> I'm looking for medium-scale examples of using function-generating 
> functions. I'm doing it because examples like this: 
>
> (def make-incrementer 
>  (fn [increment] 
>(fn [x] (+ increment x 
>
> ... or this: 
>
> (def incish (partial map + [100 200 300])) 
>
> ... show the mechanics, but I'm looking for examples that would resonate 
> more with an object-oriented programmer. Such examples might be ones that 
> close over a number of values (which looks more like an object), or 
> generate multiple functions that all close over a shared value (which looks 
> more like an object), or 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). 
>
> Note: please put the flamethrower down. I'm not saying that "looking like 
> objects" is the point of higher-order functions. 
>
> I'll give full credit. 
>
> - 
> Brian Marick, Artisanal Labrador 
> Contract programming in Ruby and Clojure 
> Occasional consulting on Agile 
>
>
>

-- 
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

Re: Attractive examples of function-generating functions

2012-08-08 Thread Jim Weirich

On Aug 8, 2012, at 1:50 PM, Timothy Baldridge  wrote:

> 
> >>I'm looking for medium-scale examples of using function-generating 
> >>functions.
> 
> 
> I'm not sure if this is exactly what you're looking for, but this sort of 
> thing is pretty cool: 
> 
> (defn make-point [x y]
>   (fn [member]
> (cond (= member :x) x
>  (= member :y) y)))

Nice.  I always enjoyed this variation on the whole make-point theme:

(defn make-point [x y]
 (fn [f] (f x y)))

(defn point-x [pt]
 (pt (fn [x y] x)))

(defn point-y [pt]
 (pt (fn [x y] y)))

(def pt (make-point 1 2))

(println  [(point-x pt)
  (point-y pt)])

-- 
-- Jim Weirich
-- jim.weir...@gmail.com





-- 
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


Re: Attractive examples of function-generating functions

2012-08-08 Thread Brian Marick

On Aug 8, 2012, at 2:50 PM, Timothy Baldridge wrote:

> I'm not sure if this is exactly what you're looking for, but this sort of 
> thing is pretty cool: 
> 
> (defn make-point [x y]
>   (fn [member]
> (cond (= member :x) x
>  (= member :y) y)))
> 

I actually have a whole chapter on this. (Arguably, the whole first part of the 
book leads up to that chapter.) I even use Point as an example!

But "it's functions all the way down!" is not what I'm looking for in this 
section. Because you wouldn't use such a scheme instead of conventional objects.

-
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Occasional consulting on Agile


-- 
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


Re: Attractive examples of function-generating functions

2012-08-08 Thread Nelson Morris
On Wed, Aug 8, 2012 at 11:48 AM, Brian Marick  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


Re: Attractive examples of function-generating functions

2012-08-08 Thread nicolas.o...@gmail.com
trampolines is a slightly different example.

-- 
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

Re: Attractive examples of function-generating functions

2012-08-08 Thread Timothy Baldridge
>>I'm looking for medium-scale examples of using function-generating
functions.


I'm not sure if this is exactly what you're looking for, but this sort of
thing is pretty cool:

(defn make-point [x y]
  (fn [member]
(cond (= member :x) x
 (= member :y) y)))

We're basically creating an immutable object without using a single data
structure:

(def pnt (make-point 1 2))

=> (pnt :x)
1
=> (pnt :y)
2

You can even get a bit more fancy:

(defn make-point [x y]
  (fn [member]
(cond (= member :x) x
  (= member :y) y
  (= member :with-x)
(fn [newx]
   (make-point newx y)

Timothy


>

-- 
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