SlideShare una empresa de Scribd logo
1 de 52
Descargar para leer sin conexión
Luca Mugnaini
Functional Programming
and Elm
Functional Programming
Originated from lambda calculus developed in
the 1930s by Alonzo Church
What is Functional Programming?
Programming with
mathematical functions*
*It doesn’t mean you need to know math
• Do this, then do that
• Any effect (Unsafe)
• Commands (statements)
• Control Flow
• Name of a location
Imperative
• No notion of sequence
• Limited effect (Safe)
• Expressions
• Data Flow
• Name of a value
vs. Functional
f : a à World à (World, b)
If there are no side-effects, how can
we change the state of the world?
f... takes in the state of the
world and returns a new
world, thus remaining pure
No Side-effects
update : Msg à Model à ( Model, Cmd Msg )
https://channel9.msdn.com/Shows/Going+Deep/Erik-Meijer-Functional-Programming
Functional-flavored
• Lisp 1958 (Scheme, Clojure,
Racket)
• ML ”Lisp with types” =>
Ocaml (F#, Scala, Reason)
• Erlang (Elixir)
• Javascript, Java 8, Kotlin,
Perl, Python, PHP, etc.
Functional-first, Pure
• Haskell (Elm)
Nirvana
“Haskell is useless”
Simon Peyton Jones
Honesty/Dishonesty
Erik Meijer
https://www.youtube.com/watch?v=iSmkqocn0oQ&t=22s
https://www.youtube.com/watch?v=z0N1aZ6SnBk&t=533
https://www.infoq.com/presentations/Taming-Effect-Simon-Peyton-Jones
Web Development
Web Development Fatigue3
Javascript fatigue +
CSS fatigue +
Framework/Npm fatigue =
Web
Development
Fatigue
What if I told you
Front-end can be done
without Javascript, HTML, CSS?
Elm
● Pure Functional Language
● Compiles to Javascript
● Statically typed + inferred
● Support Immutability and Currying
● Virtual DOM
What is Elm?
● Designed by Evan Czaplicki
● April 2012: Version 0.1
● August 2018: Latest version 0.19
● Stable (21 months since 0.18)
● Community in elmlang.slack.com
History
● No loops (“i = i + 1” doesn’t compile)
● No “null” and no “undefined”
● No “return”, everything is an expression
● “if” always need “else”
● No “this”, no callbacks, no closures, no hoisting
● No automatic type conversion
● JSON decoder/encoder instead of stringify/parse
Differences with Javascript
Javascript Elm
npm / yarn Built in
React / Angular / Vue
Flux / Redux / MobX
Built in
Built in
Immutable.js
Typescript / Flow
Built in
Built in
Without HTML, CSS: elm-ui
row [ padding 10, spacing 7 ]
[ el [] none
, el [] none
, el [] none
]
spacing : Distance between children
padding : Parent Padding
[ centerX
, centerY
]
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform:
translate(-50%, -50%);
}
CSS elm-ui
No more CSS tricks
10
nice
things
about
Elm
Nice thing #1
Helpful, friendly errors
Nice thing #2
No runtime exceptions
Nice thing #3
Performance
Nice thing #4
Enforced Semantic
Nice thing #5
Time-travelling debugger
Nice thing #6
Type inference
Nice thing #7
elm-format
Nice thing #8
Demystify FP concepts
Functors
Monads Category Theory
Algebraic Data Type
The Elm Architecture is a simple pattern for
architecting webapps. It is made of thee parts
Nice thing #9
The Elm Architecture (TEA)
Model the state of your application
View a way to view your state as HTML
Update a way to update your state
28
Cartoon cute unicorns vectors design by loutpany from https://freedesignfile.com
Safe AreaUnsafe Area
Elm Runtime
DOM
MsgModel
Model
Model
Html
Model
New!
Effects
http requests,
ports Cmd
EventEvent!
New!
New!
Cmd
DOM
Html
EventEvent!
update
view
[1,2] + [3,4] = ?
[1,2] + [3,4] = ?
Error [1,2,3,4]
[4,6] “1,23,4”
A B
C D
"1,23,4"
Javascript Elm
-- TYPE MISMATCH ---------------------
I cannot do addition with lists:
[1,2] + [3,4]
^^^^^
The left side of (+) is a list of type:
List number
But (+) only works with Int and Float
values.
Hint: Switch to the (++) operator to
append lists!
[1,2] + [3,4] = ?
What can go wrong with
automatic type conversion?
https://itnext.io/things-that-can-go-wrong-without-a-strictly-typed-language-d91d418a53a1
“If you want user input to be automatically typecast as a
number, you can add the number modifier to your v-
model managed inputs”
<input type="number" v-model="product.quantity">
<input type="number" v-model.number="product.quantity">
totalProducts() {
return this.products.reduce((sum, product) => {
return sum + product.quantity
}, 0)
}
totalProducts() {
return this.products.reduce((sum, product) => {
if (typeof product.quantity === "number") {
return sum + product.quantity;
} else {
return sum;
}
}, 0)
}
<button @click="product.quantity += 1">
<button @click="product.quantity = Number(product.quantity) + 1">
Now it works but...
type alias Product =
{ id : Int
, quantity : Int
, name : String
}
changeQuantity : Product -> String -> Product
changeQuantity product newQuantity =
{ product
| quantity =
Maybe.withDefault product.quantity
(String.toInt newQuantity)
}
“If the user typed a valid number, replace the old number with the
new one, otherwise keep the old number”
Typos <input v-model.number="product.quanity">
t
Elm
Fail silently
Input fields are
empty but buttons
work.
-- TYPE MISMATCH --------------------
The 2nd argument to `map` is not what I
expect:
84| products
^^^^^^^^
This `products` value is a:
List Product
But `map` needs the 2nd argument to be:
List { a | id : Int, name : String, quanity
: Int, quantity : Int }
Hint: Seems like a record field typo. Maybe
quanity should be quantity?
Vue
C de
module Main exposing (main)
import Html exposing (..)
import Html.Attributes exposing (..)
main =
div []
[ button [id "counter"] [ text "+1" ]
, div [] [ text <| String.fromInt 0 ]
, button [] [ text ”-1" ]
]
<div id="counter">
<button>+1</button>
<div>0</div>
<button>-1</button>
</div>
-- MODEL
type alias Model =
{ count : Int }
init : Model
init =
{ count = 0 }
-- UPDATE
type Msg
= Increment
| Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
{ model | count = model.count + 1 }
Decrement ->
{ model | count = model.count - 1 }
-- VIEW
view : Model -> Html Msg
view model =
layout [] <|
column []
[ button []
{ onPress = Just Increment
, label = text "+1"
}
, el [] <|
text (String.fromInt model.count)
, button []
{ onPress = Just Decrement
, label = text "-1"
}
]
main : Program () Model Msg
main =
Browser.sandbox
{ init = init
, view = view
, update = update
}
Examples
ellie-app.comhttps://ellie-app.com/4ddCRdyx4Rta1
www.nsb.no
unsoundscapes.itch.io/cubik
malax.github.io/elmboy
● The ideas of Functional Programming are
becoming mainstream (for a reason)
● Learning Elm will make you a better [Javascript,
React, Angular, Vue, etc.] developer
● Writing pure code is an enjoyable experience
that make front-end development fun again!

Más contenido relacionado

La actualidad más candente

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4Shameer Ahmed Koya
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingAndraž Bajt
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptxsandeep54552
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 

La actualidad más candente (20)

Loop Statements [5] M
Loop Statements [5] MLoop Statements [5] M
Loop Statements [5] M
 
GNU octave
GNU octaveGNU octave
GNU octave
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Stack
StackStack
Stack
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
Queue
QueueQueue
Queue
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar a Functional programming and Elm

Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional ProgrammingYuan Wang
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVAMuskanSony
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)Spiros
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshopVinay Kumar
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#Bertrand Le Roy
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Dadangsachir WANDA ir.mba
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010Skills Matter
 

