Skip to content

Instantly share code, notes, and snippets.

@matesio
Created January 31, 2018 21:34
Show Gist options
  • Save matesio/73a46d2e60cc19fff2ebc555ddbc2b20 to your computer and use it in GitHub Desktop.
Save matesio/73a46d2e60cc19fff2ebc555ddbc2b20 to your computer and use it in GitHub Desktop.
Scala Lists Basics worksheet
/*
*intellij idea, file-> new -> scala worksheet
*/
//Lisp-style approach for creating list.
//val myList = "Element1" :: "Element2" :: "Element3" :: "Element4":: Nil
:: "Element3" :: "Element4"
//Java-style approach
val myList = List("Element1", "Element2", "Element3", "Element4")
//all element to uppercase.
val mapped = myList.map( s => s.toUpperCase )
//iterating and printing each element in the list
mapped.foreach(println)
//prepending element to the list
val y = "Element0" :: myList
/*
*output-
*y: List[String] = List(Element0, Element1, Element2, Element3, Element4)
*/
val myList1 = List("Element5")
//Merging lists.
val merged = myList ::: myList1
/*
*output-
*merged: List[String] = List(Element1, Element2, Element3, Element4, Element5)
*/
//List.concat can also be used.
//val merged = List.concat(myList,myList1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment