# Programmation appliquée en Scala Copyright © Cay S. Horstmann 2015 # Laziness • Laziness = only doing what is necessary, at the latest possible moment
• Example: “short-circuit evaluation” of `&&`, `||`
`if (i < s.length && s(i) == '.')`
• Can we write such an operation?
`def and(a: Boolean, b: Boolean) = if (!a) false else b`
• No, that doesn't work:
```val s = "Hello"
val i = 5
if (and(i < s.length, s(i) == '.'))
// Throws IndexOutOfBoundsException```

# By-Name Parameters

• When a parameter of a function (declared with `def`) is prefixed by `=>`, it is not evaluated when the function is called.
`def and(a: => Boolean, b: => Boolean) = if (!a) false else b`
• When calling
`and(i < s.length, s(i) == '.')`
`and` receives two functions `() => { i < s.length }`, `() => { s(i) == '.' }`
• When the parameters `a` and `b` are evaluated, the functions are called
`if (!a) ... // Calls () => { i < s.length }`

# Let's define if/else

Which of these will work? We want to call

`ifElse(i < 5, println("Hi"), println("Bye"))`

1. ```def ifElse(c: Boolean, a: Unit, b: Unit) {
c && { a; true } || { b; true } }```
2. ```def ifElse(c: Boolean, a: => Unit, b: => Unit) {
c && { a; true } || { b; true } }```
3. ```def ifElse(c: => Boolean, a: => Unit, b: => Unit) {
c && { a; true } || { b; true } }```
4. None of these will work

# `lazy val`

• When a `val` is declared `lazy`, its initialization is deferred until the first time it is used.
```lazy val words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated the first time words is used
```
• If `words` is never used, the file is not opened.
• Halfway between `val` and `def`:
```val words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated as soon as words is defined
def words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated every time words is used
```
• Implemented with hidden flag that is tested whenever `words` is used

# Lab • You work with a buddy
• One of you (the coder) writes the code, the other (the scribe) types up answers
• When you get stuck, ask your buddy first!
• Switch coder/scribe roles each lab
• The coder submits the worksheet. Include the scribe's name in the worksheet!
• The scribe submits answers. Include the coder's name in the report!

# Part 1: Do-It-Yourself `while`

1. You've seen how to implement an if/else statement in Scala. Now we want to do the same with `while`. We have two arguments, the condition and the body. For example,
```val iter = Source.fromFile("/usr/share/dict/words").getLines.buffered
// "buffered" gives you an iterator with a "head" method for lookahead
While(iter.head.length < 15, iter.next)
val longWord = iter.head // The first word of length >= 15
```
How do you declare `While`? (Just the header, not the implementation.)
2. Now on with the implementation. If the condition is true, execute the body. Then call the function recursively. What do you get when running the code snippet above?
3. It's a little ugly. Make it so that you can call
`While(iter.head.length < 15) { iter.next }`
Hint: Curry

# Homework

Do this as individual work, not with your partner

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