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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:06:50+00:00 2026-05-26T04:06:50+00:00

I have a nested map m which is like: m = Map(email -> a@b.com,

  • 0

I have a nested map m which is like:

m = Map("email" -> "a@b.com", "background" -> Map("language" -> "english"))

I have an array arr = Array("background","language")

How do I foldLeft/reduce the array and find the string “english” from the map. I tried this:

arr.foldLeft(m) { (acc,x) => acc.get(x) }

But I get this error:

<console>:10: error: type mismatch;
 found   : Option[java.lang.Object]
 required: scala.collection.immutable.Map[java.lang.String,java.lang.Object]
       arr.foldLeft(m) { (acc,x) => acc.get(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-05-26T04:06:51+00:00Added an answer on May 26, 2026 at 4:06 am

    You should pay attention to types. Here, you start with m : Map[String, Any] as your acc. You combine with a string x and calls get, which returns an Option[Object]. To continue, you must check that there is a value, check whether this value is a Map, cast (unchecked because of type erasure, hence dangerous).

    I believe the fault is in the that the type of your structure, Map[String, Any] represents what you have rather poorly.

    Suppose you do instead

    sealed trait Tree
    case class Node(items: Map[String, Tree]) extends Tree
    case class Leaf(s: String) extends Tree
    

    You may add some helpers to make declaring a Tree easy

    object Tree {
      implicit def fromString(s: String) = Leaf(s)
      implicit def fromNamedString(nameAndValue: (String, String)) 
        = (nameAndValue._1, Leaf(nameAndValue._2))
    }
    object Node {
      def apply(items: (String, Tree)*) : Node = Node(Map(items: _*))
    }
    

    Then declaring the tree is just as easy as your first version, but the type is much more precise

    m = Node("email" -> "a@b.com", "background" -> Node("language" -> "english"))
    

    You can then add methods, for instance in trait Tree

      def get(path: String*) : Option[Tree] = {
        if (path.isEmpty) Some(this)
        else this match {
          case Leaf(_) => None
          case Node(map) => map.get(path.head).flatMap(_.get(path.tail: _*))
        }
      }
      def getLeaf(path: String*): Option[String] 
        = get(path: _*).collect{case Leaf(s) =>s}
    

    Or if you would rather do it with a fold

      def get(path: String*) = path.foldLeft[Option[Tree]](Some(this)) {
        case (Some(Node(map)), p) => map.get(p)
        case _ => None
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested resource in my routes.rb like this: map.resources :users, :only =>
I have the following code which I use to map a nested list in
If you have a nested resource defined like this: map.resources :magazines, :has_many => :ads.
I have a nested map: Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();
I have a number of tables, which have nested tables. I using jQuery to
I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]],
I'm using Rails 2. I have resources nested like this: - university_categories - universities
I have nested containers std::map<int, std::map<T, U> > and want to populate them properly,
I have a nested set within a map within a map that I am
I have inherited code which contains static nested classes as: public class Foo {

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.