SlideShare uma empresa Scribd logo
1 de 71
struktury
algebraiczne
w js
marek
● JS Developer @ BH for 4+ years
● FP, JS, react, elixir, haskell
● intercaetera.com
po co?
jest z tym problem
← to jest trudne
problem → → rozwiązanie
problem → → rozwiązanie
struktury algebraiczne
- rozwiązują ogólne problemy*
- odkryte racjonalnie**
- bardzo rygorystyczne***
intuicja
- do budowania systemów
- do naprawiania istniejących aplikacji
- do refaktoryzacji
- do rozwiązywania bugów
← potrzebna tu
co to?
- zbiór
- operacja na nim
- zasady które spełnia
- typ
- funkcja na nim
- zasady które spełnia
</>
(
notacja typów
- Int, String — typy
- n :: Type — n należy do typu Type
- Constr a — konstruktor/generyk (jak T<a>)
- a -> b — funkcja
- a -> b -> c — funkcje są curry’owane
f = (a, b) => a+b ~= g = a => b => a + b
f(2, 3) => 5 ~= g(2)(3) => 5
currying
ściąga na przyszłość
https://github.com/fantasyland/fantasy-land
https://github.com/fantasyland/static-land
)
funktory <$>
Int -> Bool
() -> Int
Identity Int
Dict String Int
List Int
Maybe Int
Int -> Bool
() -> Int
Identity Int
Dict String Int
List Int
Maybe Int
a -> Bool
() -> a
Identity a
Dict String a
List a
Maybe a
monady funktory są
jak burrito 🌯
zróbmy nasz własny
Effect a
() -> a
1 :: Number
() => 1 :: Effect Number
() => 2 :: Effect Number
() => ‘alpha’ :: Effect String
() => [1, 2, 3] :: Effect List Number
funktor = typ + fmap
fmap f :: (a -> b) -> f a -> f b
Array.prototype.map
fmap Array :: (a -> b) -> Array a -> Array b
[1, 2, 3].map(x => x*2) => [2, 4, 6]
fmap Effect :: (a -> b) -> Effect a -> Effect b
różne funktory
- kowariantne
- kontrawariantne
- bifunktory i profunktory
- transformacje naturalne
- aplikatywy
- funktory monoidalne
- …monady?
monoid <>
monoidy nie są funktorami
monoid = typ + concat + empty
typ: Int
concat: +
empty: 0
typ: Int
concat: *
empty: 1
monoidy mogą być funktorami
typ: Array
concat: Array.prototype.concat
empty: []
monoidy mogą być funkcjami
typ: a -> a
concat: _.flow
empty: x => x
← czyli po prostu składanie
funkcji
ale przede wszystkim
monoidy są redukowalne
array.reduce(reducer, initial)
[type].reduce(concat, empty)
jak zredukować Effect?
concat f :: f -> f -> f
zwykle
concat Effect :: Effect -> Effect -> Effect
można tak, ale to niezbyt
praktyczne
w programowaniu funkcyjnym
chodzi o składanie funkcji
jak mamy takie same typy
to nie ma problemu
(a → a) ° (a → a) ⇒ (a → a)
(Int → Int) ° (Int → Int) ⇒ (Int → Int)
ale jak nie, to funkcje muszą się
zgadzać w typach
(a → b) ° (b → c) ⇒ (a → c)
(Int → String) ° (String → Bool) ⇒ (Int → Bool)
ale co jeśli chcielibyśmy złożyć
2 × a -> Effect a
compose :: (a -> b) -> (b -> c) -> (a -> c)
concat (a -> Effect c) ::
(a -> Effect b) -> (b -> Effect c) -> (a -> Effect c)
ale co my teraz zrobiliśmy?
monoid w
endofunktorze
“All told, a monad is just a
monoid in the category of
endofunctors.”
monada =<<
monada = funktor + chain
chain f :: (a -> f b) -> f a -> f b
Array.prototype.flatMap
chain Effect :: (a -> Effect b) -> Effect a -> Effect b
funkcje = kompozycja
monady = ✨kompozycja✨
“Once you understand monads, you
lose the ability to explain them to
others.”
mam nadzieję że poszło mi w
miarę ok

Mais conteúdo relacionado

Mais de Brainhub

The hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivityThe hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivityBrainhub
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wildBrainhub
 
WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?Brainhub
 
React performance
React performanceReact performance
React performanceBrainhub
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
React Native in a nutshell
React Native in a nutshellReact Native in a nutshell
React Native in a nutshellBrainhub
 
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)Brainhub
 
Technologia, a Startup - Brainhub
Technologia, a Startup - BrainhubTechnologia, a Startup - Brainhub
Technologia, a Startup - BrainhubBrainhub
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLBrainhub
 
How should you React to Redux
How should you React to ReduxHow should you React to Redux
How should you React to ReduxBrainhub
 
Wprowadzenie do React
Wprowadzenie do ReactWprowadzenie do React
Wprowadzenie do ReactBrainhub
 
JavaScript and Desktop Apps - Introduction to Electron
JavaScript and Desktop Apps - Introduction to ElectronJavaScript and Desktop Apps - Introduction to Electron
JavaScript and Desktop Apps - Introduction to ElectronBrainhub
 
Why and How You Should Move from PHP to Node.js
Why and How You Should Move from PHP to Node.jsWhy and How You Should Move from PHP to Node.js
Why and How You Should Move from PHP to Node.jsBrainhub
 
All you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsAll you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsBrainhub
 
Lexical scope, function vs. block scope, hoisting, scope closures
Lexical scope, function vs. block scope, hoisting, scope closuresLexical scope, function vs. block scope, hoisting, scope closures
Lexical scope, function vs. block scope, hoisting, scope closuresBrainhub
 

Mais de Brainhub (15)

The hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivityThe hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivity
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
 
WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?
 
React performance
React performanceReact performance
React performance
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
React Native in a nutshell
React Native in a nutshellReact Native in a nutshell
React Native in a nutshell
 
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
 
Technologia, a Startup - Brainhub
Technologia, a Startup - BrainhubTechnologia, a Startup - Brainhub
Technologia, a Startup - Brainhub
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
How should you React to Redux
How should you React to ReduxHow should you React to Redux
How should you React to Redux
 
Wprowadzenie do React
Wprowadzenie do ReactWprowadzenie do React
Wprowadzenie do React
 
JavaScript and Desktop Apps - Introduction to Electron
JavaScript and Desktop Apps - Introduction to ElectronJavaScript and Desktop Apps - Introduction to Electron
JavaScript and Desktop Apps - Introduction to Electron
 
Why and How You Should Move from PHP to Node.js
Why and How You Should Move from PHP to Node.jsWhy and How You Should Move from PHP to Node.js
Why and How You Should Move from PHP to Node.js
 
All you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsAll you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, Generators
 
Lexical scope, function vs. block scope, hoisting, scope closures
Lexical scope, function vs. block scope, hoisting, scope closuresLexical scope, function vs. block scope, hoisting, scope closures
Lexical scope, function vs. block scope, hoisting, scope closures
 

Struktury algebraiczne w JavaScripcie