Functional Programming

What is Functional Programming:

  • FP applications consists only of immutable values and pure functions

  • Pure functions means that function depends only on it's inputs parameters and (b) functions have no side effect.

When you Think in FP you see an application as a data flowing into the application, being transformed by a series of transformer functions and producing an output.

When people talk about functional programming, they mention a dizzying number of “functional” characteristics. They mention immutable data, first class functions and tail call optimisation. These are language features that aid functional programming. They mention mapping, reducing, pipelining, recursing, currying and the use of higher order functions. These are programming techniques used to write functional code. They mention parallelization, lazy evaluation and determinism. These are advantageous properties of functional programs.

Functional programming like an algebra. Algebra is two things: a SET of objects and operations used on those objects to create new objects from that set.

Pure functions + Immutable values = Algebra

Functional Programming Simplified:

  • Functional error handling (Try, Success, Failure)

  • Option

  • for-выражения

  • Option chaining leads to flatMap

  • Тип c map, flatMap называется монадой

  • IO Monad

  • State Monad

  • Невозможно скомбинировать две монады вместе, но можно скомбинировать их эффекты. Monad Transformer - это обертка поверх монады и операции.

  • Используйте Monad Transformers для организации цепочки вычислений с монадами разных видов

  • В Scala можно писать модульный код разделяя интерфейс и реализацию. Разделение происходит путем выделения trait-ов и их композиции.

  • Можно делать object Object extends Trait и тем самым создать новый локальный объект в коде.

  • Линзы используются для глубокого изменения сложного объекта.

  • Используйте Futures для асинхронных вычислений.

Articles

Algebra

JS

Memoization

https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library

Lazy

Monades

Zones

Lenses

ADT

https://codewords.recurse.com/issues/three/algebra-and-calculus-of-algebraic-data-types

Transducer

Это как Reducer, только применяет map/filter/reduce к каждому из итерируемых значений. Если нужно выполнить итерацию списка, то не нужно проходить его несоклько раз подряд для выполнения map/filter/reduce, а достаточно одной итерации. Дает прирост производительности.

Библиотека https://github.com/cognitect-labs/transducers-js

http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data http://jlongster.com/Transducers.js-Round-2-with-Benchmarks

https://medium.com/javascript-inside/introduction-into-channels-and-transducers-in-javascript-a1dfd0a09268#.q66hx0qmz

Last updated