Search code examples
listscalasublistdivide

Create sublists of scala list


Input: List(1,2,3)

Expected Output 1: List(1), List(1, 2), List(1,2,3)

Expected Output 2: List(1, 1, 2, 1, 2, 3)


Solution

  • Expected Output 1

    val l = List(1,2,3)
    l.map( x => (l.head to x).toList)
    
    

    Expected Output 2

    l.flatMap{ x => 
       l.head to x 
    }