Re: Problem passing a function into a function

2011-11-03 Thread Sam Aaron
Hi Andy,

the issue you're having is that using the & for rest args captures the rest of 
the arguments as a seq. Therefore when you pass a fn as the final parameter to 
#'timed-agent, #'test-func is not bound to the fn you passed in but a seq 
containing that fn. You either need to pull out the fn using something like 
#'first or you can define #'timed-agent to deal with multiple arities:

(defn foo
  "A simple function that will tell you whether you passed
   in 0, 1 or 2 args"
  ([](println "no args"))
  ([a]   (println "one arg"))
  ([a b] (println "two args!")))

Sam

---
http://sam.aaron.name

On 3 Nov 2011, at 21:57, AndyK wrote:

> I'm running into a strange problem that I can't see the bottom of.
> Wonder if someone can explain why I'm seeing the behavior I'm
> seeing...
> 
> I have a function that uses a Java timer to run a function and can
> also check the results of that function by passing in an optional test
> function
> 
> (defn timed-agent [limit timed-func & test-func]
>  (let [a (agent 0)
>;; test-func can be defined here
>agent-func (fn [v] (let [result (timed-func v)]
> (when test-func (test-func result)) (inc
> v)))
>t (java.util.Timer.)
>tt (proxy [java.util.TimerTask] [] (run [] (send-off a agent-
> func)))]
>(set-validator! a #(> limit %))
>(.scheduleAtFixedRate t tt 1000 1000)))
> 
> This works if I don't pass in a test function
> (timed-agent 10 prn)
> You will see output of 0 1 2 3...9
> 
> This does not work if I pass in a test function
> (timed-agent 10 prn prn)
> You will see output of 0 and nothing more
> 
> This does work if I define test-func within the function (on the
> commented line) - ex, as prn
> You will see 0 nil 1 nil 2 nil...9 nil
> 
> First question - why can't I pass in a test function and have the
> timer loop work as expected
> Second question - is there a better way for me to set an upper bound
> on a Java timer using Clojure?
> 
> -- 
> 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: Problem passing a function into a function

2011-11-03 Thread Chris Perkins
The problem is that inside your timed-agent function, "test-func" is not a 
function - it is a one-element sequence containing your test function. 
That's what using & in the arguments vector does.

Try it like this:  (defn timed-agent [limit timed-func & [test-func]]  ...)

- Chris

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

Problem passing a function into a function

2011-11-03 Thread AndyK
I'm running into a strange problem that I can't see the bottom of.
Wonder if someone can explain why I'm seeing the behavior I'm
seeing...

I have a function that uses a Java timer to run a function and can
also check the results of that function by passing in an optional test
function

(defn timed-agent [limit timed-func & test-func]
  (let [a (agent 0)
;; test-func can be defined here
agent-func (fn [v] (let [result (timed-func v)]
 (when test-func (test-func result)) (inc
v)))
t (java.util.Timer.)
tt (proxy [java.util.TimerTask] [] (run [] (send-off a agent-
func)))]
(set-validator! a #(> limit %))
(.scheduleAtFixedRate t tt 1000 1000)))

This works if I don't pass in a test function
(timed-agent 10 prn)
You will see output of 0 1 2 3...9

This does not work if I pass in a test function
(timed-agent 10 prn prn)
You will see output of 0 and nothing more

This does work if I define test-func within the function (on the
commented line) - ex, as prn
You will see 0 nil 1 nil 2 nil...9 nil

First question - why can't I pass in a test function and have the
timer loop work as expected
Second question - is there a better way for me to set an upper bound
on a Java timer using Clojure?

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