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

  • Home
  • SEARCH
  • 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 7657463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:02:20+00:00 2026-05-31T13:02:20+00:00

I’m trying to understand the traverseImpl implementation in scalaz-seven : def traverseImpl[F[_], A, B](l:

  • 0

I’m trying to understand the traverseImpl implementation in scalaz-seven:

def traverseImpl[F[_], A, B](l: List[A])(f: A => F[B])(implicit F: Applicative[F]) = {
  DList.fromList(l).foldr(F.point(List[B]())) {
     (a, fbs) => F.map2(f(a), fbs)(_ :: _)
  }
}

Can someone explain how the List interacts with the Applicative? Ultimately, I’d like to be able to implement other instances for Traverse.

  • 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-31T13:02:21+00:00Added an answer on May 31, 2026 at 1:02 pm

    An applicative lets you apply a function in a context to a value in a context. So for instance, you can apply some((i: Int) => i + 1) to some(3) and get some(4). Let’s forget that for now. I’ll come back to that later.

    List has two representations, it’s either Nil or head :: tail. You may be used to fold over it using foldLeft but there is another way to fold over it:

    def foldr[A, B](l: List[A], acc0: B, f: (A, B) => B): B = l match {
       case Nil => acc0
       case x :: xs => f(x, foldr(xs, acc0, f))
    }
    

    Given List(1, 2) we fold over the list applying the function starting from the right side – even though we really deconstruct the list from the left side!

    f(1, f(2, Nil))
    

    This can be used to compute the length of a list. Given List(1, 2):

    foldr(List(1, 2), 0, (i: Int, acc: Int) => 1 + acc)
    // returns 2
    

    This can also be used to create another list:

    foldr[Int, List[Int]](List(1, 2), List[Int](), _ :: _)
    //List[Int] = List(1, 2)
    

    So given an empty list and the :: function we were able to create another list. What if our elements are in some context? If our context is an applicative then we can still apply our elements and :: in that context. Continuing with List(1, 2) and Option as our applicative. We start with some(List[Int]())) we want to apply the :: function in the Option context. This is what the F.map2 does. It takes two values in their Option context, put the provided function of two arguments into the Option context and apply them together.

    So outside the context we have (2, Nil) => 2 :: Nil

    In context we have: (Some(2), Some(Nil)) => Some(2 :: Nil)

    Going back to the original question:

    // do a foldr 
    DList.fromList(l).foldr(F.point(List[B]())) {
      // starting with an empty list in its applicative context F.point(List[B]())
      (a, fbs) => F.map2(f(a), fbs)(_ :: _)
      // Apply the `::` function to the two values in the context
    }
    

    I am not sure why the difference DList is used. What I see is that it uses trampolines so hopefully that makes this implementation work without blowing the stack, but I have not tried so I don’t know.

    The interesting part about implementing the right fold like this is that I think it gives you an approach to implement traverse for algebric data types using catamorphisms.

    For instance given:

    trait Tree[+A]
    object Leaf extends Tree[Nothing]
    case class Node[A](a: A, left: Tree[A], right: Tree[A]) extends Tree[A]
    

    Fold would be defined like this (which is really following the same approach as for List):

    def fold[A, B](tree: Tree[A], valueForLeaf: B, functionForNode: (A, B, B) => B): B = {
      tree match {
        case Leaf => valueForLeaf
        case Node(a, left, right) => functionForNode(a, 
            fold(left, valueForLeaf, functionForNode), 
            fold(right, valueForLeaf, functionForNode)
          )
      }
    }
    

    And traverse would use that fold with F.point(Leaf) and apply it to Node.apply. Though there is no F.map3 so it may be a bit cumbersome.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.