WebFeb 10, 2016 · 1 - Declare your List val myCharList: List [Char] = List (' (',')',' (',')') 2 - Define your method def printList ( chars: List [Char] ): Boolean = { if ( chars.isEmpty ) true //every item of the list has been printed else { println ( chars.head ) printList ( chars.tail ) } } 3 - Call the … WebFeb 28, 2024 · Lambda Expression in Scala. Lambda Expression refers to an expression that uses an anonymous function instead of variable or value. Lambda expressions are more convenient when we have a simple function to be used in one place. These expressions are faster and more expressive than defining a whole function. We can make our lambda …
Find elements in a list that are not in the second list (in scala)
WebApr 16, 2015 · scala> val l = List ("a", "b", "c") scala> l.lift (1) Some ("b") scala> l.lift (5) None Whenever you're performing an operation that may fail in this way it's great to use an Option and get the type system to help make sure you are handling the case where the element doesn't exist. Explanation: WebJan 25, 2011 · scala> val data =List ( ("2001",13.1), ("2009",3.1), ("2004",24.0), ("2011",1.11)) data: List [ (java.lang.String, Double)] = List ( (2001,13.1), (2009,3.1), (2004,24.0), (2011,1.11)) scala> data.foreach (x => println (x._1+" "+x._2)) 2001 13.1 2009 3.1 2004 24.0 2011 1.11 Share Improve this answer Follow answered Jan 25, 2011 at 12:17 fox tv albany ny
Accessing the next element in list to compare in Scala
WebIdiom #7 Iterate over list indexes and values. Print each index i with its value x from an array-like collection items WebNov 15, 2012 · scala.List is immutable, meaning you cannot update it in place. If you want to create a copy of your List which contains the updated mapping, you can do the following: val updated = l2.updated ( 2, 55 ) There are mutable ordered sequence types as well, in scala.collection.mutable, such as Buffer types which seem more like what you want. WebFeb 11, 2013 · 2 Answers Sorted by: 45 You can use pattern matching: val hd::tail = List (1,2,3,4,5) //hd: Int = 1 //tail: List [Int] = List (2, 3, 4, 5) Or just .head/.tail methods: val hd = foo.head // hd: Int = 1 val hdOpt = foo.headOption // hd: Option [Int] = Some (1) val tl = foo.tail // tl: List [Int] = List (2, 3, 4) Share Improve this answer Follow fox tv akgün