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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:04:20+00:00 2026-06-16T03:04:20+00:00

My problem is phrased in the code below. I’m trying to get some input

  • 0

My problem is phrased in the code below.
I’m trying to get some input that has the .map function in it. I know that if I call .map to it, it will return an Int to me.

  // In my case, they are different representations of Ints
  // By that I mean that in the end it all boils down to Int
  val list: Seq[Int] = Seq(1,2,3,4)
  val optInt: Option[Int] = Some(1)
  // I can use a .map with a Seq, check!
  list.map {
    value => println(value)
  }
  // I can use it with an Option, check!
  optInt.map {
    value => println(value)
  }
  // Well, you're asking yourself why do I have to do it,
  // Why don't I use foreach to solve my problem. Check!
  list.foreach(println)
  optInt.foreach(println)

  // The problem is that I don't know what I'm going to get as input
  // The only thing I know is that it's "mappable" (it has the .map function)
  // And that if I were to apply .map it would return Ints to me
  // Like this:
  def printValues(genericInputThatHasMap: ???) {
    genericInputThatHasMap.map {
      value => println(value)
    }
  }

  // The point is, what do I have to do to have this functionality?
  // I'm researching right now, but I still haven't found anything.
  // That's why I'm asking it here =(

  // this works:
  def printValues(genericInputThatHasMap: Seq[Int]) {
    genericInputThatHasMap.map {
      value => println(value)
    }
  }

Thanks in advance! Cheers!

  • 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-16T03:04:21+00:00Added an answer on June 16, 2026 at 3:04 am

    First for a quick note about map and foreach. If you’re only interested in performing an operation with a side effect (e.g., printing to standard output or a file, etc.) on each item in your collection, use foreach. If you’re interested in creating a new collection by transforming each element in your old one, use map. When you write xs.map(println), you will in fact print all the elements of the collection, but you’ll also get back a (completely useless) collection of units, and will also potentially confuse future readers of your code—including yourself—who expect foreach to be used in a situation like this.

    Now on to your problem. You’ve run into what is in my opinion one of the ugliest warts of the Scala standard library—the fact that methods named map and foreach (and flatMap) get magical treatment at the language level that has nothing to do with a specific type that defines them. For example, I can write this:

    case class Foo(n: Int) {
      def foreach(f: Int => Unit) {
        (0 until n) foreach f
      }
    }
    

    And use it in a for loop like this, simply because I’ve named my method foreach:

    for (i <- Foo(10)) println(i)
    

    You can use structural types to do something similar in your own code:

    def printValues(xs: { def foreach(f: (Int) => Unit): Unit }) {
      xs foreach println
    }
    

    Here any xs with an appropriately typed foreach method—for example an Option[Int] or a List[Int]—will compile and work as expected.

    Structural types get a lot messier when you’re trying to work with map or flatMap though, and are unsatisfying in other ways—they impose some ugly overhead due to their use of runtime reflection, for example. They actually have to be explicitly enabled in Scala 2.10 to avoid warnings for these reasons.

    As senia’s answer points out, the Scalaz library provides a much more coherent approach to the problem through the use of type classes like Monad. You wouldn’t want to use Monad, though, in a case like this: it’s a much more powerful abstraction than you need. You’d use Each to provide foreach, and Functor for map. For example, in Scalaz 7:

    import scalaz._, Scalaz._
    
    def printValues[F[_]: Each](xs: F[Int]) = xs foreach println
    

    Or:

    def incremented[F[_]: Functor](xs: F[Int]) = xs map (_ + 1)
    

    To summarize, you can do what you want in a standard, idiomatic, but arguably ugly way with structural types, or you can use Scalaz to get a cleaner solution, but at the cost of a new dependency.

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

Sidebar

Related Questions

Problem: Been struggling to get my code to load external shaders and it is
Problem: I am trying to build a recursive tree using a function and data
I have a problem with JNI that has taken me all day and will
hello i have problem with the following code that is on the head of
Problem: I have a table that prints out vertical but I would like it
Problem in MATLAB Code for solving desired 'n' number of simultaneous equations of the
Problem: I have a method that creates a list from the parsed ArrayList. I
I have run into a rather weird little problem. In the following code I
I have a simple yet hard problem here that is driving me nuts ...
I'm trying to add some strings, images and tables into my pdf file (there

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.