Programming in Scala (Second Edition) reading notes 4

Source: Internet
Author: User

The 1.Tuple and list are immutable objects (Immutalbe), but the elements of a tuple can be of different types

Val mytuple = ("A", 1, "Park", 3.13359) println (mytuple._1)//A println (mytuple._2)//1 println (Mytuple._3) Park

Creating a tuple simply put the element in parentheses and create an instance of the Tuple4 class, as defined by Tuple4 in Scala.

/** a tuple of 4 elements; the canonical representation of a  [[scala. product4]]. * *   @constructor   Create a new tuple with  4 elements. note that it is more idiomatic to create a  tuple4 via  ' (t1, t2, t3, t4)  *   @param   _1    Element 1 of this Tuple4 *   @param   _2    element 2 of this tuple4 *   @param   _3   element  3 of this Tuple4 *   @param   _4   element 4  of this tuple4 */@deprecatedInheritance ("tuples will be made final  in a future version. ", " 2.11.0 ") Case class tuple4[+t1, +t2, +t3,  +T4] (_1: T1, _2: T2, _3: T3, _4: T4)   extends Product4[T1,  T2, t3, t4]{  override def tostring ()  =  "("  + _1 +   ","  + _2 +  ","  + _3 +  ","  + _4 +  ")"    }

A tuple like 5 elements is an instance of Tuple5, and a tuple of 6 elements is an instance of Tuple6

So you can explain why you use _1, _2, _3, and so on to refer to elements in a tuple.

The type parameter in the TUPLE4 definition is preceded by a + sign, which represents the Covarience parameter and will be explained in depth later

2. Set has two types, immutable and mutable, with the same name, creating immutable set objects by default

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/55/46/wKiom1SJYMeBdQOaAAF8Nvro-Is758.jpg "title=" 2014-12-11 17_15_07-programming in Scala, 2nd edition.pdf-foxit reader.png "alt=" wkiom1sjymebdqoaaaf8nvro-is758.jpg "/ >

var jetSet = Set ("Bench", "Audi", "Tesilla") JetSet + = "Luhu" println (Jetset.contains ("Bicycle"))


To add a new element to a set, you call + on the set, passing in the new element. Both mutable and immutable sets offer A + method, but their behavior

Differs. Whereas a mutable set would add the element to itself, an immutable

Set would create and return a new set with the element added

For immutable set, the + operation creates a new set

For Mmutalbe set, the + operation adds an element to its own


3. Map is similar to set and there are two implementations of Immuatlbe and mutable

4. Reading files

Import Scala.io.Sourceobject Testmain {def main (args:array[string]) {var lines = Source.fromfile ("E:/testread.txt"). G Etlines Lines.foreach (println)}}


Programming in Scala (Second Edition) reading notes 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.