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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:13:37+00:00 2026-06-10T09:13:37+00:00

Why is this snippet not printing out: You successfully implemented the function Details: Why

  • 0

Why is this snippet not printing out: “You successfully implemented the function”

Details:

Why is the type of val actual seeming to be of type List[Either[List[Int], Int]], when I am even saying to the compiler explicitly that actual’s type should be of type List[Int]?

// Flatten a nested list structure

def flatten[T](list: List[Either[List[T], T]]): List[T] = list flatMap {
    // TODO: Implement
    case list: List[T]  => list
    case element: T     => List(element)
}

implicit def ElementToEitherLeft[T](obj: T) = Left(obj)
implicit def ElementToEitherRight[T](obj: T) = Right(obj)

val list: List[Either[List[Int], Int]] = List(List(1, 1), 2, List(3, 5))
val actual: List[Int] = flatten[Int](list)
val expected = List(1, 1, 2, 3, 5)
if (actual == expected)     print("You successfully implemented the function")
else                        print("Unfortunatly, that's not quite rigth yet")
  • 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-10T09:13:39+00:00Added an answer on June 10, 2026 at 9:13 am

    When you compiled your flatten you should have seen a warning like this:

    warning: there were 2 unchecked warnings; re-run with -unchecked for details
    

    If you’d then compiled with with -unchecked, you’d have seen this:

    <console>:9: warning: non variable type-argument T in type pattern List[T] is unchecked since it is eliminated by erasure
               case list: List[T]  => list
                          ^
    <console>:10: warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure
               case element: T     => List(element)
    

    In short, flatMap isn’t going to unwrap your Either items for you, and what you’ve written only compiles because of some unpleasant facts about type erasure and pattern matching.

    Fortunately there’s an easy fix:

     def flatten[T](list: List[Either[List[T], T]]): List[T] = list flatMap {
       case Left(list)  => list
       case Right(item) => item :: Nil
     }
    

    Or, even better:

    def flatten[T](list: List[Either[List[T], T]]): List[T] =
      list.flatMap(_.fold(identity, List(_)))
    

    Either will work as you expect.

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

Sidebar

Related Questions

I need to bind a method into a function-callback, except this snippet is not
This snippet of code always parses the date into the current timezone, and not
this is my snippet , please help me why the con .open is not
This works: var.replace(/[^0-9]+/g, ''); That simple snippet will replace anything that is not a
Could someone kindly point me why this snippet is not compiled: my $crond =
This code snippet taken from http://doc.qt.digia.com/qt/qhboxlayout.html#details is, barring some magic I'm not aware of,
Anybody has any idea why this snippet is NOT working in newest version of
Just don't know why this snippet is not correct. Calendar now = Calendar.getInstance(); DateFormat
I am printing out a list of college majors we offer, then within each
This snippet throws an NullPointerException due to the fact that its unboxed to a

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.