Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:49:09+00:00 2026-06-15T16:49:09+00:00

I am planning to start using Monadic style in my Scala code for, amongst

  • 0

I am planning to start using Monadic style in my Scala code for, amongst others, threading state. Here’s a simplified example of combining 3 monadic functions (and caring only about the side effects)

import scalaz._
import Scalaz._

object MonadTest {
  def adder(i: Int) = State[String, Int] ({str: String => (str + i.toString + " ", i) })
  val oneTwoThreeMonad = for {
    m1 <- adder(1)
    m2 <- adder(2)
    m3 <- adder(3)
  } yield m3
  oneTwoThreeMonad("start: ")._1 //String = "start: 1 2 3 "
}

This all is pretty self-explanatory and works as expected. But for this approach to be really useful to me I would like to be able to combine it with List for-comprehension. Here’s a bit of (not working) code to show what I mean:

val list = List(1, 2, 3)

val oneTwoThreeBis = for {
  i <- list
  mx <- adder(i)
} yield mx

Basically I would like to be able to combine monads based on arguments from a List – run the monadic function on each of the elements of the list and accumulate the side-effects as I go. I understand the example syntax doesn’t work and I see why it doesn’t – I’m just looking for a clean, elegant equivalent.

I am pretty sure it is possible to achieve this using scalaz monad transformers, more specifically with StateT but I’m not really sure how one would go about doing it.

PS. I’m using Scalaz 7.0-M3, so the syntax might be a little different from the most common 6.x.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:49:10+00:00Added an answer on June 15, 2026 at 4:49 pm

    I’m not sure I understand exactly what you’re looking for, but it sounds like you want something more like traverse here (where traverse is a more general version of Haskell’s mapM):

    import scalaz._, Scalaz._
    
    def adder(i: Int) = State[String, Int](str => (str + i.toString + " ", i))
    
    List(1, 2, 3).traverseS(adder)("start: ")._1
    

    This will print the following, as expected:

    res0: String = "start: 1 2 3 "
    

    Note that I’m using traverseS (where the S stands for State) to avoid having to write out the rather messy type parameter, but traverse is more generally useful anytime you want to map a monadic function over something traversable.

    I’m happy to give a StateT example if this isn’t what you wanted, but that’s going to end up with you having something of type List[(String, Int)].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MySQL with Memcached, but I'm planning to start using PostgreSQL instead of
I've been developing Java Web apps using Eclipse as the IDE. Planning to start
I'm planning to start using hashes insead of regular keys. But I can't find
I am planning to start some Java mobile phone development. I downloaded the wireless
Greeting, I'm a C# programmer guy. I'm planning to start developing app for iphone
We are planning to write an application that, in its start phase will be
Planning to create a new website for our product using ASP.NET MVC 4. Site
I'm planning to start an application comprises of augmented reality in android. Proposed application
Suppose, I am about to start a project using ASP.NET and SQL Server 2005.
I´m planning to make a full dynamic site using pjax, with static menu (only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.