title: Scala insert into list at specific locations
body: <p>This is the problem that I did solve, however being a total imperative Scala noob, I feel I found something totally not elegant. Any ideas of improvement appreciated. </p>
  
  <pre><code>val l1 = 4 :: 1 :: 2 :: 3 :: 4 :: Nil // original list
  val insert = List(88,99) // list I want to insert on certain places
  
  // method that finds all indexes of a particular element in a particular list
  def indexesOf(element:Any, inList:List[Any]) = {
          var indexes = List[Int]()
          for(i &lt;- 0 until inList.length) {
                  if(inList(i) == element) indexes = indexes :+ i
          }
          indexes
  }
  
  
  var indexes = indexesOf(4, l1) // get indexes where 4 appears in the original list
  
  println(indexes)
  
  var result = List[Any]()
  
  // iterate through indexes and insert in front
  for(i &lt;- 0 until indexes.length) {
          var prev = if(i == 0) 0 else indexes(i-1)
          result = result ::: l1.slice(prev, indexes(i)) ::: insert
  }
  result = result ::: l1.drop(indexes.last) // append the last bit from original list
  
  println(result)
  </code></pre>
  
  <p>I was thinking more elegant solution would be achievable with something like this, but that's just pure speculation.</p>
  
  <pre><code>var final:List[Any] = (0 /: indexes) {(final, i) =&gt; final ::: ins ::: l1.slice(i, indexes(i))
  </code></pre>