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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:42:50+00:00 2026-05-23T20:42:50+00:00

I’m currently working on implementing my own Trie in Scala (for learning/hobby purposes), and

  • 0

I’m currently working on implementing my own Trie in Scala (for learning/hobby purposes), and I’m trying to keep it generic (so that it can store anything Iterable, not just Strings). My class signature looks like

class Trie[Item <% Iterable[Part], Part](items: Item*) extends Set[Item]

I already have contains, += and -= implemented (it’s extending the mutable version of Set), but I’m having some trouble with the iterator. My current approach has me scratching my head looking for a graceful implementation. I have a way to iterate over all of the TrieNodes, and give off only the ones that are marked as a “valid ending”. From there I plan to follow parent links to get the individual parts. (e.g. “hello” in the tree would be stored as an ‘o’ node marked as an ending, with parent ‘l’ -> ‘l’ -> ‘e’ -> ‘h’)

Now my problem. Since I’m trying to keep things generic I have no way to reconstruct the “Item” from its “Parts.” So my question to the people of SO is what would be the most graceful way to handle this? Should I add a reconstruction function to the constructor arguments? Should Item be bounded differently to enforce the presence of the function? Or is it something else entirely?

  • 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-23T20:42:50+00:00Added an answer on May 23, 2026 at 8:42 pm

    There is an implied relationship between Item and Part. At the minimum you need to decompose an Item into Part objects and to reconstruct you need to do the reverse.

    So taking "hello": String, you need to have f("hello") return ('h': Char, "ello": String) and you need the inverse function g('h', "ello") return "hello".

    So any two types with two such functions will do as long as some rules are followed. I think the rules are easy to intuit. It’s more or less how you decompose a list recursively using head and tail and rebuild it using ::

    You could use a context bound to provide these functions for the usual type.

    (edit)

    Actually I can’t really use a context bound because there are two type parameters, but this is what I had in mind:

    trait R[Item, Part] {
      def decompose(item: Item): (Part, Item)
      def recompose(item: Item, part: Part): Item
      def empty: Item
    }
    
    class NotATrie[Item, Part](item: Item)(implicit rel: R[Item, Part]) {
      val brokenUp = {
        def f(i: Item, acc: List[Part] = Nil): List[Part] = {
          if (i == rel.empty) acc
          else {
            val (head, tail) = rel.decompose(i)
            f(tail, head :: acc)
          }
        }
        f(item)
      }
    
      def rebuilt =  (rel.empty /: brokenUp)( (acc, el) => rel.recompose(acc, el) )
    }
    

    Then we provide some implicit objects:

    implicit object string2R extends R[String, Char] {
      def decompose(item: String): (Char, String) = (item.head, item.tail)
      def recompose(item: String, part: Char): String = part + item
      def empty: String = ""
    }
    
    implicit object string2RAlt extends R[String, Int] {
      def decompose(item: String): (Int, String) = {
        val cp = item.codePointAt(0)
        val index = Character.charCount(cp)
        (cp, item.substring(index))
      }
      def recompose(item: String, part: Int): String = 
        new String(Character.toChars(part)) + item
      def empty: String = ""
    }
    
    val nat1 = new NotATrie[String, Char]("hello")
    nat1.brokenUp  // List(o, l, l, e, h)
    nat1.rebuilt   // hello
    
    val nat2 = new NotATrie[String, Int]("hello\ud834\udd1e")
    nat2.brokenUp // List(119070, 111, 108, 108, 101, 104)
    nat2.rebuilt  // hello
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.