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 7430681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:10:48+00:00 2026-05-29T09:10:48+00:00

In Eric Torreborre’s blogpost on the paper Essence of the Iterator Pattern , he

  • 0

In Eric Torreborre’s blogpost on the paper Essence of the Iterator Pattern, he describes how the cartesian product of a traverse is also a traverse.

Can anyone show me an example of this using the scalaz library as I can’t figure it out. Let’s say the problem is that, for a List[Int] I want to provide both of:

  1. The Int sum of the elements in the list
  2. A List[String] the elements of which are created by appending the “Z” to the String representation of the Ints

My understanding is that I can do this using traverse but in such a way as to only actually traverse my structure once, unlike this solution:

val xs = List(1, 2, 3, 4)
val (sum, strings)  = (xs.sum, xs map (_.toString + "Z"))

NOTE 1 – I know that there are other ways of doing this and that I neither need traverse for this example, and nor is traverse even necessarily the clearest way to solve it. I am, however, trying to understand traverse, so am really looking for the answer to the question as stated


EDIT – thanks to missingfaktor below for showing how to do this using State. I guess what I want to know is how I can compose the two independent calculations. For example; my functions are notionally as follows:

val shape = (_ : List[Int]) map (_.toString + "Z")
val accum = (_ : List[Int]).sum

I want to have these mechanisms of accumulation independently of one another and then choose whether to traverse my List[Int] using either or both of them. I imagined some code a bit like this:

xs traverse shape //A List[String]
xs traverse accum //An Int

xs traverse (shape <x> accum) //The pair (List[String], Int)

Eric implies that this is possible but I don’t get how to do it ~ i.e. I don’t see how to define shape and accum in such a way as that they can be composed, nor how to compose them.

NOTE 2 that shape and accum are not meant to literally be the functions with the signatures as above. They are expressions which have the type necessary to perform the above traversals.

  • 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-05-29T09:10:49+00:00Added an answer on May 29, 2026 at 9:10 am

    I’m adding my own answer, building on Jason’s one, to show different ways of traversing the list:

    import org.specs2._
    import scalaz.std.anyVal._, scalaz.std.list._
    import scalaz._, std.tuple._
    import scalaz.{Monoid, Applicative}
    
    class TraverseSpec extends mutable.Specification {
    
      implicit val Sum = Monoid[Int].applicative
      implicit val Concat = Monoid[List[String]].applicative
      implicit val A: Applicative[({type λ[α] = (Int, List[String])})#λ] = Sum.product[({type λ[α]=List[String]})#λ](Concat)
      val xs = List(1, 2, 3, 4)
    
      "traverse - by folding the list with a Monoid" >> {
        val (sum, text) = Foldable[List].foldMap(xs)(a => (a, List(a.toString + "Z")))
        (sum, text) === (10, List("1Z", "2Z","3Z", "4Z"))
      }
      "traverse - with a function returning a tuple" >> {
        val (sum, text) = A.traverse(xs)(a => (a, List(a.toString + "Z")))
        (sum, text.reverse) === (10, List("1Z", "2Z","3Z", "4Z"))
      }
      "traverse - with 2 functions and 2 traversals" >> {
        val count   = (a: Int) => a
        val collect = (a: Int) => List(a.toString+"Z")
    
        val sum  = Sum.traverse(xs)(count)
        val text = Concat.traverse(xs)(collect)
    
        (sum, text.reverse) === (10, List("1Z", "2Z","3Z", "4Z"))
      }
      "traverse - with 2 functions and 1 fused traversal" >> {
        val sum     = (a: Int) => a
        val collect = (a: Int) => List(a.toString+"Z")
    
        implicit def product[A, B, C](f: A => B): Product[A, B] = Product(f)
        case class Product[A, B](f: A => B) {
          def <#>[C](g: A => C) = (a: A) => (f(a), g(a))
        }
    
        val (total, text)  = A.traverse(xs)(sum <#> collect)
        (total, text.reverse) === (10, List("1Z", "2Z","3Z", "4Z"))
      }
    }
    

    I think that the last example shows what you’re after: 2 independently defined functions which can be composed to do just one traversal.

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

Sidebar

Related Questions

As Eric Gunnerson shows in this blog post, in C# you can nest using
I'm reading through Eric Evans' awesome work, Domain-Driven Design. However, I can't help feeling
I can use Eric Martin's simplemodal somewhat. But I would like to call it
Can I match and replace a text pattern in a MYSQL select? EDIT For
I have a string String a = /home/eric/workspace/ + file; file can be any
Eric suggests that you read your team's diffs every morning. Can I get TFS
I have 3 little questions about eric martin's modal window which I can't seem
In Eric Lippert's blog entry on umpires and the C# compiler and spec, he
A few years ago Eric Lippert blogged about a hypothetical infoof operator that would
How many version are eric meyer created of his CSS reset and where is

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.