FAQ
I'm trying to use a domonad block with the state monad, and I'm running
into trouble chaining together multiple monadic instructions.

Take this for example:

;; This block of code fetches the value from :a in the state,
;; and then associates a new value for :b if :a is set.
(domonad state-m
  [a (fetch-val :a)
  _ (if a
    (update-state #(assoc % :b 2))
    (m-result nil))
  ]
  nil)

My question is, what if I wanted to run two update functions, like
(update-state #(assoc % :b 2)) and (update-state #(assoc % :c 3)),
but within the if statement?

(p.s. I realize that assoc is variadic, and could easily deal with this,
however I'm using
it as a simple example of a more general case)

--
--
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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ben Wolfson at Jul 11, 2013 at 9:18 pm
    Is it not possible just to put another domonad block in the true branch of
    the if? Alternately, it should be possible to do it with an explicit bind
    operation.

    On Thu, Jul 11, 2013 at 2:11 PM, JvJ wrote:

    I'm trying to use a domonad block with the state monad, and I'm running
    into trouble chaining together multiple monadic instructions.

    Take this for example:

    ;; This block of code fetches the value from :a in the state,
    ;; and then associates a new value for :b if :a is set.
    (domonad state-m
    [a (fetch-val :a)
    _ (if a
    (update-state #(assoc % :b 2))
    (m-result nil))
    ]
    nil)

    My question is, what if I wanted to run two update functions, like
    (update-state #(assoc % :b 2)) and (update-state #(assoc % :c 3)),
    but within the if statement?

    (p.s. I realize that assoc is variadic, and could easily deal with this,
    however I'm using
    it as a simple example of a more general case)

    --
    --
    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 unsubscribe from this group and stop receiving emails from it, send an
    email to clojure+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Ben Wolfson
    "Human kind has used its intelligence to vary the flavour of drinks, which
    may be sweet, aromatic, fermented or spirit-based. ... Family and social
    life also offer numerous other occasions to consume drinks for pleasure."
    [Larousse, "Drink" entry]

    --
    --
    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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • JvJ at Jul 11, 2013 at 9:43 pm
    Yeah it is possible to do it with another domonad block. I just don't want
    to clutter up the code by repeating myself.

    I made a macro do-state that introduces a new domonad block using the
    current state. It works:

    (defmacro do-state [bindings & r]
         `(fn [st] ( (domonad state-m ~bindings ~@r) st)))

    (domonad state-m
      [a (fetch-val :a)
      _ (if a
        (do-state[ _ (update-state #(assoc % :b 2))
                                                               _ (update-state
    #(assoc % :b 2))]
                                                               nil)
        (m-result nil))
      ]
      nil)
    On Thursday, July 11, 2013 2:18:44 PM UTC-7, Ben wrote:

    Is it not possible just to put another domonad block in the true branch of
    the if? Alternately, it should be possible to do it with an explicit bind
    operation.


    On Thu, Jul 11, 2013 at 2:11 PM, JvJ <kfjwh...@gmail.com <javascript:>>wrote:
    I'm trying to use a domonad block with the state monad, and I'm running
    into trouble chaining together multiple monadic instructions.

    Take this for example:

    ;; This block of code fetches the value from :a in the state,
    ;; and then associates a new value for :b if :a is set.
    (domonad state-m
    [a (fetch-val :a)
    _ (if a
    (update-state #(assoc % :b 2))
    (m-result nil))
    ]
    nil)

    My question is, what if I wanted to run two update functions, like
    (update-state #(assoc % :b 2)) and (update-state #(assoc % :c 3)),
    but within the if statement?

    (p.s. I realize that assoc is variadic, and could easily deal with this,
    however I'm using
    it as a simple example of a more general case)

    --
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clo...@googlegroups.com<javascript:>
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+u...@googlegroups.com <javascript:>
    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 unsubscribe from this group and stop receiving emails from it, send an
    email to clojure+u...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Ben Wolfson
    "Human kind has used its intelligence to vary the flavour of drinks, which
    may be sweet, aromatic, fermented or spirit-based. ... Family and social
    life also offer numerous other occasions to consume drinks for pleasure."
    [Larousse, "Drink" entry]
    --
    --
    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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupclojure @
categoriesclojure
postedJul 11, '13 at 9:11p
activeJul 11, '13 at 9:43p
posts3
users2
websiteclojure.org
irc#clojure

2 users in discussion

JvJ: 2 posts Ben Wolfson: 1 post

People

Translate

site design / logo © 2022 Grokbase