68 // Boolean: true false |
68 // Boolean: true false |
69 // |
69 // |
70 // List[Int]: Nil, List(_) |
70 // List[Int]: Nil, List(_) |
71 // |
71 // |
72 // Option[Int]: None, Some(0), Some(1), ... |
72 // Option[Int]: None, Some(0), Some(1), ... |
|
73 // Option[Boolean]: None, Some(true), Some(false) |
73 // Option[...]: None, Some(_) |
74 // Option[...]: None, Some(_) |
74 |
75 |
75 def safe_div(x: Int, y: Int) : Option[Int] = |
76 def safe_div(x: Int, y: Int) : Option[Int] = |
76 if (y == 0) None else Some(x / y) |
77 if (y == 0) None else Some(x / y) |
77 |
78 |
|
79 safe_div(10 + 5, 4 - 1) |
|
80 |
78 List(1,2,3,4,5,6).indexOf(7) |
81 List(1,2,3,4,5,6).indexOf(7) |
79 |
82 List[Int]().minOption |
80 def my_min(ls: List[Int]) : Option[Int] = ls.minOption |
83 |
|
84 def my_min(ls: List[Int]) : Option[Int] = |
|
85 ls.minOption |
81 |
86 |
82 my_min(List(1,2,3,4)) |
87 my_min(List(1,2,3,4)) |
83 |
88 |
84 |
89 |
85 // better error handling with Options (no exceptions) |
90 // better error handling with Options (no exceptions) |
88 // |
93 // |
89 |
94 |
90 import scala.util._ |
95 import scala.util._ |
91 import io.Source |
96 import io.Source |
92 |
97 |
93 val my_url = "https://nms.kcl.ac.uk/christian.urban2/" |
98 val my_url = "https://nms.kcl.ac.uk/christian.urban/" |
94 |
99 |
95 Source.fromURL(my_url)("ISO-8859-1").mkString |
100 Source.fromURL(my_url)("ISO-8859-1").mkString |
|
101 Source.fromURL(my_url)("ISO-8859-1").getLines().toList |
96 |
102 |
97 Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("") |
103 Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("") |
98 |
104 |
99 Try(Some(Source.fromURL(my_url)("ISO-8859-1").mkString)).getOrElse(None) |
105 Try(Some(Source.fromURL(my_url)("ISO-8859-1").mkString)).getOrElse(None) |
100 |
106 |
101 |
107 |
102 // the same for files |
108 // the same for files |
|
109 |
103 Try(Some(Source.fromFile("test.txt")("ISO-8859-1").mkString)).getOrElse(None) |
110 Try(Some(Source.fromFile("test.txt")("ISO-8859-1").mkString)).getOrElse(None) |
104 |
111 |
|
112 Try(Source.fromFile("test.txt")("ISO-8859-1").mkString).toOption |
|
113 |
|
114 Using(Source.fromFile("test.txt")("ISO-8859-1"))(_.mkString).toOption |
105 |
115 |
106 // how to implement a function for reading |
116 // how to implement a function for reading |
107 // (lines) something from files... |
117 // (lines) from files... |
108 // |
118 // |
109 def get_contents(name: String) : List[String] = |
119 def get_contents(name: String) : List[String] = |
110 Source.fromFile(name)("ISO-8859-1").getLines.toList |
120 Source.fromFile(name)("ISO-8859-1").getLines().toList |
111 |
121 |
112 get_contents("text.txt") |
122 get_contents("text.txt") |
113 get_contents("test.txt") |
123 get_contents("test.txt") |
114 |
124 |
115 // slightly better - return Nil |
125 // slightly better - return Nil |
118 |
128 |
119 get_contents("text.txt") |
129 get_contents("text.txt") |
120 |
130 |
121 // much better - you record in the type that things can go wrong |
131 // much better - you record in the type that things can go wrong |
122 def get_contents(name: String) : Option[List[String]] = |
132 def get_contents(name: String) : Option[List[String]] = |
123 Try(Some(Source.fromFile(name)("ISO-8859-1").getLines.toList)).getOrElse(None) |
133 Try(Some(Source.fromFile(name)("ISO-8859-1").getLines().toList)).getOrElse(None) |
124 |
134 |
125 get_contents("text.txt") |
135 get_contents("text.txt") |
126 get_contents("test.txt") |
136 get_contents("test.txt") |
127 |
137 |
128 |
138 |
154 |
164 |
155 |
165 |
156 // getOrElse is for setting a default value |
166 // getOrElse is for setting a default value |
157 |
167 |
158 val lst = List(None, Some(1), Some(2), None, Some(3)) |
168 val lst = List(None, Some(1), Some(2), None, Some(3)) |
159 |
|
160 |
|
161 |
169 |
162 |
170 |
163 // a function that turns strings into numbers |
171 // a function that turns strings into numbers |
164 // (similar to .toInt) |
172 // (similar to .toInt) |
165 Integer.parseInt("1234") |
173 Integer.parseInt("1234") |
232 // but this only works when the arguments are clear, but |
240 // but this only works when the arguments are clear, but |
233 // not with multiple occurences |
241 // not with multiple occurences |
234 lst.find(n => odd(n) && n > 2) |
242 lst.find(n => odd(n) && n > 2) |
235 |
243 |
236 |
244 |
237 |
245 // lexicographic ordering |
238 val ps = List((3, 0), (3, 2), (4, 2), (2, 2), |
246 val ps = List((3, 0), (3, 2), (4, 2), (2, 2), |
239 (2, 0), (1, 1), (1, 0)) |
247 (2, 0), (1, 1), (1, 0)) |
240 |
248 |
241 def lex(x: (Int, Int), y: (Int, Int)) : Boolean = |
249 def lex(x: (Int, Int), y: (Int, Int)) : Boolean = |
242 if (x._1 == y._1) x._2 < y._2 else x._1 < y._1 |
250 if (x._1 == y._1) x._2 < y._2 else x._1 < y._1 |