DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Simplifying Monads in Scala

Simplifying Monads in Scala

Let's go over the importance of flatMaps to Monads in Scala to see what role the combinator plays and how to use them to simplify your Monad objects.

Nikhil Kumar user avatar by
Nikhil Kumar
·
Aug. 23, 17 · Tutorial
Like (8)
Save
Tweet
Share
43.07K Views

Join the DZone community and get the full member experience.

Join For Free

Monads are not at all a complex topic, but yet it comes under the advanced section of Scala language. So basically, Monads are structures that represent sequential computations.

Let's be clear: A Monad is not a class or a trait; it is a concept.

A Monad is an object that wraps another object in Scala. In Monads, the output of a calculation at any step is the input to other calculations, which run as a parent to the current step.

One positive point: Assuming you already know about flatMaps in Scala and you know how to access future values using combinators (including flatMap), you already know the use case for flatMap. It flattens the resulting list of strings into a sequence of characters, as seen in the following example:

scala> var name = Seq("Nikhil", "Mateo")
name: Seq[String] = List(Nikhil, Mateo)

scala> var exMap = name.map(_.toLowerCase)
exMap: Seq[String] = List(nikhil, mateo)

scala> var exMap = name.flatMap(_.toLowerCase)
exMap: Seq[Char] = List(n, i, k, h, i, l, m, a, t, e, o)


Directly check the exact example of the Monad:

val numList1 = List(1,2)
val numList2 = List(3,4)

numList1 flatMap { x => numList2 map {
        y => x + y
    }
}


Output: res10: List[Int] = List(4, 5, 5, 6)

Without a Monad:

for {
     | first <- numList1
     | second <- numList2
     | } yield first + second
res11: List[Int] = List(4, 5, 5, 6)


FlatMap is way more powerful than map. It gives us the ability to chain operations together, as you've seen in the previous section. Map's functionality is just a subset of flatMap's functionality.

If you want to have a map() available in your Monad, you can express it using a Monad's existing flatMap() and unit() methods like this:

(note that g is some function Int → Something, not Int → List[Something]):

m map g = flatMap(x => unit(g(x)))


Note: This is a quick post about Monads, and I will be writing more on them soon.

Though this isn't complete information, we're off to a good start. We have the practical idea behind monads. If you have suggestions please share, that would be a great for everyone to learn.

Monad (functional programming) Scala (programming language)

Published at DZone with permission of Nikhil Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • OpenID Connect Flows
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: