Slide 1

Slide 1 text

L e a r n i n g E l m i n J S 2 0 1 8 . 3 . 2 0 R o p p o n g i . j s # 1

Slide 2

Slide 2 text

• Front-end engineer at PERSOL • GitHub: boiyaa
 Twitter: boiyaaaaaa

Slide 3

Slide 3 text

T h e o b j e c t o f t h i s s l i d e By emulating Elm code in JavaScript, • Know Elm syntax • Know what Elm does • Learn better JS code from Elm

Slide 4

Slide 4 text

• Purely functional language for front-end web • Compiles to JS • Type-safe and non-nullable • Declarative UI building like React • Single source of truth like Redux

Slide 5

Slide 5 text

C o u n t e r A p p

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

( . . ) m e a n s t h i s m o d u l e e x p o r t s a l l d e f i n i t i o n s

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

F u n c t i o n t h a t t a k e s “ m o d e l ” a s a n a rg u m e n t

Slide 14

Slide 14 text

C u r r i e d f u n c t i o n t h a t t a k e s “ m s g ” a n d “ m o d e l ” a s a rg u m e n t s

Slide 15

Slide 15 text

L o o k s l i k e c o n s t a n t B u t f u n c t i o n t h a t t a k e s n o a rg u m e n t s

Slide 16

Slide 16 text

E l m d o e s n o t h a v e v a r i a b l e s , c o n s t a n t s , a n d c l a s s . A n d e v e r y t h i n g i s a n e x p re s s i o n . T h a t i s w h y f e w k e y w o rd s s u c h a s v a r, l e t , c o n s t , f u n c t i o n , a n d re t u r n .

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

“ V i e w ” f u n c t i o n • Returns HTML structure defined declaratively

Slide 19

Slide 19 text

A f u n c t i o n a p p l i c a t i o n i n E l m i s u s e d w h i t e s p a c e i n s t e a d o f p a re n t h e s i s b e c a u s e e v e r y f u n c t i o n s a re c u r r i e d “ V i e w ” f u n c t i o n

Slide 20

Slide 20 text

J S c u r r i e d f u n c t i o n s a re h a rd t o re a d “ V i e w ” f u n c t i o n

Slide 21

Slide 21 text

J S X l o o k s b e t t e r “ V i e w ” f u n c t i o n

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

• Messages are things that happen (Action in Redux) • Update function is called when message is received and returns updated app state (Reducer in Redux) “ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n

Slide 24

Slide 24 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n

Slide 25

Slide 25 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n U n i o n t y p e s C a s e e x p re s s i o n

Slide 26

Slide 26 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n U n i o n t y p e s C a s e e x p re s s i o n D a t a c o n s t r u c t o r

Slide 27

Slide 27 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n U n i o n t y p e s C a s e e x p re s s i o n D a t a c o n s t r u c t o r c a n b e u s e d a s a f i r s t c l a s s v a l u e . B y u s i n g u n i o n t y p e s a n d c a s e e x p re s s i o n , c o m p i l e r f a i l s w h e n p a t t e r n s d o n o t c o v e r a l l c a s e s D a t a c o n s t r u c t o r

Slide 28

Slide 28 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n U n a b l e t o e m u l a t e i n J S b e c a u s e o f u n s u p p o r t i n g t h e m . U n i o n t y p e s i n Ty p e S c r i p t a n d F l o w a re t h e s e t o f o t h e r t y p e s , a re n o t f i r s t c l a s s , a n d c a n n o t b e u s e d p a t t e r n m a t c h i n g .

Slide 29

Slide 29 text

F o r m A p p

Slide 30

Slide 30 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n C o d e g e t s f a t t e r i f t h e c o n s t r u c t o r h a s a n a rg u m e n t s C o d e i s n e s t e d e a c h t i m e a n c o n s t r u c t o r i s a d d e d

Slide 31

Slide 31 text

“ M e s s a g e s ” a n d “ U p d a t e ” f u n c t i o n R e d u x w a y

Slide 32

Slide 32 text

C o n c l u s i o n s • Difficult to emulate Elm’s robustness in JS • Possible to adopt what everything is an expression and reduce coding patterns by restricting some statements and return. • Necessary to care about what it is hard to ensure the robustness in TypeScript or Flow due to using JS modules and using the any type.

Slide 33

Slide 33 text

T h a n k y o u