Functional Kotlin with Arrow by Karin-Aleksandra Monoid

June 22, 2022

In the Functional Kotlin with Arrow presentation by Karin-Aleksandra Monoid, she presents a functional coding talk focused on Kotlin and the Arrow library. The speaker, a senior software engineer with a background in Kotlin, Scala, and functional programming, aims to convince enterprise Java developers to use Kotlin and Arrow. She begins by discussing Arrow, a library consisting of multiple parts, including core, effects, optics, and meta. In this presentation, she focuses on Arrow Core, which offers more functional programming opportunities in Kotlin. Karin-Aleksandra covers various aspects of Arrow, such as non-empty lists as an example of domain-oriented functional programming, using either for handling errors, and the concept of monads in modelled comprehensions. She also discusses her background in functional programming and her experience training Java developers to use functional programming. The talk includes a discussion on handling errors using either, chaining idlers, and using monad comprehensions with either in Kotlin. The speaker emphasizes the benefits of using functional programming and the Arrow library in Kotlin for enterprise Java developers.

Functional Kotlin with Arrow: A comprehensive overview

Introduction to Arrow and Functional Programming

Karin-Aleksandra Monoid, with a rich background in Kotlin, Scala, and functional programming, she positioned her talk to bridge the gap for developers transitioning from Java to Kotlin.

Arrow, she explained, is a comprehensive library for functional programming in Kotlin, consisting of several modules: core, effects, optics, and meta. Her focus for this session was on Arrow Core, which provides the foundational tools for integrating functional programming paradigms into Kotlin applications.

 

Non-empty lists for domain-oriented Functional Programming

One of the first concepts she introduced was non-empty lists. In certain contexts, such as a fintech company handling payments, an empty list might be meaningless. By using non-empty lists, developers can enforce domain constraints that ensure lists always contain at least one element. This approach not only improves data integrity but is also compatible with existing serialization libraries like Jackson, making integration seamless.

 

Handling errors with Either and Result

Karin-Aleksandra then shifted the discussion to error handling in Kotlin, highlighting the differences from Java’s approach. Java’s checked exceptions, while straightforward, can clutter code and reduce readability. Kotlin, lacking native support for checked exceptions, often uses the Stream API for error handling. However, she pointed out the limitations of this approach, where errors might be obscured by null values.

Arrow offers a more expressive alternative through its Either and Result classes. Either is a functional programming pattern where results are represented as either a success (Right) or a failure (Left). This allows for clearer, more manageable error handling in APIs. Additionally, Validated and Ior (a combination of left and right) provide further flexibility, especially for scenarios where multiple errors need to be collected.

 

Monad comprehensions and chaining

A key feature of Arrow highlighted in the talk is the use of monads, particularly Either, for chaining operations. Monad comprehensions in Arrow simplify the handling of sequential computations that might fail. When an error occurs in a chain, the computation halts, returning the error immediately. This is achieved using flatMap, which applies a function to each element of a monadic structure, allowing for elegant error propagation.

Karin-Aleksandra explained how to write functions using Either, either explicitly or by leveraging modern Kotlin’s context receivers. This approach facilitates cleaner, more maintainable code, especially in applications with complex business logic.

 

Advanced functional constructs: Optional, Effects, and Optics

Beyond error handling, Arrow provides various other functional constructs like Optional for dealing with optional values, Effects for managing asynchronous operations in suspend functions, and Optics for traversing and modifying immutable data structures. These tools enhance Kotlin’s functional programming capabilities, making it easier to build robust, maintainable applications.

 

Personal journey and training insights

Karin-Aleksandra shared her personal journey into functional programming, starting with Haskell during her university years. Despite initial reluctance, her experience with Scala and functional coding eventually led her to appreciate its benefits. This background fueled her passion for teaching Java developers about functional programming, breaking down complex concepts into digestible parts to facilitate their learning process.

When asked about stacking monads in Kotlin or Arrow, she emphasized the importance of the specific use case, acknowledging that while she hadn’t combined them extensively, the Arrow library’s design supports such flexibility. She also contrasted her current work with Clojure and Golang, highlighting the distinct differences between functional and non-functional programming styles.

 

Conclusion

Karin-Aleksandra Monoid’s presentation on Functional Kotlin with Arrow was a comprehensive introduction to the benefits and techniques of functional programming in Kotlin. By focusing on practical examples and real-world applications, she effectively demonstrated how Arrow can transform the way developers handle errors, manage data, and build more reliable software.

For enterprise Java developers and those new to functional programming, this talk provided valuable insights and tools to begin exploring the functional paradigm in Kotlin, making a compelling case for its adoption in modern software development.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.