forked from abdiwahid-hajir/uw-complex-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.kt
More file actions
59 lines (50 loc) · 1.9 KB
/
Library.kt
File metadata and controls
59 lines (50 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package edu.uw.complexkotlin
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}
// write a lambda using map and fold to solve "FIZZBUZZ" for the first
// fifteen numbers (0..15).
// use map() to return a list with "", "FIZZ" (for 3s) or "BUZZ" (for 5s).
// use fold() to compress the array of strings down into a single string.
// the final string should look like FIZZBUZZFIZZFIZZBUZZFIZZFIZZBUZZ for 0..15.
// store this lambda into 'fizzbuzz' so that the tests can call it
//
val fizzbuzz : (IntRange) -> String = { _ -> "" }
// Example usage
/*
if (fizzbuzz(0..1) == "")
println("Success!")
if (fizzbuzz(0..3) == "FIZZ")
println("Success!")
if (fizzbuzz(0..5) == "FIZZBUZZ")
println("Success!")
*/
// This is a utility function for your use as you choose, and as an
// example of an extension method
fun Int.times(block: () -> Unit): Unit {
for (it in 1..this) {
block()
}
}
// Use this function
fun process(message: String, block: (String) -> String): String {
return ">>> ${message}: {" + block(message) + "}"
}
// Create r1 as a lambda that calls process() with message "FOO" and a block that returns "BAR"
val r1 = { "" }
// Create r2 as a lambda that calls process() with message "FOO" and a block that upper-cases
// r2_message, and repeats it three times with no spaces: "WOOGAWOOGAWOOGA"
val r2_message = "wooga"
val r2 = { "" }
// write an enum-based state machine between talking and thinking
enum class Philosopher { }
// create an class "Command" that can be used as a function (provide an "invoke()" function)
// that takes a single parameter ("message" of type String)
// primary constructor should take a String argument ("prompt")
// when invoked, the Command object should return a String containing the prompt and then the message
class Command(val prompt: String) { }