🗊 Презентация Scala. Lists

Нажмите для полного просмотра!
Scala. Lists, слайд №1 Scala. Lists, слайд №2 Scala. Lists, слайд №3 Scala. Lists, слайд №4 Scala. Lists, слайд №5 Scala. Lists, слайд №6 Scala. Lists, слайд №7 Scala. Lists, слайд №8 Scala. Lists, слайд №9 Scala. Lists, слайд №10 Scala. Lists, слайд №11 Scala. Lists, слайд №12 Scala. Lists, слайд №13 Scala. Lists, слайд №14 Scala. Lists, слайд №15 Scala. Lists, слайд №16 Scala. Lists, слайд №17 Scala. Lists, слайд №18 Scala. Lists, слайд №19 Scala. Lists, слайд №20

Вы можете ознакомиться и скачать презентацию на тему Scala. Lists. Доклад-сообщение содержит 20 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1


Lists
Описание слайда:
Lists

Слайд 2


Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the 37th element)...
Описание слайда:
Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the 37th element) very fast--O(1) Lists are composed of values linked together All access starts from the head (first element) and follows links Random access takes linear time

Слайд 3


Lists are immutable Lists, like Strings, are immutable Because all access is via the head, creating a “new” list is a fast operation
Описание слайда:
Lists are immutable Lists, like Strings, are immutable Because all access is via the head, creating a “new” list is a fast operation

Слайд 4


List operations Basic fast (constant time) operations list.head (or list head) returns the first element in the list list.tail (or list tail) returns...
Описание слайда:
List operations Basic fast (constant time) operations list.head (or list head) returns the first element in the list list.tail (or list tail) returns a list with the first element removed value :: list returns a list with value appended to the front list.isEmpty (or list isEmpty ) tests whether the list is empty Some slow (linear time) operations list(i) returns the ith element (starting from 0) of the list list.last (or list last) returns the last element in the list list.init (or list init) returns a list with the last element removed This involves making a complete copy of the list list.length (or list length) returns the number of elements in the list list.reverse (or list reverse) returns a new list with the elements in reverse order In practice, the slow operations are hardly ever needed

Слайд 5


