tag:blogger.com,1999:blog-22587889.post8500761222734440572..comments2024-02-11T13:21:47.930+05:30Comments on Ruminations of a Programmer: An exercise in Refactoring - Playing around with Monoids and EndomorphismsAnonymoushttp://www.blogger.com/profile/01613713587074301135noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-22587889.post-29841586954618331062013-03-26T13:05:22.630+05:302013-03-26T13:05:22.630+05:30Hi Debasish,
you should not represent money with ...Hi Debasish,<br /><br />you should not represent money with a Double. You are going to get inexact results. (See e.g. http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)xpmatteohttps://www.blogger.com/profile/16626755265235840659noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-40833428315734180302013-03-12T08:58:09.415+05:302013-03-12T08:58:09.415+05:30Dear Debasish,
Thank you for your advice. I read ...Dear Debasish,<br /><br />Thank you for your advice. I read up on Kleisli. It looks very cool indeed and similar to what I'm trying to do. I'll try to rework my data representation to fit that pattern. I'll share my code if it will have anything interesting in it.<br /><br />Once again big thanks,<br />AlexAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-17647690610779224702013-03-07T10:58:38.358+05:302013-03-07T10:58:38.358+05:30You can try Kleisli >>= .. Will chalk out an...You can try Kleisli >>= .. Will chalk out an implementation tonight ..Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-137113117050957092013-03-07T02:44:39.791+05:302013-03-07T02:44:39.791+05:30Dear Debasish
If you let me rephrase my question ...Dear Debasish<br /><br />If you let me rephrase my question to make it more general...<br /><br />What is a pattern for implementing a map class that maps a key (action) to a list of methods and how would a client go about calling those methods and passing arguments to them?<br /><br />I greatly appreciate your help!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-43087130889600492242013-03-07T02:37:44.501+05:302013-03-07T02:37:44.501+05:30The best I could come up with is:
trait ActionMap...The best I could come up with is:<br /><br />trait ActionMapper {<br /> def id: Int<br /> def active: Boolean<br /><br /> val doOne = (user: String) => List("one" + "*" + user)<br /> val doTwo = (user: String) => List("two" + "*" + user + ":" + id)<br /> val doThree = (user: String) => List("three" + "*" + user + ":" + active)<br /><br /> def collectAll(funs: List[String => List[String]])(user: String): List[String] =<br /> (List[String]() /: funs)((a, b) => a ::: b(user))<br /><br /> val actMap = Map(<br /> "one" -> List(doOne),<br /> "one+two" -> List(doOne, doTwo),<br /> "one+two+three" -> List(doOne, doTwo, doThree))<br /><br /> def act(action: String, user: String): List[String] = {<br /> collectAll(actMap(action))(user)<br /> }<br />}<br /><br />object ActionExecutioner extends App with ActionMapper {<br /> override val id = 321<br /> override val active = false<br /><br /> println(act("one+two+three", "void"))<br />}<br /><br />Using either val of def for do* functions.<br /><br />I wonder if there is a more elegant way of doing it. The problem is that I have to implement/override both 'id' and 'active' in order to use the trait and thus I can't just do this:<br /><br />object Fails extends ActionMapper {<br /> act("one", "void")<br />}<br /><br />although those args are not technically required for 'doOne'.<br /><br />AlexAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-40660276899016431212013-03-07T02:32:30.244+05:302013-03-07T02:32:30.244+05:30Dear Debasish
Regarding the first question each d...Dear Debasish<br /><br />Regarding the first question each do* function returns List[String]. Alternatively each function could be called as continuation if possible.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-85083866443187636852013-03-06T12:57:31.309+05:302013-03-06T12:57:31.309+05:30Dear Anonymous -
Making the do* as vals will let ...Dear Anonymous -<br /><br />Making the do* as vals will let u do away with the partial applications .. have a look at https://gist.github.com/debasishg/5097410 .. Let me know what u think.<br /><br />Thanks.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-66233343487962976912013-03-06T12:39:31.627+05:302013-03-06T12:39:31.627+05:30Dear Anonymous -
Your doOne, dotwo all seem to be...Dear Anonymous -<br /><br />Your doOne, dotwo all seem to be String => String .. at least the implemented ones. Are they really String => String or String => List[String] ?Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-18377358125792319522013-03-06T08:12:00.820+05:302013-03-06T08:12:00.820+05:30Nice article!
I have a question as I'm new to...Nice article!<br /><br />I have a question as I'm new to Scala and I'm looking for a pattern similar to the one in the article, yet different.<br /><br />I'd like to define a map that has a key as an Action, and a list of methods to be applied as a value.<br /><br />I have this prototype, sorry for dumping this code here:<br /><br />def doOne(user: String): List[String] = List("one" + "*" + user)<br /><br /> def doTwo(user: String): List[String] = List("two" + "*" + user)<br /><br /> def doThree(user: String): List[String] = List("three" + "*" + user)<br /><br /> def collectAll(funs: List[String => List[String]])(user: String): List[String] =<br /> (List[String]() /: funs)((a, b) => a ::: b(user))<br /><br /> val actMap = Map(<br /> "one" -> List(doOne _),<br /> "one+two" -> List(doOne _, doTwo _)<br /> )<br /><br /> def act(action: String, user: String): List[String] = {<br /> collectAll(actMap(action))(user)<br /> }<br /><br />So I can do something like this with partially applied functions but they all take the same type and number of arguments. In my case I need to pass various args to functions, and I want it to happen without explicitly providing them. I can't think of a proper way of doing it. Method 'act' will be called by client which will provide args in some way. I'm suspecting that either continuations, closures or something else :) should allow me to do this.<br />I would appreciate to be pointed in a right direction. Thank youAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-82691616720463392112013-03-04T15:18:46.641+05:302013-03-04T15:18:46.641+05:30Great post! The endomorphism monoid is often handy...Great post! The endomorphism monoid is often handy. Here's a haskell version for fun:<br /><br />https://gist.github.com/jhickner/5081100Unknownhttps://www.blogger.com/profile/10428472637740674918noreply@blogger.com