• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is Monad and how it is useful

 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am learning Scala and I came across Monad, I have gone through it but I didn't understand it. So here I have question what is Monad in Scala and other Functional Programming and how it is useful?

 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer? I don't know, but I've decided not to worry about it!

Long answer? Well, there seems to be a whole industry around explaining monads to functional programmers. My limited understanding is that it's a set of rules for functional behaviour, a bit like an OO pattern but based on mathematical theory. If your "foo" function obeys these rules then it's a monad and certain assumptions can be made about it e.g. about how you can combine it with other functions. I think one common use of monads is to encapsulate functions that mutate state so you know when your code may have side effects. So monads (and monoids) occur in other functional programming languages, not just Scala, because they all need to manage state changes in a controlled fashion.

But don't rely on my vague ramblings - this guy can explain it all much better:
http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads
 
Nishan Patel
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chris webster wrote:Short answer? I don't know, but I've decided not to worry about it!

Long answer? Well, there seems to be a whole industry around explaining monads to functional programmers. My limited understanding is that it's a set of rules for functional behaviour, a bit like an OO pattern but based on mathematical theory. If your "foo" function obeys these rules then it's a monad and certain assumptions can be made about it e.g. about how you can combine it with other functions. I think one common use of monads is to encapsulate functions that mutate state so you know when your code may have side effects. So monads (and monoids) occur in other functional programming languages, not just Scala, because they all need to manage state changes in a controlled fashion.

But don't rely on my vague ramblings - this guy can explain it all much better:
http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads



Thanks for your reply.. It means a lot to me.

But here question is what set of rules functions has to satisfy? Only these three, 1) Associativity. 2) Left identity. and 3) Right identity.

Or functions have also follow other rule as well?
 
chris webster
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try tomorrow's webinar what have the monads ever done for us from Typesafe.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My advice is: Unless you're an academic who wants to know all the theoretical details, don't worry about what the theoretical explanation of what a monad is exactly. Understanding the theoretical, mathematical definition of monads is not necessary for practical programming.

This is my own practical idea: Monads are a common functional programming design pattern. A monad is a data type that acts as a "box" for some data. The box has a method that you can give a function, and it will execute that function of the content of the box, giving you another box with the result of the function. That method is often called "map" or "flatMap".

Examples of monads in Scala are Option and List. These are both "boxes" that contain data. An Option contains either zero (None) or one (Some) element; a List can contain zero or more elements. Both Option and List have a flatMap method. When you call flatMap and pass it a function, you'll get back another Option or List with that function applied to the element(s) contained by the Option or List.
 
chris webster
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice practical explanation from Jesper there.

If you really want to start digging into this pure FP stuff, have a look at Functional Programming In Scala by Paul Chiusano and Runar Bjarnason. I just got my hard copy of this a couple of days ago (after waiting 4 months!), and it looks like a great way to learn about pure FP and improve your Scala at the same time.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a very good graphical explanation of these concepts!

http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Don't I deserve a Cow for this?
 
Nishan Patel
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. Now I can imagine what Monad is..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic