@kajetansw
kajetan.dev
Kajetan Świątek
👨🎨 Front-end developer
🇵🇱 Wrocław
Why should you be interested in FP?
What is Elm?
Why should you at least try it?
Other FP languages that compile to JS
Elm vs TypeScript
Easier*
* subjective; relatively, in comparison
Harder*
Model
View
Update
render
message
Elm runtime
command
message
type alias Model = Int
init : Model
init = 0
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, text (String.fromInt model)
, button [ onClick Increment ] [ text "+" ]
]
type Msg
= Increment
| Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
Model
View
Update
render
message
getHero : String -> Cmd Msg
getHero url =
Http.get
{ url = url
, expect = Http.expectJson HeroReceived heroDecoder
}
Fetching data via HTTP requires data decoder
type Maybe a
= Just a
| Nothing
sanitize : String -> Maybe Int
sanitize input =
String.toInt (String.trim input)
getDefault : Maybe Int -> Int
getDefault input =
case input of
Just i -> i
Nothing -> 0
Sanitizing user's input
* disadvantage comes from the nature of JavaScript
Prevents runtime data inconsistency
(API changes, etc.)
No `null` and `undefined`
Immutability
No `any` or `unknown` type
Prevents poor typing from 3rd-party libraries
Easy to adopt in existing project
** mutability can be good when used properly (e.g. when comes to the performance)
fp-ts
@effect-ts/core
@kajetansw
kajetan.dev