Stepping through a list def printList1(myList: List[Any]) { for (i
Описание слайда:
Stepping through a list def printList1(myList: List[Any]) { for (i

Слайд 6


List construction with :: and Nil Lists are homogeneous: All elements have the same type However, scala> "abc" :: List(1, 2, 3) res15:...
Описание слайда:
List construction with :: and Nil Lists are homogeneous: All elements have the same type However, scala> "abc" :: List(1, 2, 3) res15: List[Any] = List(abc, 1, 2, 3) The newly-created list has a type which is the least upper bound An empty list has “nothing” in it scala> List() res16: List[Nothing] = List() The “name” of the empty list is Nil scala> Nil res17: scala.collection.immutable.Nil.type = List() Lists are built from Nil and the :: operator (which is right-associative) scala> 1 :: 2 :: 3 :: Nil res18: List[Int] = List(1, 2, 3) scala> 1 :: (2 :: (3 :: Nil)) res19: List[Int] = List(1, 2, 3)

Слайд 7


Basic recursion Recursion is when a method calls itself Here’s the basic formula for working with a list: if the list is empty return some initial...
Описание слайда:
Basic recursion Recursion is when a method calls itself Here’s the basic formula for working with a list: if the list is empty return some initial value (often an empty list) else process the head recur with the tail def printList2(myList: List[Any]) { if(! myList.isEmpty) { println(myList head) printList2(myList tail) } }

Слайд 8


Again, with pattern matching Here’s our same method again: def printList2(myList: List[Any]) { if(! myList.isEmpty) { println(myList head)...
Описание слайда:
Again, with pattern matching Here’s our same method again: def printList2(myList: List[Any]) { if(! myList.isEmpty) { println(myList head) printList2(myList tail) } } Here it is with pattern matching: def printList3(myList: List[Any]) { myList match { case h :: t => println(myList head) printList3(myList tail) case _ => } }

Слайд 9


map map applies a one-parameter function to every element of a List, returning a new List scala> List(1, 2, 3, 4) map (n => 10 * n) res0: List[Int] =...
Описание слайда:
map map applies a one-parameter function to every element of a List, returning a new List scala> List(1, 2, 3, 4) map (n => 10 * n) res0: List[Int] = List(10, 20, 30, 40) The result list doesn’t have to be of the same type scala> List(1, 2, 3, 4) map (n => n % 2 == 0) res1: List[Boolean] = List(false, true, false, true) Since an element of the list is the only parameter to the function, and it’s only used once, you can abbreviate the function scala> List(1, 2, 3, 4) map (10 * _ + 6) res2: List[Int] = List(16, 26, 36, 46) Of course, you don’t have to use a literal function; you can use any previously defined function (yours or Scala’s) scala> List(-1, 2, -3, 4) map (_ abs) res3: List[Int] = List(1, 2, 3, 4)

Слайд 10


flatMap flatten “flattens” a list (removes one level of nesting) scala> val nested = List(List(1, 2, 3), List(4, 5)) nested: List[List[Int]] =...
Описание слайда:
flatMap flatten “flattens” a list (removes one level of nesting) scala> val nested = List(List(1, 2, 3), List(4, 5)) nested: List[List[Int]] = List(List(1, 2, 3), List(4, 5)) scala> nested flatten res0: List[Int] = List(1, 2, 3, 4, 5) flatMap is like map, but the function given to flatMap is expected to return a list of values; the resultant list of lists is then “flattened” Syntax: def map[B](f: (A) => B): List[B] def flatMap[B](f: (A) => Traversable[B]): List[B] Example: scala> val greeting = List("Hello".toList, "from".toList, "Scala".toList) greeting: List[List[Char]] = List(List(H, e, l, l, o), List(f, r, o, m), List(S, c, a, l, a)) scala> greeting map (word => word.toList) res2: List[List[Char]] = List(List(H, e, l, l, o), List(f, r, o, m), List(S, c, a, l, a)) scala> greeting flatMap (word => word.toList) res3: List[Char] = List(H, e, l, l, o, f, r, o, m, S, c, a, l, a)

Слайд 11


filter filter is used to remove unwanted elements from a list, returning a new list scala> List(1, -2, 3, -4) filter (_ > 0) res3: List[Int] =...
Описание слайда:
filter filter is used to remove unwanted elements from a list, returning a new list scala> List(1, -2, 3, -4) filter (_ > 0) res3: List[Int] = List(1, 3) There is a corresponding (less often used) filterNot method scala> List(1, -2, 3, -4) filterNot (_ > 0) res4: List[Int] = List(-2, -4)

Слайд 12


foldl, foldr The “fold” functions apply a binary operator to the values in a list, pairwise, starting from the left or starting from the right scala>...
Описание слайда:
foldl, foldr The “fold” functions apply a binary operator to the values in a list, pairwise, starting from the left or starting from the right scala> val list = List(10, 1, 2, 3) list: List[Int] = List(10, 1, 2, 3) scala> list.foldLeft(0)(_ - _) res3: Int = -16 scala> list.foldRight(0)(_ - _) res4: Int = 8 scala> ((((0 - 10) - 1) - 2) - 3) res6: Int = -16 scala> (10 - (1 - (2 - (3 - 0)))) res8: Int = 8

Слайд 13


for Scala’s for comprehension can be used like Java’s for loop scala> for (ch
Описание слайда:
for Scala’s for comprehension can be used like Java’s for loop scala> for (ch

Слайд 14


Another for example You need to start with a generator, and after that you can have more generators, definitions, and guards scala> for { i
Описание слайда:
Another for example You need to start with a generator, and after that you can have more generators, definitions, and guards scala> for { i

Слайд 15


for-yield The value of a for comprehension, without a yield, is () With a yield, the value is a list of results (one result for each time through the...
Описание слайда:
for-yield The value of a for comprehension, without a yield, is () With a yield, the value is a list of results (one result for each time through the loop) The syntax is: for (sequence) yield expression Examples: scala> for (i for (n

Слайд 16


Another for-yield example Here’s a more complete example (Odersky, p. 125): val forLineLengths = for { file
Описание слайда:
Another for-yield example Here’s a more complete example (Odersky, p. 125): val forLineLengths = for { file

Слайд 17


toList scala> Array(1, 2, 3, 4) toList res12: List[Int] = List(1, 2, 3, 4) scala> "abc" toList res13: List[Char] = List(a, b, c) scala>...
Описание слайда:
toList scala> Array(1, 2, 3, 4) toList res12: List[Int] = List(1, 2, 3, 4) scala> "abc" toList res13: List[Char] = List(a, b, c) scala> Map("apple" -> "red", "banana" -> "yellow") toList res14: List[(java.lang.String, java.lang.String)] = List((apple,red), (banana,yellow)) scala> Set("abc", 123) toList res16: List[Any] = List(abc, 123) scala> List(1, 2, 3) toList res17: List[Int] = List(1, 2, 3) Also: toArray, toString, toSet, toMap

Слайд 18


Pattern matching Given this definition: scala> val myList = List("a", "b", "c") myList: List[java.lang.String] =...
Описание слайда:
Pattern matching Given this definition: scala> val myList = List("a", "b", "c") myList: List[java.lang.String] = List(a, b, c) This works: scala> val List(x, y, z) = myList x: java.lang.String = a y: java.lang.String = b z: java.lang.String = c But it’s pretty useless unless you know the exact number of items in the list Here’s a better way: scala> val hd :: tl = myList hd: java.lang.String = a tl: List[java.lang.String] = List(b, c)

Слайд 19


Example program object EnglishToGerman { def main(args: Array[String]) { println(translate("Scala is a wonderful language !")) } def...
Описание слайда:
Example program object EnglishToGerman { def main(args: Array[String]) { println(translate("Scala is a wonderful language !")) } def translate(english: String) = { val dictionary = Map("a" -> "ein", "is" -> "ist", "language" -> "Sprache", "wonderful" -> "wunderbar") def lookup(word: String) = { if (dictionary contains word) dictionary(word) else word } (english.split(" ") map (lookup(_))).mkString(" ") } } Output: Scala ist ein wunderbar Sprache !

Слайд 20


The End
Описание слайда:
The End



Теги Scala Lists
Похожие презентации
Mypresentation.ru
Загрузить презентацию