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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:34:02+00:00 2026-05-23T08:34:02+00:00

I was hoping someone could provide me with some basic code help in Scala.

  • 0

I was hoping someone could provide me with some basic code help in Scala. I’ve written some demo code in Python.

Consider a list of elements, where an element can hold either an integer or a list of other elements. I’d like to recursively examine this structure and modify it while keeping the overall structure.

To represent this in python, I’ve made each ‘element’ a dictionary with one key (‘i’ for item). The value corresponding to that key is either an int, or a list of similar dicts. Thus,

lst = [{'i': 1}, {'i': 2}, {'i': [{'i': 5}, {'i': 6}]}, {'i': 3}]

def recurse(x):
  if isinstance(x, list):
    return [recurse(a) for a in x]
  else:
    if isinstance(x['i'], list):
      return dict(i=[recurse(a) for a in x['i']])
    else:
      return dict(i=(x['i'] + 1))

print "Input:"
for i in lst:
    print i
print "\nResult:\n%s" % recurse(lst)

>>>
Input:
{'i': 1}
{'i': 2}
{'i': [{'i': 5}, {'i': 6}]}
{'i': 3}

Result:    
[{'i': 2}, {'i': 3}, {'i': [{'i': 6}, {'i': 7}]}, {'i': 4}]

I understand it’s a bit of a weird way to go about doing things, but the data I have been provided is structured like that. I think my issue is that python lets you return different types from the same function, while I don’t believe Scala does.

Also for the record, the Scala elements are represented as Elem(4), or Elem(List(Elem(3)…, so I assume pattern matching can come into it somewhat.

  • 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-23T08:34:02+00:00Added an answer on May 23, 2026 at 8:34 am

    I would rather not call that a List of List, as that does not tell what those lists contains. The structure is a tree, more precisely a leafy tree, where there are data only in the leaves. That would be :

    sealed trait Tree[+A]
    case class Node[+A](children: Tree[A]*) extends Tree[A]
    case class Leaf[+A](value: A) extends Tree[A]
    

    then add a method map to apply a function on each value in the tree

    sealed trait Tree[+A] {
      def map[B](f: A => B): Tree[B]
    }
    case class Node[+A](children: Tree[A]*) extends Tree[A] {
      def map[B](f : A => B) = Node(children.map(_.map(f)): _*)
    }
    case class Leaf[+A](value: A) extends Tree[A] {
      def map[B](f: A => B) = Leaf(f(value))
    }
    

    Then your input is :

    val input = Node(Leaf(1), Leaf(2), Node(Leaf(5), Leaf(6)), Leaf(3))
    

    And if you call input.map(_ + 1) you get your output

    The result display is somewhat ugly because of the varargs Tree[A]*. You may improve by adding in Node override def toString = "Node(" + children.mkString(", ") + ")"

    You may prefer the method in one place only, either outside the classes, or directly in Tree. Here as a method in Tree

    def map[B](f: A => B): Tree[B] = this match {
      case Node(children @ _*) => Node(children.map(_.map(f)): _*)
      case Leaf(v) => Leaf(f(v))
    }
    

    Working the untyped way as in Python is not very scala like but may be done.

    def recurse(x: Any) : Any = x match {
      case list : List[_] => list.map(recurse(_))
      case value : Int => value + 1
    }
    

    (putting values directly in the list. Your map (dictionary) with key “i” complicates it and force accepting a compiler warning, as we would have to force a cast that could not be checked, namely that a map accepts string as keys : case map: Map[String, _])


    Using case Elem(content: Any) sounds to me as giving no extra safety compared to putting values directly in List, while being much more verbose, and none of the safety and clarity of calling it a tree and distinguishing nodes and leaves without being noticeably terser.

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

Sidebar

Related Questions

Im using the .NET framework 1.1 and Im hoping someone could help me implement
I was hoping someone could help me figure out what to do about a
I was hoping someone could help me writing a regex for c++ that matches
I was hoping someone could help me figure out why my application keeps timing
I was hoping someone could help me understand the syntax of blocks when used
I was hoping someone could shed light on IE 6 > image rotation code.
I was hoping someone could help me with figuring out how to POST to
Hoping that someone here will be able to provide some mysql advice... I am
I was hoping someone could help with a MS Word Macro. Basically, I have
I was hoping someone could help me with a question I've come up on.

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.