Programmation appliquée en Scala

Copyright © Cay S. Horstmann 2015 Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License

Conditional Expressions

Vectors

What is the Value of...

"Fred".toVector.map(c => 'a' <= c && c <= 'z')
  1. Vector('F', 'r', 'e', 'd')
  2. Vector('r', 'e', 'd')
  3. Vector(false, true, true, true)
  4. Something else (other value, syntax error, ...)

Maps

Tuples

What is result...

...where

val words = "Mary had a little lamb".split(" ").toVector
val result = words.map(w => w -> w.length).toMap
  1. A Vector[(String, Int)]
  2. A Vector[String => Int]
  3. A Map[String, Int]
  4. Something else (other type, syntax error, ...)

Scaladoc

Scaladoc Tips

Lab

Scary looking lab

Part 1: If Expressions

  1. Make a worksheet Day2. Paste in
    if (math.random > 0.5) "Bonjour" else "Adieu"
    and save. What do you get?
  2. Paste it in a couple more times. What do you get?
  3. Make a vector with ten results of that call. (Not by pasting it 10 times, of course.)
  4. Given a sequence of integers, produce a sequence of tuples (n, 0) if n is positive and (0, -n) if n is negative. Test with -10.to(10).
  5. Write a function that yields 1 if x > 0 , 0 if x equals 0, and -1 if x < 0. Complete val sign = ... and then call -10.to(10).map(sign).
  6. Do not use any constructs that weren't covered! For example, don't use def in the preceding assignment.

Part 2: Strings are Sequences Too

  1. What is "Fred"(2)? Why?
  2. How can you turn it into "Free"? That is, what call yields a new string with characters 'F' 'r' 'e' 'e'? (Hint: Don't use substring, but look at the title of the slide.)
  3. How can you reverse a string? Tip: Don't hesitate to experiment in the worksheet. That's what it's there for. Type "Bonjour".rev and hit Ctrl Space. What happens?
  4. Does that method work for other sequences too? Experiment with 1.to(10). What happens?
  5. Does distinct work with strings? What does it do? Give an example.
  6. What about map? Give an example.
  7. How do you remove the last character of a string? Hint: You saw earlier how to drop the first character. Look up the Scaladoc of StringOps.

Part 3: Fun with Vectors

  1. What is
    val words = io.Source.fromFile("/usr/share/dict/words").getLines().toVector
    (If you get an exception that you don't have /usr/share/dict/words, then first save the contents of this link somewhere and change the location to something like "C:\\Users\\Yourname\\Downloads\\words.txt".)
  2. How many words are in it? (Try out the obvious, or look into Scaladoc.)
  3. What is the median entry? Or the two median ones if the length was even.
  4. What are the distinct word lengths? (Go back to lab 1 if you need a hint.)
  5. Which ones have maximum length? (Hint: Check out the Scaladoc for filter.)
  6. More filtering. Find all palindromes (i.e. strings that equal their reverse). Hint: In Scala, you compare strings with ==.

Part 4: Misery with Tuples

  1. What is 1 -> 2 -> 3? What is the type of the result?
  2. Write a function flatten that takes such a thing and turns it into an (Int, Int, Int). Use _1, _2
  3. Repeat with destructuring.
  4. What is the result of the following statement, and why?
    val x,y,z = (1,2,3)

Part 5: If I Had Just One Data Structure...

  1. And if I could just take one method, it would be groupBy. What is words.groupBy(w => w.length)?
  2. Make a map so that myMap('a') yields all words that start with the letter a and so on.
  3. How many words start with a given letter? Hint: myMap.map(...)
  4. Extra credit: Turn this into a list of tuples that is sorted by frequency. Hint: sorted doesn't work because Scala doesn't know how to sort the tuples. There are two other sort methods. One of them wants a function that maps into something that Scala does know how to sort—the easy choice for solving this exercise.
  5. Extra credit: Now do it with the other method (and some tuple misery).

Homework

Do this as individual work, not with your partner

When all done, email the signed zip files to Fatemeh.Borran@heig-vd.ch