SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
ELM - FUNKCIONÁLNÍ NEBE
PRO WEBOVÉVÝVOJÁŘE
Tomáš Látal
Barcamp Hradec Králové 2016
CODERETREAT
• “Do příště se naučím nový programovaní jazyk”
• Funkcionální
• Clojure / F# / Elm
ELM - CO SE MI LÍBILO
• FP pro web - kompilace do JS
• Virtual DOM (React)
• One-way data flow (Redux)
• Typy
ELM - CO BYLO NOVÉ
• Pouze Immutable
• Pouze Stateless (Pure) funkce
• No Runtime Exceptions
POROVNÁNÍ RYCHLOSTÍ
http://elm-lang.org/blog/blazing-fast-html-round-two
ELM - ÚVOD
HISTORIE
• 2012 - Evan Czaplicky
• inspirováno Haskellem
• v0.17
• v0.18 (cca listopad 2016)
SYNTAXE
1 + 1 -- 2
8 - 1 -- 7
10.5 * 2 -- 21
33 / 2 -- 16.5 s reálným dělením
33 // 2 -- 16 s celočíselným dělením
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True
"Text"
'a'
"Ahoj " ++ "světe!" -- "Ahoj světe!"
https://learnxinyminutes.com/docs/cs-cz/elm/
STRUKTURY
["jedna", "dva", "fizz", "čtyři"]
[1, 2, 3, 4, 5]
[1..5]
List
("elm", 42)
Tuple (n-tice)
STRUKTURY
Record
pocatek =
{ x = 0, y = 0, z = 0 }
{ x = 3, y = 7 }.x -- 3 .y { x = 3, y = 7 } -- 7
Přístup k datům
{ osoba |
jmeno = "Jiří" }
Změna hodnoty
ŘÍDÍCÍ STRUKTURY
if n < 0 then
"n je záporné"
else if n > 0 then
"n je kladné"
else
"n je nula"
case seznam of
[] -> "prázdný"
[x]-> “pouze s jednou položkou " ++ toString x
x::xs -> “více prvků, první je " ++ toString x
FUNKCE
vynasob a b =
a * b
vynasob 7 6 -- 42
zdvoj =
vynasob 2
List.map zdvoj [1..4] -- [2, 4, 6, 8]
FUNKCE
objem {sirka, delka, hloubka} =
let
obsah = sirka * delka
in
obsah * hloubka
objem { sirka = 3, delka = 2, hloubka = 7 } -- 42
FUNKCE
fib n =
if n < 2 then
1
else
fib (n - 1) + fib (n - 2)
List.map fib [0..8] -- [1, 1, 2, 3, 5, 8, 13, 21, 34]
FUNKCE
List.filter (num -> num < 15) (List.map fib [0..8])
-- [1, 1, 2, 3, 5, 8, 13]
[0..8]
|> List.map fib
|> List.filter (num -> num < 15)
TYPY
5 : Int
6.7 : Float
"ahoj" : String
True : Bool
type alias Bod3D =
{ x : Float, y : Float, z : Float }
TYPY U FUNKCÍ
fib : Int -> Int
fib n =
if n < 2 then
1
else
fib (n - 1) + fib (n - 2)
List.map : (a -> b) -> List a -> List b
List.map fib [0..8] -- [1, 1, 2, 3, 5, 8, 13, 21, 34]
UNIONTYPY
type Smer =
Sever | Jih | Vychod | Zapad
naStupne : Smer -> Float
naStupne smer =
case smer of
Sever -> 0.0
Vychod -> 90.0
Jih -> 180.0
Zapad -> 210.0
ELM ARCHITECTURE
• Model - stav aplikace
• Update - aktualizuje Model
• View - vykresluje Model (HTML/SVG)
COUNTER - MODEL
-- MODEL
type alias Model = Int
model : Model
model =
0
COUNTER - UPDATE
-- UPDATE
type Msg = Increment | Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
COUNTER -VIEW
-- VIEW
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick Increment ] [ text "+" ]
]
COUNTER - MAIN
main =
App.beginnerProgram
{model = model
,view = view
,update = update
}
DEMO
TOOLS
• Package manager
• Reactor
• Time-traveling debugger
• Elm format
CO MI ELM DAL
• Píšu víc Pure funkcí
• Snažím se pokrýt rovnou všechna větvení
programu
• Z funkcí nevracím null
• Nebojím se FP jazyků
DÍKY ZA POZORNOST
http://elm-lang.org
@TomasLatal

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Elm - Funkcionální nebe pro webové vývojáře

  • 1. ELM - FUNKCIONÁLNÍ NEBE PRO WEBOVÉVÝVOJÁŘE Tomáš Látal Barcamp Hradec Králové 2016
  • 2. CODERETREAT • “Do příště se naučím nový programovaní jazyk” • Funkcionální • Clojure / F# / Elm
  • 3. ELM - CO SE MI LÍBILO • FP pro web - kompilace do JS • Virtual DOM (React) • One-way data flow (Redux) • Typy
  • 4. ELM - CO BYLO NOVÉ • Pouze Immutable • Pouze Stateless (Pure) funkce • No Runtime Exceptions
  • 7. HISTORIE • 2012 - Evan Czaplicky • inspirováno Haskellem • v0.17 • v0.18 (cca listopad 2016)
  • 8. SYNTAXE 1 + 1 -- 2 8 - 1 -- 7 10.5 * 2 -- 21 33 / 2 -- 16.5 s reálným dělením 33 // 2 -- 16 s celočíselným dělením not True -- False not False -- True 1 == 1 -- True 1 /= 1 -- False 1 < 10 -- True "Text" 'a' "Ahoj " ++ "světe!" -- "Ahoj světe!" https://learnxinyminutes.com/docs/cs-cz/elm/
  • 9. STRUKTURY ["jedna", "dva", "fizz", "čtyři"] [1, 2, 3, 4, 5] [1..5] List ("elm", 42) Tuple (n-tice)
  • 10. STRUKTURY Record pocatek = { x = 0, y = 0, z = 0 } { x = 3, y = 7 }.x -- 3 .y { x = 3, y = 7 } -- 7 Přístup k datům { osoba | jmeno = "Jiří" } Změna hodnoty
  • 11. ŘÍDÍCÍ STRUKTURY if n < 0 then "n je záporné" else if n > 0 then "n je kladné" else "n je nula" case seznam of [] -> "prázdný" [x]-> “pouze s jednou položkou " ++ toString x x::xs -> “více prvků, první je " ++ toString x
  • 12. FUNKCE vynasob a b = a * b vynasob 7 6 -- 42 zdvoj = vynasob 2 List.map zdvoj [1..4] -- [2, 4, 6, 8]
  • 13. FUNKCE objem {sirka, delka, hloubka} = let obsah = sirka * delka in obsah * hloubka objem { sirka = 3, delka = 2, hloubka = 7 } -- 42
  • 14. FUNKCE fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2) List.map fib [0..8] -- [1, 1, 2, 3, 5, 8, 13, 21, 34]
  • 15. FUNKCE List.filter (num -> num < 15) (List.map fib [0..8]) -- [1, 1, 2, 3, 5, 8, 13] [0..8] |> List.map fib |> List.filter (num -> num < 15)
  • 16. TYPY 5 : Int 6.7 : Float "ahoj" : String True : Bool type alias Bod3D = { x : Float, y : Float, z : Float }
  • 17. TYPY U FUNKCÍ fib : Int -> Int fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2) List.map : (a -> b) -> List a -> List b List.map fib [0..8] -- [1, 1, 2, 3, 5, 8, 13, 21, 34]
  • 18. UNIONTYPY type Smer = Sever | Jih | Vychod | Zapad naStupne : Smer -> Float naStupne smer = case smer of Sever -> 0.0 Vychod -> 90.0 Jih -> 180.0 Zapad -> 210.0
  • 19. ELM ARCHITECTURE • Model - stav aplikace • Update - aktualizuje Model • View - vykresluje Model (HTML/SVG)
  • 20. COUNTER - MODEL -- MODEL type alias Model = Int model : Model model = 0
  • 21. COUNTER - UPDATE -- UPDATE type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1
  • 22. COUNTER -VIEW -- VIEW view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]
  • 23. COUNTER - MAIN main = App.beginnerProgram {model = model ,view = view ,update = update }
  • 24. DEMO
  • 25. TOOLS • Package manager • Reactor • Time-traveling debugger • Elm format
  • 26. CO MI ELM DAL • Píšu víc Pure funkcí • Snažím se pokrýt rovnou všechna větvení programu • Z funkcí nevracím null • Nebojím se FP jazyků