Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Kotlin & Arrow the functional way

69 views

Published on

Kotlin is a great language for developing server-side applications; it's an object-oriented language and also a functional one, supporting features such as function types, lambdas or higher order functions. But...is this enough to switch completely from a imperative paradigm to a functional paradigm?
In this talk we'll see how features from Arrow library completes Kotlin in order to follow a pure functional way.

Published in: Software
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full doc Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Kotlin & Arrow the functional way

  1. 1. Kotlin & Arrow: The functional way Noe Luaces @noe_luaces
  2. 2. ● Galician since I have memory (now living in Barcelona) ● Avid comic book reader since I was a teenager ● Software Engineer since October 2007 ● Kotlin Developer since October 2018 > whoami
  3. 3. What is Kotlin?
  4. 4. Designed to interoperate fully with other JVM-based languages Object Oriented Kotlin is...
  5. 5. Designed to interoperate fully with other JVM-based languages Object Oriented Great introductory language to functional programming Functional Kotlin is...
  6. 6. Designed to interoperate fully with other JVM-based languages Object Oriented Great introductory language to functional programming Can be used in backend, frontend or as a script language Functional General Purpose Kotlin is...
  7. 7. Designed to interoperate fully with other JVM-based languages Object Oriented Great introductory language to functional programming Can be used in backend, frontend or as a script language Functional General Purpose Kotlin is... Android has officially adopted Kotlin as the supported language for mobile development services Mobile Standard
  8. 8. ● Developed by JetBrains in 2011 ● Its name comes from Kotlin island, Russia Kotlin background
  9. 9. ● JetBrains lead Dmitry Jemerov said that they were looking for a language with the same features as Scala but with a compilation velocity similar to Java Kotlin background
  10. 10. Function type
  11. 11. Function Type ● Functions in Kotlin are first-class values ● They can be assigned to variables and passed around as parameters
  12. 12. Function Type (function reference)
  13. 13. Function Type
  14. 14. Function Type
  15. 15. Function Type
  16. 16. Higher Order Functions
  17. 17. Higher Order Functions ● Functions that accept parameters typed as a function ● Functions that accept lambda expressions as parameters
  18. 18. Higher Order Functions ● Scope functions like run, apply, let, with, also accept a lambda expression ● Other functions such as map, flatMap, filter, fold
  19. 19. Higher Order Functions
  20. 20. Pure Functions
  21. 21. Pure Functions ● Depend only on the input to produce the result, not on any hidden information or external state ● Have no observable side effects, like modifying a variable passed by reference. ● Hence, they are easy to test
  22. 22. Pure Functions
  23. 23. Not pure functions
  24. 24. Immutability
  25. 25. Immutable objects ● Remain in exactly the state in which they were created. Therefore, they are thread-safe ● Are easier to write, use and they make cleaner code ● Make easier to parallelize the program as there are no conflicts among objects
  26. 26. Immutable objects ● Are preferred whenever possible in Kotlin ● Are defined with the keyword val and can be initialized only single time ● Data classes, that hold the state which they were created with
  27. 27. Immutable objects
  28. 28. Pattern matching
  29. 29. The "when {}" block ● Advanced form of the switch-case statement known from Java ● No break statements are needed at the end of each case
  30. 30. The "when {}" block ● Due smartcast, we can access methods or properties of the type without extra checks ● Smartcast also works with subclasses providing a full pattern matching ● Could be used as an expression or a statement
  31. 31. The "when {}" block
  32. 32. The "when {}" block
  33. 33. Currying
  34. 34. Currying ● Is a mathematical technique ● Consists on breaking down a function that takes multiple arguments into a series of functions that each take only one argument
  35. 35. Currying
  36. 36. Null safety
  37. 37. Null safety ● Explicit nullable declarations ● Safe calls ● !! operator
  38. 38. Null safety
  39. 39. What is missing? In Functional Programming, we try to avoid throwing exceptions: ● For methods which fail often and predictably, like parsing user input as a number, it’s costly and unnecessary to throw exceptions ● Handling checked exceptions can easily make the client’s code needlessly complicated
  40. 40. What is Arrow?
  41. 41. Arrow background ● Open source library sponsored by 47 Degrees ● Fusion between KATEGORY and funKTionale, the most relevant FP libraries for Kotling in 2017 ● First release was available in 2018 ● Equivalent to Cats for Scala and Vavr for Java
  42. 42. Modeling absence
  43. 43. Data Type: Option ● Avoids NullPointerException, without error detail ● Possible values: None or Some
  44. 44. Data Type: Option
  45. 45. Dealing with Option values: Fold {() -> "Nothing!"} {(Book) -> "Book.name found!"}
  46. 46. Dealing with Option values: Fold
  47. 47. Dealing with Option values: Fold
  48. 48. Capturing Throwable
  49. 49. Data type: Try ● Useful to handle Runtime Exceptions ● Failure or Success ● Can be combined with a when statement that uses smart cast
  50. 50. Data type: Try
  51. 51. Modeling Errors
  52. 52. Data type: Either ● Represents a value with two possibilities ● Left (error case) or Right (happy case) ● Allows to fail fast providing all the context
  53. 53. Data type: Either
  54. 54. Data type: Either
  55. 55. Dealing with Either values
  56. 56. {cat -> IsDeadFailure(cat)} { cat -> cat.name} Dealing with Either values: Bimap
  57. 57. Dealing with Either values: Bimap
  58. 58. Dealing with Either values: Bimap
  59. 59. Dealing with Either values
  60. 60. Modeling Parallel Errors
  61. 61. Data type: Validated ● Collect all errors at once ● Possible values: Valid or Invalid ● Often used the typealias ValidatedNel ( instead of Validated<Nel>)
  62. 62. Data type: Validated
  63. 63. Data type: Validated
  64. 64. Data type: Validated
  65. 65. To sum up... KOTLIN ARROW HIGHER ORDER FUNCTIONS IMMUTABILITY PATTERN MATCHING CURRYING EITHER OPTION TRY VALIDATIONS
  66. 66. Why Kotlin? ● Concise syntax ● Null safety ● Ease of transition from Java ● Interoperability with other JVM-based languages ● Introductory language to functional programming ● Android's adoption as the supported language
  67. 67. Why Arrow? ● Data types ● Type classes ● Effects ● Optics ● Other functional programming patterns as higher- level abstractions ready to use
  68. 68. Noe Luaces @noe_luaces Thank you

×