2016年6月30日 星期四

[Scala IA] The Basics : Ch2. Getting Started - Working with Array and List

Working with Array and List 
Chapter 4 is dedicated to data structures, but until then I’ll introduce List and Array so you can start writing useful Scala scripts. In Scala, array is an instance of the scala.Array class and is similar to the Java array: 
scala> val array = new Array[String](3)
array: Array[String] = Array(null, null, null)

scala> array(0) = "This"

scala> array(1) = "is"

scala> array(2) = "mutable"

scala> array
res3: Array[String] = Array(This, is, mutable)

Always remember that in Scala the type information or parameterization is provided using square brackets. The type parameterization is a way to configure an instance with type information when creating the instance. Now iterating through each element in the array is easy; call the foreach method: 
scala> array.foreach(e => println(e))
This
is
mutable


scala> array.foreach(println)
This
is
mutable

You’re passing a function literal to the foreach method to print all the elements in the array. There are other useful methods defined for Array objects; for a complete list look in Scaladoc for scala.collection.mutable.ArrayLike. The most obvious question in your mind right now is probably why we have to check ArrayLike, which is a different class than the Array used to check methods that are available for an Array instance in Scala. The answer is Predef. ScalaPredef provides additional array functionality dynamically using ArrayLike when you create an instance of an Array. Again, Predef is a great place to start to understand the Scala Library. 
NOTE 
Predef implicitly converts Array to scala.collection.mutable.ArrayOpsArrayOps is the subclass of ArrayLike, so ArrayLike is more like the interface for all the additional methods available to Array type collections.

When writing Scala scripts, you sometimes have to take command-like arguments. You can do that implicitly as a val type variable called args. In the following example you’ll create a Scala script that takes input from a user and prints it to the console: 
  1. args.foreach(println)  
Open your favorite editor and save this line in a file called myfirstScript.scala. Open a command prompt to the location where the file is saved and run the following command: 
# scala myfirstScript.scala my first script
my
first
script

The Array is a mutable data structure; by adding each element to the array, you’re mutating the array instance and producing a side effect. In functional programming, methods should not have side effects. The only effect a method is allowed to have is to compute a value and return that value without mutating the instance. An immutable and more functional alternative to a sequence of objects like Array is List. In Scala, List is immutable and makes functional-style programming easy. Creating a list of elements is as easy as the following: 
scala> val myList = List("This", "is", "immutable")
myList: List[java.lang.String] = List(This, is, immutable)

The List is shorthand for scala.collection.immutable.List, and again Predef automatically makes it available to you: 
scala> val myList = scala.collection.immutable.List("This", "is", "immutable")
myList: List[String] = List(This, is, immutable)

scala> myList.getClass
res0: Class[_ <: b="" list="" nbsp="" tring="">class scala.collection.immutable.$colon$colon
 
Most of us are used to mutable collections where, when we add or remove elements, the collection instance expands or shrinks (mutates), but immutable collections never change. Instead, adding or removing an element from an immutable collection creates a new modified collection instance without modifying the existing one. The following example adds an element to an existing List
scala> val oldList = List(1,2)
oldList: List[Int] = List(1, 2)

scala> val newList = 3 :: oldList
newList: List[Int] = List(3, 1, 2)

scala> oldList
res2: List[Int] = List(1, 2)

In this example you’re adding 3 to an existing List containing elements 1 and 2 using the :: method. The job of the :: method is to create a new List with all the existing elements plus the new element added at the front of the List. To add at the end of the List, invoke the :+ method: 
scala> val newList = oldList :+ 3
newList: List[Int] = List(1, 2, 3)

Scala provides a special object called Nil to represent an empty List, and you can use it to create new lists easily: 
scala> val myList = "This" :: "is" :: "immutable" :: Nil
myList: List[String] = List(This, is, immutable)

In this example you’re using a new instance of List every time you add an element. To delete an element from a List, you could use the - method, but it’s deprecated. A better way would be to use the filterNot method, which takes a predicate and selects all the elements that don’t satisfy the predicate. To delete 3 from the newList, you can do something like the following: 
scala> val afterDel = newList.filterNot(_ == 3)
afterDel: List[Int] = List(1, 2)

You’ll delve deeper into Scala collections in chapter 4, section 4.3, but for now you know enough to play with Scala and script a few things. In the meantime I encourage you to look into methods defined for List and play with them.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...