Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created July 31, 2015 11:49
Show Gist options
  • Save fancellu/c2ed64da112523ef7547 to your computer and use it in GitHub Desktop.
Save fancellu/c2ed64da112523ef7547 to your computer and use it in GitHub Desktop.
InsertAt.scala Alternative for http://aperiodic.net/phil/scala/s-99/p21.scala
Alternative for http://aperiodic.net/phil/scala/s-99/p21.scala
def insertAt[A](e: A, n: Int, ls: List[A]) =
ls.patch(n, List(e), 0)
insertAt('new,1,li) //> res1: List[Symbol] = List('a, 'new, 'b, 'c, 'd)
insertAt('new,0,li) //> res2: List[Symbol] = List('new, 'a, 'b, 'c, 'd)
insertAt('new,99,li) //> res3: List[Symbol] = List('a, 'b, 'c, 'd, 'new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment