Re: Do you use Monads in your real clojure applications
Thank you for your great answer Konrad. On Jan 3, 9:46 am, googlegro...@khinsen.fastmail.net wrote: > Dragan R writes: > > > On the net I read that "Impure functional programming doesn't really > > need monads." > > and "It appears that in the presence of mutable state, a lot of the > > advantages of monads become moot." > > Monads are an abstraction mechanism, so you never need them. You can > always use the lower-level techniques in terms of which monads are > implemented. > > The only language that has made monads nearly inevitable is Haskell, > because its standard library is based on monads. But even in Haskell, > monads can be avoided, at the cost of rewriting stuff that is already > in the standard library. > > As with all abstractions, the real question is not whether you need > them, but whether their use improves your programs. This depends as > much on the programmer as on the problem, so there is not clear > answer. As a rule of thumb, I'd say that you should consider using > monads if your application > > 1) can profit from more than one of them, or > 2) can profit from the generic monad operators. > > I probably use monad more than the average programme in my own code, > but that's also because I happen to be familiar with them. I could > very well live with fewer monads in my code. But once you know monads, > they appear magically everywhere you look ;-) > > Konrad. -- 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: Do you use Monads in your real clojure applications
Thanks to all authors for help. Best regards, Dragan Radevic -- 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: Do you use Monads in your real clojure applications
Hi, Am 04.01.2012 um 10:12 schrieb Thorsten Wilms: >> (defn handler [{:keys [params] :as req}] >> (if (person/valid? params) >> {:status 200 :body (json/generate-string (person/create params))} >> {:status 400})) Or you let the create function return nil on invalid params. (defn handler [{:keys [params] :as req}] (if-let [p (person/create params)] {:status 200 :body (json/generate-string p)} {:status 400})) Sincerely Meikel -- 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: Do you use Monads in your real clojure applications
On 01/04/2012 03:05 AM, Takahiro Hozumi wrote: In MVC pattern, Model should take responsibility for business logic. Therefore I write validate function for creating in the model. If creating a instance of the model should be safe, I must validate a parameter in the create function. My problem is that a controller have to validate a parameter twice in the validate function and the create function. Ring handler example (defn handler [{:keys [params] :as req}] (if (person/valid? params) {:status 200 :body (json/generate-string (person/create params))} {:status 400})) I think this might be suited to monads, which I don't fully understand. If you work with self-imposed restrictions that make you call functions with the same parameters twice in short order, you should reconsider those restrictions. If all the information you need from a validator is true/false, you can just call a model function. One that is written under the assumption that a certain requirement is met, before it is called. If the validator returns nil or some concrete piece of imformation, you just need to call it, store the result. Then you may call a model function with the result (if it is non-nil). Moustache makes that very straightforward: (defn integer [s] "returns nil if s does not represent an integer (try (Integer/parseInt s) (catch Exception e))) (app ["order" [id integer]] my-handler) ; for "/order/134" @id@ will be bind to 134 (not "134"), this route will not match "/order/abc". Example taken from https://github.com/cgrand/moustache -- Thorsten Wilms thorwil's design for free software: http://thorwil.wordpress.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: Do you use Monads in your real clojure applications
In MVC pattern, Model should take responsibility for business logic. Therefore I write validate function for creating in the model. If creating a instance of the model should be safe, I must validate a parameter in the create function. My problem is that a controller have to validate a parameter twice in the validate function and the create function. Ring handler example (defn handler [{:keys [params] :as req}] (if (person/valid? params) {:status 200 :body (json/generate-string (person/create params))} {:status 400})) I think this might be suited to monads, which I don't fully understand. On Jan 4, 4:35 am, Mark Engelberg wrote: > Some of the most common uses for monads have pre-existing mechanisms > with Clojure to handle them, e.g.: > sequence monad (for) > state monad (Clojure has many stateful mechansisms) > maybe monad (Clojure programmers usually just return nil for failure, > and use something like when-let to process it) > > In terms of higher-level DSLs constructed out of monads, the most > useful monadic frameworks I've seen are monads for parsing, and monads > for representing probability distributions. If I needed to do one of > those things in Clojure, I'd look closely at monad options. But since > I haven't needed to do those things, and the common uses for monads > are already covered, I haven't found a need to do any monadic style > programming in my own code. > > --Mark -- 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: Do you use Monads in your real clojure applications
Some of the most common uses for monads have pre-existing mechanisms with Clojure to handle them, e.g.: sequence monad (for) state monad (Clojure has many stateful mechansisms) maybe monad (Clojure programmers usually just return nil for failure, and use something like when-let to process it) In terms of higher-level DSLs constructed out of monads, the most useful monadic frameworks I've seen are monads for parsing, and monads for representing probability distributions. If I needed to do one of those things in Clojure, I'd look closely at monad options. But since I haven't needed to do those things, and the common uses for monads are already covered, I haven't found a need to do any monadic style programming in my own code. --Mark -- 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: Do you use Monads in your real clojure applications
On Jan 3, 2:46 am, googlegro...@khinsen.fastmail.net wrote: > I probably use monad more than the average programme in my own code, > but that's also because I happen to be familiar with them. I could > very well live with fewer monads in my code. But once you know monads, > they appear magically everywhere you look ;-) Big +1 to that. I've submitted a talk to ClojureWest that partly touches on how to use monads in Clojure. Basically, monads are like design patterns for writing DSL's. Other than that, while they don't let you do anything you couldn't do in other ways, I really like how they eliminate the need for dealing with state held in symbols. I've found that testing is easier and code is cleaner using them. For instance, I showed how a state monad could be used in Chris Granger's Korma library. https://github.com/ibdknox/Korma/pull/35 Jim -- 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: Do you use Monads in your real clojure applications
We use monads within one of our work project, but not to any large amount. It mostly boils down to using the Maybe monad to avoid giant nested if- lets. - Lee Hinman -- 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: Do you use Monads in your real clojure applications
On 3 January 2012 08:46, wrote: > Dragan R writes: > > > On the net I read that "Impure functional programming doesn't really > > need monads." > > and "It appears that in the presence of mutable state, a lot of the > > advantages of monads become moot." > > Monads are an abstraction mechanism, so you never need them. You can > always use the lower-level techniques in terms of which monads are > implemented. This +1. You need to be more specific about what you mean when you say code "uses monads". In one sense, any code which uses a 'for' sequence comprehension is using a monad, because it satisfies all of the properties of a monad. In another sense, only code which contains and names specific things as monads, and uses general operators which apply to all monads, is using them. I've been using monads a lot recently with Overtone -- by which I mean I've been using (state-t cont-m), the continuation monad transformed to add state. I'm using it because: a) I want to simulate state representing the current beat number, and b) I want to schedule future events using apply-at; so rather than returning from a function to continue a melody, I call apply-at and pass the current continuation to it. As an example of what I've achieved, see this gist: https://gist.github.com/1441831 Using clojure.algo.monads, I have created a basic DSL which allows me to say (wait 1) to pause for one beat, and (at-current-beat (foo)) to schedule event (foo) to happen on the current beat. These commands are relative to the current time, but using monads I can transform them into commands at an absolute time using overtone's built-in 'at and 'apply-at macros, and to automagically handle the scheduling. This is a work in progress; I'm planning a fuller write-up of what I'm doing which I will send to the overtone list once I've ironed out the wrinkles. Phil -- 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: Do you use Monads in your real clojure applications
Hi, I used monads in two projects. * The last rewrite of ClojureQL before v1.0 used a state monad to keep track of various things during query creation. * ClojureCheck also uses a monad approach to create and combine generators for test data. * Dave Ray and I tried a monad style in the async branch of seesaw. Both were custom monad implementations. Both work(ed) reasonably well. However things have a relatively high strangeness factor. Since the execution of things is deferred till someone actually runs the monad pipeline, you can't use your usual try/catch construct to take care of problems. Everything has to be constrained to your monadic function. So you need to have some way to error out of your monadic pipeline. This leads you to monad transformers and complicates things even more. In Clojure I haven't seen a use were other approaches weren't just as feasible or were monads would have simplified things. They are a legal approach, but I would judge on a case-by-base basis. Sincerely Meikel -- 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
Do you use Monads in your real clojure applications
Dragan R writes: > On the net I read that "Impure functional programming doesn't really > need monads." > and "It appears that in the presence of mutable state, a lot of the > advantages of monads become moot." Monads are an abstraction mechanism, so you never need them. You can always use the lower-level techniques in terms of which monads are implemented. The only language that has made monads nearly inevitable is Haskell, because its standard library is based on monads. But even in Haskell, monads can be avoided, at the cost of rewriting stuff that is already in the standard library. As with all abstractions, the real question is not whether you need them, but whether their use improves your programs. This depends as much on the programmer as on the problem, so there is not clear answer. As a rule of thumb, I'd say that you should consider using monads if your application 1) can profit from more than one of them, or 2) can profit from the generic monad operators. I probably use monad more than the average programme in my own code, but that's also because I happen to be familiar with them. I could very well live with fewer monads in my code. But once you know monads, they appear magically everywhere you look ;-) Konrad. -- 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: Do you use Monads in your real clojure applications
Midje has some of its error handling code implemented with a monad. https://github.com/marick/Midje/blob/master/src/midje/error_handling/monadic.clj#L32 `error-let` is like a regular let, except if there is a validation-error, that error short-circuits out. Alex -- 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: Do you use Monads in your real clojure applications
On Mon, 2012-01-02 at 05:18 -0800, Dragan R wrote: > and are monads realy useful in impure functional languages like > clojure? Clojure's impurity doesn't mean that we wouldn't like to avoid side-effecty ways of doing stuff. Monads can help you there, among other things. The monad idea captures a very high-level abstraction, giving you the stuff that is implemented on that idea for free. For example, if you want an idea of `map' for your data structure, you'll get m-fmap if you write a monad instance. Some of this stuff seeps into more commonplace operators. `for' is essentially a sugary and faster `domonad sequence-m'. Likewise, the terribly useful `-?>' from core.incubator is much like `((with-monad maybe-m (m-chain [...])) start)'. That said, Haskell-style evaluation and type inference does make them more useful and easier to use. > I would like to know do you use Monads in your real clojure > applications, Aside from `-?>' and `for', I've implemented one custom monad instance for a production Clojure application. No doubt some of the stuff I've done with dynamic vars would have been cleaner with the reader monad; https://github.com/straszheimjeffrey/The-Kiln will, I think, be a good use case for the reader monad when it's ready. -- Stephen Compall ^aCollection allSatisfy: [:each|aCondition]: less is better -- 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
Do you use Monads in your real clojure applications
Hello, I would like to know do you use Monads in your real clojure applications, and are monads realy useful in impure functional languages like clojure? On the net I read that "Impure functional programming doesn't really need monads." and "It appears that in the presence of mutable state, a lot of the advantages of monads become moot." ( sources: http://www.reddit.com/r/programming/comments/p66e/are_monads_actually_used_in_anything_except http://marijnhaverbeke.nl/monad.html ) so I am interesting to hear what clojure programmers think about that. Thanks in advance for your help. Best regards, Dragan Radevic -- 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