= Either.left(ErrorResponse(401, "Not authorised"))
40 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter3/Recipe7.kt:
--------------------------------------------------------------------------------
1 | package chapter3
2 |
3 | import java.util.concurrent.ConcurrentHashMap
4 | import kotlin.system.measureNanoTime
5 |
6 | /**
7 | * Chapter: Expressive functions and adjustable interfaces
8 | * Recipe: Approach to automatic functions memoization
9 | */
10 | fun main(vararg args: String) {
11 | fun factorial(n: Int): Long = if (n == 1) n.toLong() else n * factorial(n - 1)
12 | val cachedFactorial = ::factorial.memoized()
13 | println(" Execution time: " + measureNanoTime { cachedFactorial(12) } + " ns")
14 | println(" Execution time: " + measureNanoTime { cachedFactorial(13) } + " ns")
15 | }
16 |
17 | fun ((P) -> R).memoized(): (P) -> R = Memoizer.memoize
(this)
18 |
19 | class Memoizer
private constructor() {
20 |
21 | private val map = ConcurrentHashMap
()
22 |
23 | private fun doMemoize(function: (P) -> R): (P) -> R = { param: P ->
24 | map.computeIfAbsent(param) { param: P ->
25 | function(param)
26 | }
27 | }
28 |
29 | companion object {
30 | fun memoize(function: (T) -> U): (T) -> U =
31 | Memoizer().doMemoize(function)
32 | }
33 | }
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe1.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import java.time.Instant
4 | import chapter4.Recipe1.Message
5 |
6 | /**
7 | * Chapter: Powerful Data Processing
8 | * Recipe: Composing and consuming collections the easy way
9 | */
10 | fun main(vararg args: String) {
11 | val sentMessages = listOf (
12 | Message("Hi Agat, any plans for the evening?", "Samuel"),
13 | Message("Great, I'll take some wine too", "Samuel")
14 | )
15 | val inboxMessages = mutableListOf(
16 | Message("Let's go out of town and watch the stars tonight!", "Agat"),
17 | Message("Excelent!", "Agat")
18 | )
19 |
20 | val allMessages: List = sentMessages + inboxMessages
21 |
22 | allMessages.forEach { (text, _) ->
23 | println(text)
24 | }
25 | }
26 |
27 | object Recipe1 {
28 | data class Message(val text: String,
29 | val sender: String,
30 | val timestamp: Instant = Instant.now())
31 | }
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe2.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import java.time.Instant
4 | import chapter4.Recipe2.getMessages
5 | import chapter4.Recipe2.Folder
6 |
7 | /**
8 | * Chapter: Powerful Data Processing
9 | * Recipe: Filtering datasets
10 | */
11 | fun main(vararg args: String) {
12 | getMessages().filter { it.folder == Folder.INBOX && it.sender == "Agat" }
13 | .forEach { (text) ->
14 | println(text)
15 | }
16 | }
17 |
18 | object Recipe2 {
19 | data class Message(val text: String,
20 | val sender: String,
21 | val receiver: String,
22 | val folder: Folder = Folder.INBOX,
23 | val timestamp: Instant = Instant.now())
24 |
25 | enum class Folder { INBOX, SENT }
26 |
27 | fun getMessages() = mutableListOf(
28 | Message("Je t'aime", "Agat", "Sam", Folder.INBOX),
29 | Message("Hey, Let's go climbing tomorrow", "Stefan", "Sam", Folder.INBOX),
30 | Message("<3", "Sam", "Agat", Folder.SENT),
31 | Message("Yeah!", "Sam", "Stefan", Folder.SENT)
32 | )
33 | }
34 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe3.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe3.getNews
4 |
5 | /**
6 | * Chapter: Powerful Data Processing
7 | * Recipe: Automatic null removal
8 | */
9 | fun main(vararg args: String) {
10 | getNews()
11 | .filterNotNull()
12 | .forEachIndexed { index, news ->
13 | println("$index. $news")
14 | }
15 | }
16 |
17 | object Recipe3 {
18 | data class News(val title: String, val url: String)
19 |
20 | fun getNews() = listOf(
21 | News("Kotlin 1.2.40 is out!", "https://blog.jetbrains.com/kotlin/"),
22 | News("Google launches Android KTX Kotlin extensions for developers",
23 | "https://android-developers.googleblog.com/"),
24 | null,
25 | null,
26 | News("How to Pick a Career", "waitbutwhy.com")
27 | )
28 | }
29 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe4.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe4.allMessages
4 | import java.time.Instant
5 |
6 | /**
7 | * Chapter: Powerful Data Processing
8 | * Recipe: Sorting data with custom comparators
9 | */
10 | fun main(vararg args: String) {
11 | allMessages.forEach { println(it.text) }
12 | }
13 |
14 | object Recipe4 {
15 | data class Message(val text: String,
16 | val sender: String,
17 | val receiver: String,
18 | val time: Instant = Instant.now())
19 |
20 | val sentMessages = listOf(
21 | Message("I'm programming in Kotlin, of course",
22 | "Samuel",
23 | "Agat",
24 | Instant.parse("2018-12-18T10:13:35Z")),
25 | Message("Sure!",
26 | "Samuel",
27 | "Agat",
28 | Instant.parse("2018-12-18T10:15:35Z"))
29 | )
30 |
31 | val inboxMessages = mutableListOf(
32 | Message("Hey Sam, any plans for the evening?",
33 | "Samuel",
34 | "Agat",
35 | Instant.parse("2018-12-18T10:12:35Z")),
36 | Message("That's cool, can I join you?",
37 | "Samuel",
38 | "Agat",
39 | Instant.parse("2018-12-18T10:14:35Z"))
40 | )
41 |
42 | val allMessages = sentMessages + inboxMessages
43 | }
44 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe5.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe5.Address
4 | import chapter4.Recipe5.generateRecipientsString
5 |
6 | /**
7 | * Chapter: Powerful Data Processing
8 | * Recipe: Building strings based on dataset elements
9 | */
10 | fun main(vararg args: String) {
11 | val addresses = listOf(Address("hilary@gmail.com", "hilly"),
12 | Address("bob@gmail.com", "bobby"))
13 |
14 | val text = generateRecipientsString(addresses)
15 | println(text)
16 | }
17 |
18 | object Recipe5 {
19 | fun generateRecipientsString(recipients: List): String =
20 | recipients.filterNotNull()
21 | .map { it.emailAddress }
22 | .joinToString(", ", "To: ", "
")
23 |
24 | data class Address(val emailAddress: String, val displayName: String)
25 | }
26 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe6.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe6.messages
4 | import java.time.Instant
5 |
6 | /**
7 | * Chapter: Powerful Data Processing
8 | * Recipe: Dividing data into subsets
9 | */
10 | fun main(vararg args: String) {
11 | val pagedMessages = messages.windowed(4, partialWindows = true, step = 4) {
12 | it.map { it.text }
13 | }
14 | pagedMessages.forEach { println(it) }
15 | }
16 |
17 | object Recipe6 {
18 | data class Message(val text: String,
19 | val time: Instant = Instant.now())
20 |
21 | val messages = listOf(
22 | Message("Any plans for the evening?"),
23 | Message("Learning Kotlin, of course"),
24 | Message("I'm going to watch the new Star Wars movie"),
25 | Message("Would u like to join?"),
26 | Message("Meh, I don't know"),
27 | Message("See you later!"),
28 | Message("I like ketchup"),
29 | Message("Did you send CFP for Kotlin Conf?"),
30 | Message("Sure!")
31 | )
32 | }
33 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe7.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe7.getStudents
4 |
5 | /**
6 | * Chapter: Powerful Data Processing
7 | * Recipe: Data transformation with map and flatMap
8 | */
9 | fun main(vararg args: String) {
10 | fun getLecturesOfCoursesWithSubscribedStudents() =
11 | getStudents()
12 | .flatMap { student ->
13 | student.courses
14 | }
15 | .distinct()
16 | .map { course -> course.lecturer }
17 | .distinct()
18 | }
19 |
20 | object Recipe7 {
21 | class Course(val name: String, val lecturer: Lecturer, val isPaid: Boolean = false)
22 | class Student(val name: String, val courses: List)
23 | class Lecturer(val name: String)
24 |
25 | fun getStudents(): List = listOf()
26 | }
27 |
--------------------------------------------------------------------------------
/Kotlin-Samples/src/main/kotlin/chapter4/Recipe8.kt:
--------------------------------------------------------------------------------
1 | package chapter4
2 |
3 | import chapter4.Recipe8.album
4 | import chapter4.Recipe8.Track
5 | import chapter4.Recipe8.getStartTime
6 |
7 | /**
8 | * Chapter: Powerful Data Processing
9 | * Recipe: Folding and reducing data sets
10 | */
11 | fun main(vararg args: String) {
12 | println(album.getStartTime(Track("10/10", 176)))
13 | println(album.getStartTime(Track("Growing Up Beside You", 191)))
14 | println(album.getStartTime(Track("Coming Up Easy", 292)))
15 | }
16 |
17 | object Recipe8 {
18 | data class Track(val title: String, val durationInSeconds: Int)
19 | data class Album(val name: String, val tracks: List