###### tags: `WebHack#32` # A Gentle Introduction to Scala and FP Speaker: __Anton Semenov__ Slides: TBA ## What lies at the core of FP? > ## Function programming is a way of solving problems which centers around combinning side-effect-free constructs in a declarative... > ## Declarative vs. Imperative > ## Deep dive (FP & Scala) - immutability - pure functions - referenctial transparency - algebraic data type - pattern matching - recursion ## Value mutation > Variables - > Immutable values - ## Immutable values ### Benefits - Reduce concurrency issues - No shared mutable state - Easier local reasoning ### Drawbacks - Extra memory usage ## Pure functions > Side-effect free ## Referential transparency > All calls to a function can be substituted with its return value > ## Business value with pure function - Describe side-effecting computations - ## Pure functions Pros and Cons > Benefits - Ultimate local reasoning - Parallelizable execution - Easy testing > Drawbacks - Extra objec allocations - Require an effect system or a library to operate mutable stateHiH ## Algebraic data types ADT = Sum type + Product type ## Product types > Traditional classes > Product types ## Pattern matching > ## ADTs and pattern matching > ## Recursion > Declarative > Native mapping to many real-world problems > Can be as efficient as a loop* > ## FP in scala > Immutability > Pure functions > Referenctial transparency > Algebraic data types > Pattern matching > Recursion ## Scala language features > For comprehensions - - - - Take advantage of... > Higher-kinded type - Simple types (Int, String, User) - Unary ype coonstructors - Binary type constructors - Higher-knded types > Systax extension - > Macros - Eliminate boilerplate code - Provide implementations at compile time > Implicit values - Provide automatic value forwarding - Resolved at compile-time - Can be created by the compiler > Syntax extensions ## Scala in numbers > Scala adoption rates > Scala and productivity > Scala's performance - Uses JVM as the main runtime platform - Can be compiled to a native binary - Up to 35% performance increase with GraaJVM - Utilizes incremental compilation for only a few second daily per build ## Scala as a language - Rapidly developed - Adopted by many big companies - Offers significant productivity benefits - Keeps reasonalbe performance ## Scala in real world > Areas of application - Data pipelines - Resilient applications - Distributed systems ## Scala vs. Java & Kotlin > Java & Kotlin > Scala ## Scala vs. Python > Python > Scala ## Scala vs. Golang > Golang > Scala ## Future Propective > ## Q & A Q: What's the underline (\_) means in the map() function A: The underline is the parameter of the function Q: I heard that Dotty is quite divisive in the Scala community, what are your thoughts on it? Does it make sense for a person to learn Scala considering Dotty comes out soon? A: This concern is mainly for the library developers. Most feature in Scala 2 will be adopted by Scala 3 Q: Why Java and Kotlin implemnts full features of pattern match A: Kotlin creators want to make the langauge itself as simple as possible Q: what do you think about the tradeoffs between implicits magic making code more convenient to write but potentially way more difficult for people new to the codebase to read and understand? A: The implicits of Scala has been improved, and the implicit feature becomes more explicit.