Upgrade to Pro — share decks privately, control downloads, hide ads and more …

You Should Try Elixir

Nathan
November 06, 2015

You Should Try Elixir

Talk at YEGRB, December 2014

Nathan

November 06, 2015
Tweet

More Decks by Nathan

Other Decks in Programming

Transcript

  1. Example: FizzBuzz f i z z b u z z

    ( n ) = F i z z B u z z i f r e m ( n / 3 ) = 0 a n d r e m ( n / 5 ) = 0 F i z z i f r e m ( n / 3 ) = 0 B u z z i f r e m ( n / 5 ) = 0 n {
  2. | Example: FizzBuzz d e f m o d u

    l e F i z z B u z z d o d e f f i z z _ w o r d ( 0 , 0 , _ ) , d o : " F i z z B u z z " d e f f i z z _ w o r d ( 0 , _ , _ ) , d o : " F i z z " d e f f i z z _ w o r d ( _ , 0 , _ ) , d o : " B u z z " d e f f i z z _ w o r d ( _ , _ , n ) , d o : n d e f f i z z b u z z ( n ) d o f i z z _ w o r d ( r e m ( n , 3 ) , r e m ( n , 5 ) , n ) e n d d e f p r i n t ( n ) d o 1 . . n | > E n u m . m a p ( f n ( n ) - > f i z z b u z z ( n ) e n d ) | > E n u m . j o i n ( " , " ) | > I O . p u t s e n d e n d F i z z B u z z . p r i n t ( 1 0 0 ) Pattern Matching Pipe Operator
  3. Elixir has changed the way I think about programming. Not

    just in terms of "Oh, its a functional language", it's actually changed my conception of what it means to program. ~ Dave Thomas “
  4. Elixir has changed the way I think about programming. Not

    just in terms of "Oh, its a functional language", it's actually changed my conception of what it means to program. ~ Dave Thomas “ Taken from: Think Different
  5. I want to long term use [Elixir and the Phoenix

    Framework] to replace all of my client work and everything I want to build personally ~ Chris McCord “ Taken from: Rise of The Phoenix - Building an Elixir Web Framework
  6. Programming in Erlang sucks. Elixir is everything good about Erlang

    and none — almost none — of the bad. ~ Devin Torres “ Taken from: The Excitement of Elixir
  7. Erlang Erlang was originally developed in 1986 by Ericsson for

    telephony applications to support highly concurrent systems Mature Language/Tools/Community Concurrency (OTP) Distributed (OTP) Fault Tolerant Hot code swapping
  8. Erlang libraries are simple to use! Elixir doesn't have built-in

    random or time libraries (although there are multiple packages). Let's use Erlang's libraries: d e f m o d u l e R a n d o m d o d e f u n i f o r m d o : r a n d o m . u n i f o r m e n d d e f s e e d d o : r a n d o m . s e e d ( : e r l a n g . n o w ) e n d e n d
  9. It's Easy to Get Started Ruby-like Syntax Programming Elixir –

    Dave Thomas elixir-lang.org/getting_started/1.html Exercism.io
  10. Great Toolset for a Young Language b r e w

    i n s t a l l e l i x i r Rake ~> Mix Bundler ~> Hex IRb ~> IEx RSpec ~> ExUnit
  11. Metaprogramming d e f m a c r o m

    a t c h ? ( l e f t , r i g h t ) d o q u o t e d o c a s e u n q u o t e ( r i g h t ) d o u n q u o t e ( l e f t ) - > t r u e _ - > f a l s e e n d e n d e n d i e x > l i s t = [ { : a , 1 } , { : b , 2 } , { : a , 3 } ] [ a : 1 , b : 2 , a : 3 ] i e x > E n u m . f i l t e r l i s t , f n ( t h i n g ) - > m a t c h ? ( { : a , _ } , t h i n g ) e n d [ a : 1 , a : 3 ] Taken from: Elixir: It's Not About Syntax
  12. Okay, I Want to Try Elixir! b r e w

    i n s t a l l e l i x i r Programming Elixir – Dave Thomas Distribution/Concurrency Tutorial: elixir-lang.org Exercism.io How I Start. Elixir With José Valim: Portal
  13. Head | Tail Recursion d e f m o d

    u l e M a t h d o d e f s u m _ l i s t ( [ h e a d | t a i l ] , a c c u m u l a t o r ) d o s u m _ l i s t ( t a i l , h e a d + a c c u m u l a t o r ) e n d d e f s u m _ l i s t ( [ ] , a c c u m u l a t o r ) d o a c c u m u l a t o r e n d e n d M a t h . s u m _ l i s t ( [ 1 , 2 , 3 ] , 0 ) # = > 6 Taken from: Elixir Getting Started: Recursion
  14. Concurrency/Distribution (OTP) # C o m p i l e

    t h e m o d u l e a n d c r e a t e a p r o c e s s t h a t e v a l u a t e s ` a r e a _ l o o p ` i n t h e s h e l l p i d = s p a w n ( f n - > G e o m e t r y . a r e a _ l o o p ( ) e n d ) # = > # P I D < 0 . 4 0 . 0 > # S e n d a m e s s a g e t o ` p i d ` t h a t w i l l m a t c h a p a t t e r n i n t h e r e c e i v e s t a t e m e n t s e n d p i d , { : r e c t a n g l e , 2 , 3 } # = > A r e a = 6 # { : r e c t a n g l e , 2 , 3 } s e n d p i d , { : c i r c l e , 2 } # = > A r e a = 1 2 . 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 4 9 7 3 8 # { : c i r c l e , 2 } # T h e s h e l l i s a l s o a p r o c e s s , y o u c a n u s e ` s e l f ` t o g e t t h e c u r r e n t p i d s e l f ( ) # = > # P I D < 0 . 2 7 . 0 > Taken from: Learn X in Y Minutes - Elixir
  15. Pattern Matching i e x > a = [ 1

    , 2 , 3 ] i e x > b = [ [ 1 , 2 , 3 ] ] i e x > [ c ] = [ [ 1 , 2 , 3 ] ] i e x > a = = c t r u e i e x > [ x , y , z ] = [ 1 , 2 , 3 ] i e x > z 3 i e x > " T o d a y i s " < > d a t e = " T o d a y i s D e c e m b e r 1 6 t h " i e x > d a t e " D e c e m b e r 1 6 t h " d e f m o d u l e M y A p p d o d e f c a l l b a c k ( : o k , d a t a ) d o # s u c c e s s e n d d e f c a l l b a c k ( : e r r o r , d a t a ) d o # h a n d l e f a i l u r e e n d e n d Back To FizzBuzz
  16. Pipe Operator The pipe operator(| > ) is most helpful

    when transforming data through a sequence. The operator pipes the result of an expression into the first argument of the next function. Confusing, nested function calls no longer need temporary variables to make them readable: # C o n f u s i n g , n e s t e d f u n c t i o n c a l l s d e f p r i n t ( n ) d o I O . p u t s ( E n u m . j o i n ( E n u m . m a p ( 1 . . n , f n ( n ) - > f i z z b u z z ( n ) e n d ) , " , " ) ) e n d # v e r s u s u s i n g t h e p i p e o p e r a t o r d e f p r i n t ( n ) d o 1 . . n | > E n u m . m a p ( f n ( n ) - > f i z z b u z z ( n ) e n d ) | > E n u m . j o i n ( " , " ) | > I O . p u t s e n d # I n R u b y d e f p r i n t ( n ) p u t s 1 . . n . m a p d o f i z z b u z z ( n ) e n d . j o i n ( " , " ) e n d
  17. Updating Records Functionally? i t ' r e l e

    a s e s o n e i n v i t e c r e d i t w h e n i t i s w i t h d r a w n ' d o s e n d e r = @ i n v i t e . s e n d e r e x p e c t { @ i n v i t e . w i t h d r a w ; s e n d e r . r e l o a d } . t o c h a n g e { s e n d e r . a v a i l a b l e _ i n v i t e _ c r e d i t s . l e n g t h } . b y ( 1 ) e n d i t ' r e l e a s e s o n e i n v i t e c r e d i t w h e n i t i s w i t h d r a w n ' d o s e n d e r = @ i n v i t e . s e n d e r e x p e c t { @ i n v i t e . w i t h d r a w } . t o c h a n g e { U s e r . a v a i l a b l e _ i n v i t e _ c r e d i t s ( s e n d e r . i d ) . l e n g t h } . b y ( 1 ) e n d
  18. Another Attempt # f i n d t h e

    a u t h o r c o u n t s a u t h o r _ c o u n t s = [ ] a u t h o r _ c o u n t s _ i n d e x = - 1 c u r r e n t _ a u t h o r = ' ' f o r i i n 0 . . . c o m m i t s . l e n g t h c o m m i t = c o m m i t s [ i ] i f c o m m i t . a u t h o r = = c u r r e n t _ a u t h o r a u t h o r _ c o u n t s [ a u t h o r _ c o u n t s _ i n d e x ] [ 1 ] + = 1 e l s e c u r r e n t _ a u t h o r = c o m m i t . a u t h o r a u t h o r _ c o u n t s < < [ c o m m i t . a u t h o r , 1 ] a u t h o r _ c o u n t s _ i n d e x + = 1 e n d e n d c o m m i t s . s o r t _ b y d o | c o m m i t | c o m m i t . a u t h o r e n d . c h u n k d o | c o m m i t | c o m m i t . a u t h o r e n d . e a c h d o | a u t h o r , a u t h o r _ c o m m i t s | p u t s " # { a u t h o r } h a s # { a u t h o r _ c o m m i t s . c o u n t } c o m m i t s " e n d
  19. Ruby is an OO Language, Default to OO Idioms Use

    functional style blocks Bottom-up versus Top-down Tell Don't Ask Read POODR by Sandi Metz My approaches to some Ruby problems have probably changed in subtle ways. Learning Elixir has been fun and will help me learn other languages (and their strengths) faster. I'm probably also better prepared to use Proc objects, the Proc#curry method, and Enumerator::Lazy objects but directly practicing them would be even better (perhaps with katas).