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
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
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
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">
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
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!