corrected / added midterm examples
This commit is contained in:
		| @@ -24,6 +24,16 @@ def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = { | |||||||
|   }) |   }) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | val l1 = (1 to 10).toList | ||||||
|  | val f1 = ((a: Int) => a % 2 == 0) | ||||||
|  | val f2 = ((a: Int) => a > 5) | ||||||
|  | predicates(l1)(List(f1, f2))  // List(6, 8, 10) | ||||||
|  |  | ||||||
|  | val l2 = ("Hello Scala Echo").toList | ||||||
|  | val f4 = ((a: Char) => a == 'a' || a == 'e' || a == 'o') | ||||||
|  | val f3 = ((a: Char) => !a.isUpper) | ||||||
|  | predicates(l2)(List(f3, f4))  // List('e', 'o', 'a', 'a', 'o') | ||||||
|  |  | ||||||
| /* | /* | ||||||
| // Better solution | // Better solution | ||||||
| def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = { | def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = { | ||||||
| @@ -41,4 +51,5 @@ def fixedPoint(f: Int => Int): Int => Int = { | |||||||
|   func |   func | ||||||
| } | } | ||||||
|  |  | ||||||
| fixedPoint(x => x / 2 + 5)(40) | fixedPoint(x => if (x % 10 == 0) x else x + 1)(35)  // 40 | ||||||
|  | fixedPoint(x => x / 2 + 5)(20)  // 10 | ||||||
		Reference in New Issue
	
	Block a user