
RunWith allows efficient imperative-style combining of partial functions with conditionally applied actionsįor non-literal partial function classes with nontrivial isDefinedAt method it is recommended to override applyOrElse with custom implementation that avoids double isDefinedAt evaluation. Lift and unlift do not evaluate source functions twice on each invocation This makes applyOrElse the basis for the efficient implementation for many operations and scenarios, such as:Ĭombining partial functions into orElse/ andThen chains does not lead to excessive apply/ isDefinedAt evaluation

For all partial function literals the compiler generates an applyOrElse implementation which avoids double evaluation of pattern matchers and guards. Note that expression pf.applyOrElse(x, default) is equivalent to if(pf isDefinedAt x) pf(x) else default(x)Įxcept that applyOrElse method can be implemented more efficiently. Applies fallback function where this partial function is not defined. Linear Supertypes DefaultSerializable, java.io.Serializable, StrictOptimizedSeqOps], StrictOptimizedLinearSeqOps], collection.StrictOptimizedSeqOps], StrictOptimizedIterableOps], LinearSeq, LinearSeqOps List, List], collection.LinearSeq, collection.LinearSeqOps List, List], AbstractSeq, Seq, SeqOps List, List], Iterable, collection.AbstractSeq, collection.Seq, Equals, collection.SeqOps List, List], PartialFunction, ( Int) => A, AbstractIterable, collection.Iterable, IterableFactoryDefaults List], IterableOps List, List], IterableOnceOps List, List], IterableOnce, AnyRef, Any Known Subclasses ::, NilĪpplies this partial function to the given argument when it is contained in the function domain. "Scala's Collection Library overview" section on Lists for more information. structural sharing is lost after serialization/deserialization. However, note that objects having multiple references into the same functional list (that is, objects that rely on structural sharing), will be serialized and deserialized with multiple lists, one for each reference to it. The functional list is characterized by persistence and structural sharing, thus offering considerable performance and space consumption benefits in some scenarios if used correctly. Println("There don't seem to be any week days.") Println("The first day of the week is: " + firstDay) Val days = List("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

// Make a list via the companion object factory.Val shorter = mainList.tail // costs nothing as it uses the same 2::1::Nil instances as mainList Annotations SerialVersionUID () Source List.scala Val with42 = 42 :: mainList // also re-uses mainList, cost one :: instance Val with4 = 4 :: mainList // re-uses mainList, costs one :: instance This means that many operations are either zero- or constant-memory cost.

Space: List implements structural sharing of the tail list. This includes the index-based lookup of elements, length, append and reverse. Most other operations are O(n) on the number of elements in the list. Time: List has O(1) prepend and head/tail access. If you need another access pattern, for example, random access or FIFO, consider using a collection more suited to this than List. This class is optimal for last-in-first-out (LIFO), stack-like access patterns. This class comes with two implementing case classes scala.Nil and scala.:: that implement the abstract members isEmpty, head and tail. A class for immutable linked lists representing ordered collections of elements of type A.
