On Thu, May 20, 2010 at 2:26 PM, Daniel Werner
<daniel.d.wer...@googlemail.com> wrote:
> On 20 May 2010 11:42, Anders Rune Jensen <anders.rune.jen...@gmail.com> wrote:
>> The algorithm:
>>
>> (defn change-state [cur-state]
>>   (when (> (:value cur-state) 10)
>>      (assoc cur-state :message "danger, danger")))
>>
>> How I'd have to write it when using an agent:
>>
>> (defn change-state [cur-state]
>>   (cond (> (:value cur-state) 10)
>>      (assoc cur-state :message "danger, danger")
>>      :else cur-state))
>
> In this example, you could also have written your function using 'if',
> which is not much more complex than your original definition using
> 'when':
>
> (defn change-state [cur-state]
>  (if (> (:value cur-state) 10)
>     (assoc cur-state :message "danger, danger")
>     cur-state))
>
> Of course, like you already said, this is a trivial example, but it
> shows an important fact about agent actions: They don't modify state.

I've been contemplating how to answer this for a little while. I think
I'll just show you the code that prompted me to write the macro:

(defn track-removed-on-agent [cur-state index]
  (let [pindex (cur-state :playlist-index)]
    (cond (= pindex index)
          (cond
            (= (count @playlist/tracks) 0) (stop-on-agent cur-state)
            (>= pindex (count @playlist/tracks))
            (let [new-index (dec pindex)]
              (cond (= (cur-state :state) player-state/playing)
                    (play-track-i-on-agent cur-state new-index)
                    :else
                    (assoc cur-state :playlist-index new-index)))
            :else
            (cond (= (cur-state :state) player-state/playing)
                  (play-track-i-on-agent cur-state pindex)))
          (> pindex index)
          (assoc cur-state :playlist-index (dec pindex))
          :else cur-state)))

Where is the bug in the above code?

Any code that can help me write code with less bugs for *free* is
welcome in my book. I don't know about yours?

-- 
Anders Rune Jensen

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

Reply via email to