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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:37:37+00:00 2026-05-25T00:37:37+00:00

A while back this was asked and answered on the Scala mailing list: Kevin:

  • 0

A while back this was asked and answered on the Scala mailing list:

Kevin:

Given some nested structure: List[List[...List[T]]]
what’s the best (preferably type-safe) way to flatten it to a List[T]

Jesper:

A combination of implicits and default arguments works:

case class Flat[T, U](fn : T => List[U]) 

implicit def recFlattenFn[T, U](implicit f : Flat[T, U] = Flat((l : T) 
=> List(l))) = 
   Flat((l : List[T]) => l.flatMap(f.fn)) 

def recFlatten[T, U](l : List[T])(implicit f : Flat[List[T], U]) = f.fn(l) 

Examples:

scala> recFlatten(List(1, 2, 3)) 
res0: List[Int] = List(1, 2, 3) 

scala> recFlatten(List(List(1, 2, 3), List(4, 5))) 
res1: List[Int] = List(1, 2, 3, 4, 5) 

scala> recFlatten(List(List(List(1, 2, 3), List(4, 5)), List(List(6, 7)))) 
res2: List[Int] = List(1, 2, 3, 4, 5, 6, 7) 

I have been looking at this code for a while. I cannot figure out how it works. There seems to be some recursion involved… Can anybody shed some light? Are there other examples of this pattern and does it have a name?

  • 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-25T00:37:38+00:00Added an answer on May 25, 2026 at 12:37 am

    Oh wow, this is an old one! I’ll start by cleaning up the code a bit and pulling it into line with current idiomatic conventions:

    case class Flat[T, U](fn: T => List[U]) 
    
    implicit def recFlattenFn[T, U](
      implicit f: Flat[T, U] = Flat((xs: T) => List(xs))
    ) = Flat((xs: List[T]) => xs flatMap f.fn)
    
    def recFlatten[T, U](xs: List[T3])(implicit f: Flat[List[T], U]) = f fn xs 
    

    Then, without further ado, break down the code. First, we have our Flat class:

    case class Flat[T, U](fn: T => List[U]) 
    

    This is nothing more than a named wrapper for the function T => List[U], a function that will build a List[U] when given an instance of type T. Note that T here could also be a List[U], or a U, or a List[List[List[U]]], etc. Normally, such a function could be directly specified as the type of a parameter. But we’re going to be using this one in implicits, so the named wrapper avoids any risk of an implicit conflict.

    Then, working backwards from recFlatten:

    def recFlatten[T, U](xs: List[T])(implicit f: Flat[List[T], U]) = f fn xs 
    

    This method will take xs (a List[T]) and convert it to a U. To achieve this, it locates an implicit instance of Flat[T,U] and invokes the enclosed function, fn

    Then, the real magic:

    implicit def recFlattenFn[T, U](
      implicit f: Flat[T, U] = Flat((xs: T) => List(xs))
    ) = Flat((xs: List[T]) => xs flatMap f.fn)
    

    This satisfies the implicit parameter required by recFlatten, it also takes another implicit paramater. Most crucially:

    • recFlattenFn can act as its own implicit parameter
    • it returns a Flat[List[X], X], so recFlattenFn will only be implicitly resolved as a Flat[T,U] if T is a List
    • the implicit f can fallback to a default value if implicit resolution fails (i.e. T is NOT a List)

    Perhaps this is best understood in the context of one of the examples:

    recFlatten(List(List(1, 2, 3), List(4, 5))) 
    
    • The type T is inferred as List[List[Int]]
    • implicit lookup is attempted for a `Flat[List[List[Int]], U]
    • this is matched by a recursively defined recFlattenFn

    Broadly speaking:

    recFlattenFn[List[List[Int]], U] ( f =
      recFlattenFn[List[Int], U] ( f =
        Flat[Int,U]((xs: T) => List(xs)) //default value
      )
    )
    

    Note that recFlattenFn will only match an implicit search for a Flat[List[X], X] and the type params [Int,_] fail this match because Int is not a List. This is what triggers the fallback to the default value.

    Type inference also works backwards up that structure, resolving the U param at each level of recursion:

    recFlattenFn[List[List[Int]], Int] ( f =
      recFlattenFn[List[Int], Int] ( f =
        Flat[Int,Int]((xs: T) => List(xs)) //default value
      )
    )
    

    Which is just a nesting of Flat instances, each one (except the innermost) performing a flatMap operation to unroll one level of the nested List structure. The innermost Flat simply wraps all the individual elements back up in a single List.

    Q.E.D.

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

Sidebar

Related Questions

I asked this question a while back and even though I put up several
This is related to this question I asked a while back. The end game
This is continuation to the question I already asked a while back. I've been
I asked this question a while back but now I'm looking to implement an
This question is related to this question I asked a little while back. The
I previously asked a similar question on this topic a while back and got
So basically I asked this question while back: The query contains references to items
Similar to what I asked in this question a while back about getting the
I little while back I posted this question . I have updated that question
I have seen the solution to this a while back, but can't seem to

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.