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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:49:58+00:00 2026-05-27T15:49:58+00:00

I am trying to understand mapConserve, which is said to be Like xs map

  • 0

I am trying to understand mapConserve, which is said to be “Like xs map f, but returns xs unchanged if function f maps all elements to themselves,” from List. Yet, it is giving out error.

def map [B] (f: (A) ⇒ B): List[B]
def mapConserve (f: (A) ⇒ A): List[A]
def mapConserve [B >: A <: AnyRef] (f: (A) ⇒ B): List[B]


scala> list map (x=>x)
res105: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> list mapConserve (x=>x)
<console>:12: error: inferred type arguments [Int] do not conform to method mapConserve's type parameter bounds [B >: Int <: AnyRef]
              list mapConserve (x=>x)
                   ^

The mapConserve code should satisfy the (A) => A function. If not, it still should satisfy the (A) => B function, since type A can be subtype and supertype of itself. Please enlighten me the purpose of mapConserve and the error.

  • 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-27T15:49:59+00:00Added an answer on May 27, 2026 at 3:49 pm

    Actually, mapConserve is defined as

    def mapConserve[A <: AnyRef](xs: List[A])(f: A => A): List[A]
    def mapConserve[B >: A <: AnyRef](f: A => B): List[B]
    

    so A should be a subtype of AnyRef. Int is a subtype of AnyVal, that brings an error.

    scala> val l = List("foo", "bar", "baz")
    l: List[java.lang.String] = List(foo, bar, baz)
    
    scala> l.mapConserve(_.toUpperCase)
    res4: List[java.lang.String] = List(FOO, BAR, BAZ)
    
    scala> l.mapConserve(identity)
    res5: List[java.lang.String] = List(foo, bar, baz)
    

    Update:

    The only difference between map and mapConserve, as it is described in the scaladoc:

    Builds a new list by applying a function to all elements of this list.
    Like xs map f, but returns xs unchanged if function f maps all elements to themselves (as determined by eq).

    scala> val xs = List.fill(1000000)("foo")
    xs: List[java.lang.String] = List(foo, foo,...)
    
    scala> xs.map(identity) eq xs
    res48: Boolean = false
    
    scala> xs.mapConserve(identity) eq xs
    res49: Boolean = true
    

    And xs mapConserve identity is about five times faster in my simple benchmark.

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

Sidebar

Related Questions

Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand the math of this code snippet. A token is provided which
Trying to understand how to link a function that is defined in a struct,
I am trying understand how multi queries work in mysqli. But I confess that
Trying to understand the relationship between UIView and CALayer. I read Apple documentation but
I was trying to understand how mutexes work. Did a lot of Googling but
Trying to understand the use of verifySet etc... but unless I do a workaround
Trying to understand this MSDN sample but I'm confused about these lines: IAsyncResult result
I am trying understand why the .wrap() function in my basic table of contents
Trying to understand what the pointer to function actually represent? Is it the address

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.