Similar a Functional programming and Elm (20)

Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Functional go
Functional goFunctional go
Functional go
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 

Más de Fangda Wang

[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?Fangda Wang
 
Under the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeedUnder the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeedFangda Wang
 
How Indeed asks coding interview questions
How Indeed asks coding interview questionsHow Indeed asks coding interview questions
How Indeed asks coding interview questionsFangda Wang
 
Types are eating the world
Types are eating the worldTypes are eating the world
Types are eating the worldFangda Wang
 
From ic to tech lead
From ic to tech leadFrom ic to tech lead
From ic to tech leadFangda Wang
 
Introduction to japanese tokenizer
Introduction to japanese tokenizerIntroduction to japanese tokenizer
Introduction to japanese tokenizerFangda Wang
 
Gentle Introduction to Scala
Gentle Introduction to ScalaGentle Introduction to Scala
Gentle Introduction to ScalaFangda Wang
 
To pair or not to pair
To pair or not to pairTo pair or not to pair
To pair or not to pairFangda Wang
 
Elm at large (companies)
Elm at large (companies)Elm at large (companies)
Elm at large (companies)Fangda Wang
 
Data science tools of the trade
Data science tools of the tradeData science tools of the trade
Data science tools of the tradeFangda Wang
 

Más de Fangda Wang (11)

[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?
 
Under the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeedUnder the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeed
 
How Indeed asks coding interview questions
How Indeed asks coding interview questionsHow Indeed asks coding interview questions
How Indeed asks coding interview questions
 
Types are eating the world
Types are eating the worldTypes are eating the world
Types are eating the world
 
From ic to tech lead
From ic to tech leadFrom ic to tech lead
From ic to tech lead
 
Introduction to japanese tokenizer
Introduction to japanese tokenizerIntroduction to japanese tokenizer
Introduction to japanese tokenizer
 
Gentle Introduction to Scala
Gentle Introduction to ScalaGentle Introduction to Scala
Gentle Introduction to Scala
 
To pair or not to pair
To pair or not to pairTo pair or not to pair
To pair or not to pair
 
Balanced Team
Balanced TeamBalanced Team
Balanced Team
 
Elm at large (companies)
Elm at large (companies)Elm at large (companies)
Elm at large (companies)
 
Data science tools of the trade
Data science tools of the tradeData science tools of the trade
Data science tools of the trade
 

Último

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 

Último (20)

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 

Functional programming and Elm

  • 3. Originated from lambda calculus developed in the 1930s by Alonzo Church What is Functional Programming? Programming with mathematical functions* *It doesn’t mean you need to know math
  • 4. • Do this, then do that • Any effect (Unsafe) • Commands (statements) • Control Flow • Name of a location Imperative • No notion of sequence • Limited effect (Safe) • Expressions • Data Flow • Name of a value vs. Functional
  • 5. f : a à World à (World, b) If there are no side-effects, how can we change the state of the world? f... takes in the state of the world and returns a new world, thus remaining pure No Side-effects update : Msg à Model à ( Model, Cmd Msg ) https://channel9.msdn.com/Shows/Going+Deep/Erik-Meijer-Functional-Programming
  • 6. Functional-flavored • Lisp 1958 (Scheme, Clojure, Racket) • ML ”Lisp with types” => Ocaml (F#, Scala, Reason) • Erlang (Elixir) • Javascript, Java 8, Kotlin, Perl, Python, PHP, etc. Functional-first, Pure • Haskell (Elm)
  • 7. Nirvana “Haskell is useless” Simon Peyton Jones Honesty/Dishonesty Erik Meijer https://www.youtube.com/watch?v=iSmkqocn0oQ&t=22s https://www.youtube.com/watch?v=z0N1aZ6SnBk&t=533 https://www.infoq.com/presentations/Taming-Effect-Simon-Peyton-Jones
  • 9. Web Development Fatigue3 Javascript fatigue + CSS fatigue + Framework/Npm fatigue = Web Development Fatigue
  • 10. What if I told you Front-end can be done without Javascript, HTML, CSS?
  • 11. Elm
  • 12. ● Pure Functional Language ● Compiles to Javascript ● Statically typed + inferred ● Support Immutability and Currying ● Virtual DOM What is Elm?
  • 13. ● Designed by Evan Czaplicki ● April 2012: Version 0.1 ● August 2018: Latest version 0.19 ● Stable (21 months since 0.18) ● Community in elmlang.slack.com History
  • 14. ● No loops (“i = i + 1” doesn’t compile) ● No “null” and no “undefined” ● No “return”, everything is an expression ● “if” always need “else” ● No “this”, no callbacks, no closures, no hoisting ● No automatic type conversion ● JSON decoder/encoder instead of stringify/parse Differences with Javascript
  • 15. Javascript Elm npm / yarn Built in React / Angular / Vue Flux / Redux / MobX Built in Built in Immutable.js Typescript / Flow Built in Built in
  • 16. Without HTML, CSS: elm-ui row [ padding 10, spacing 7 ] [ el [] none , el [] none , el [] none ] spacing : Distance between children padding : Parent Padding
  • 17. [ centerX , centerY ] .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } CSS elm-ui No more CSS tricks
  • 19. Nice thing #1 Helpful, friendly errors
  • 20. Nice thing #2 No runtime exceptions
  • 24. Nice thing #6 Type inference
  • 26. Nice thing #8 Demystify FP concepts Functors Monads Category Theory Algebraic Data Type
  • 27. The Elm Architecture is a simple pattern for architecting webapps. It is made of thee parts Nice thing #9 The Elm Architecture (TEA) Model the state of your application View a way to view your state as HTML Update a way to update your state
  • 28. 28 Cartoon cute unicorns vectors design by loutpany from https://freedesignfile.com
  • 29. Safe AreaUnsafe Area Elm Runtime DOM MsgModel Model Model Html Model New! Effects http requests, ports Cmd EventEvent! New! New! Cmd DOM Html EventEvent! update view
  • 31. [1,2] + [3,4] = ? Error [1,2,3,4] [4,6] “1,23,4” A B C D
  • 32. "1,23,4" Javascript Elm -- TYPE MISMATCH --------------------- I cannot do addition with lists: [1,2] + [3,4] ^^^^^ The left side of (+) is a list of type: List number But (+) only works with Int and Float values. Hint: Switch to the (++) operator to append lists! [1,2] + [3,4] = ?
  • 33. What can go wrong with automatic type conversion? https://itnext.io/things-that-can-go-wrong-without-a-strictly-typed-language-d91d418a53a1
  • 34.
  • 35. “If you want user input to be automatically typecast as a number, you can add the number modifier to your v- model managed inputs” <input type="number" v-model="product.quantity"> <input type="number" v-model.number="product.quantity">
  • 36. totalProducts() { return this.products.reduce((sum, product) => { return sum + product.quantity }, 0) } totalProducts() { return this.products.reduce((sum, product) => { if (typeof product.quantity === "number") { return sum + product.quantity; } else { return sum; } }, 0) }
  • 37. <button @click="product.quantity += 1"> <button @click="product.quantity = Number(product.quantity) + 1">
  • 38. Now it works but...
  • 39. type alias Product = { id : Int , quantity : Int , name : String } changeQuantity : Product -> String -> Product changeQuantity product newQuantity = { product | quantity = Maybe.withDefault product.quantity (String.toInt newQuantity) } “If the user typed a valid number, replace the old number with the new one, otherwise keep the old number”
  • 40. Typos <input v-model.number="product.quanity"> t Elm Fail silently Input fields are empty but buttons work. -- TYPE MISMATCH -------------------- The 2nd argument to `map` is not what I expect: 84| products ^^^^^^^^ This `products` value is a: List Product But `map` needs the 2nd argument to be: List { a | id : Int, name : String, quanity : Int, quantity : Int } Hint: Seems like a record field typo. Maybe quanity should be quantity? Vue
  • 41. C de
  • 42. module Main exposing (main) import Html exposing (..) import Html.Attributes exposing (..) main = div [] [ button [id "counter"] [ text "+1" ] , div [] [ text <| String.fromInt 0 ] , button [] [ text ”-1" ] ] <div id="counter"> <button>+1</button> <div>0</div> <button>-1</button> </div>
  • 43. -- MODEL type alias Model = { count : Int } init : Model init = { count = 0 }
  • 44. -- UPDATE type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> { model | count = model.count + 1 } Decrement -> { model | count = model.count - 1 }
  • 45. -- VIEW view : Model -> Html Msg view model = layout [] <| column [] [ button [] { onPress = Just Increment , label = text "+1" } , el [] <| text (String.fromInt model.count) , button [] { onPress = Just Decrement , label = text "-1" } ]
  • 46. main : Program () Model Msg main = Browser.sandbox { init = init , view = view , update = update }
  • 52. ● The ideas of Functional Programming are becoming mainstream (for a reason) ● Learning Elm will make you a better [Javascript, React, Angular, Vue, etc.] developer ● Writing pure code is an enjoyable experience that make front-end development fun again!