Contained in: Functional Programming in Kotlin by Tutorials
combine
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Monoids & Semigroups
May 4 2022 · Chapter
…typeclass If you use the set analogy for types, you can think of a monoid for a type T as: A commutative combine operation of type (T, T) -> T. A unit element of type T. Note: It’s important to understand that you could do this in multiple ways…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Expression Evaluation, Laziness & More About Functions
May 4 2022 · Chapter
Well, in this specific example, there’s no difference — but consider the following case instead: interface Combinable<A> { // 1 fun combine(rh: A): A } fun <A : Combinable<A>> combine(lh: A, rh: A): A = lh.combine(rh) // 2 In this case, you define: The Combinable…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Data Types
May 4 2022 · Chapter
…abstraction accepting as input something that tells where one differs from the other. In this case: The initial value for the accumulator. How you combine each element with the current value you’ve accumulated. In the same Folding.kt file, add the following code: fun <T, S> List…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Managing State
May 4 2022 · Chapter
…code to State.kt: fun <S, A, B, C> State<S, A>.zip( // 1 s2: State<S, B>, // 2 combine: (A, B) -> C // 3 ): State<S, C> = // 4 State { s0 -> // 5 val (v1, s1) = this…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Appendix K: Chapter 12 Exercise Solutions
May 4 2022 · Chapter
…pattern, and you can implement MonoidIntMult like this: object MonoidIntMult : Monoid<Int> { // 1 override val unit: Int get() = 1 // 2 override val combine: Int.(Int) -> Int get() = Int::times // 3 } In this case, you define: MonoidIntMult as an object implementing Monoid…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Algebraic Data Types
May 4 2022 · Chapter
Feel free to skip it or read it later if you want. What is algebra? Algebra is a category of arithmetic that lets you combine numbers with letters representing numbers by using specific rules. Here’s an example of a simple algebraic expression…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Error Handling With Functional Programming
May 4 2022 · Chapter
…somehow accumulate the errors into one. In Chapter 12, “Monoids & Semigroups”, you learned that a monoid describes a way to combine two values into a single value of the same type. You can represent the properties of a monoid in different ways. For example, in ValidationSemigroup.kt, add the following…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Functional Programming Concepts
May 4 2022 · Chapter
…function, the output depends only on the input parameters, making the composition of pure functions pure as well. You might argue that you can combine impure functions in the same way you combine pure functions. This is true, but composition wouldn’t fix their purity. Add the following functions…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Char>. Invoke declarativeFoldRight, passing a StringBuilder as the initial state for the accumulator. Append the character to the previous accumulator state in the combination function. Return the content of StringBuilder as a String. To test the previous code, just run the following code: fun main() { reverse("supercalifragilisticexpialidocious") pipe ::println…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
This pattern continues until you reach the element 4, which results in [1,1,2,3]. The results of each of these interations are combined into the final resulting list that you see printed at the end. In Chapter 12, “Monoids & Semigroups”, you’ll learn…
Android & Kotlin
Chapter in Functional Programming in Kotlin by Tutorials
Sequence & Flow
May 4 2022 · Chapter
…withContext(ctx) { // 3 try { Result.success(TvShowFetcher.fetch(query)) // 4 } catch (ioe: IOException) { Result.failure(ioe) // 5 } } This code should look familiar, even if it combines a few concepts. Here, you: Define fetchTvShowResult as a suspendable function with a CoroutineContext as the first parameter and a String as the second. Note…