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

state_of_functional_programming.pdf

Leif Gensert
September 24, 2020

 state_of_functional_programming.pdf

Leif Gensert

September 24, 2020
Tweet

More Decks by Leif Gensert

Other Decks in Programming

Transcript

  1. • Code in Ruby and Elixir • Small amount of

    stdlib • Not all code is runnable
  2. What is Functional Programming? fraction1 = Fraction.new(1, 2) # 1/2

    fraction2 = Fraction.new(3, 4) # 3/4 new_fraction = fraction1.add(fraction2) new_fraction # 5/4 OR fraction1.add(fraction2) fraction1 # 5/4 fraction1 = Fraction.new(1, 2) # 1/2 fraction2 = Fraction.new(3, 4) # 3/4 new_fraction = add(fraction1, fraction2) new_fraction # 5/4 OR?
  3. fraction = Fraction.new(1, 2) fraction # 2/3 magic_function(fraction, 2, 3)

    fraction = Fraction.new(2, 3) fraction.numerator = 2 fraction.denominator = 3 fraction.set_numerator(2) fraction.set_denominator(3)
  4. Artist Album Green Day Dookie Green Day American Idiot Britney

    Spears … Baby One More Time Britney Spears In The Zone Britney Spears Circus { "Green Day" => [ "Dookie", "American Idiot" ], "Britney Spears" => [ "... Baby One More Time", "In The Zone", "Circus" ], }
  5. { "Green Day" => [ "Dookie", "American Idiot" ], "Britney

    Spears" => [ "... Baby One More Time", "In The Zone", "Circus" ], } [ ["Green Day", "Dookie"], ["Green Day", "American Idiot"], ["Britney Spears", "... Baby One More Time"], ["Britney Spears", "In The Zone"], ["Britney Spears", "Circus"], ]
  6. 1 def make_map(albums_list) 2 albums_map = {} 3 4 for

    i in (0..albums_list.length - 1) do 5 artist, album = albums_list[i] 6
 7 cur_albums = albums_map[artist] 8 if cur_albums 9 cur_albums << album 10 albums_map[artist] = cur_albums 11 else 12 albums_map[artist] = [album] 13 end 14 end 15 16 albums_map 17 end
  7. 1 def make_map(albums_list, map) do 2 if album_list == []

    do 3 map 4 else 5 [ head | rest] = albums_list 6 7 [artist, album] = head 8 9 cur_albums = Map.get(map, artist) 10 11 if cur_albums do 12 new_albums = [ album | cur_albums ] 13 new_map = Map.put(map, artist, new_albums) 14 make_map(rest, new_map) 15 else 16 new_map = Map.put(map, artist, [album]) 17 make_map(rest, new_map) 18 end 19 end 20 end
  8. 1 def make_map(albums_list, map) do 2 if album_list == []

    do 3 map 4 else 5 [ head | rest] = albums_list 6 7 [artist, album] = head 8 9 cur_albums = Map.get(map, artist) 10 11 if cur_albums do 12 new_albums = [ album | cur_albums ] 13 new_map = Map.put(map, artist, new_albums) 14 make_map(rest, new_map) 15 else 16 new_map = Map.put(map, artist, [album]) 17 make_map(rest, new_map) 18 end 19 end 20 end
  9. class Database @store = {} def self.get(key) @store[key] end def

    self.insert(key, value) @store[key] = value end def self.drop @store = {} end end Database.insert("Green Day", ["Dookie"]) Database.get("Green Day") # ["Dookie"]
  10. else if message == {"insert", key, value} do Map.put(store, key,

    value) send(from, "ok") end *Pseudocode def run(store) do receive from, message do end end reference = spawn run(%{"Green Day" => ["Dookie"]}) send(reference, {"get", "Green Day"}) # ["Dookie"] if message == {"get", key} do send(from, Map.get(store, key))
  11. def run(store) do receive from, message do if message ==

    {"get", key} do send(from, Map.get(store, key)) else if message == {"insert", key, value} do new_store = Map.put(store, key, value) send(from, "ok") end end end reference = spawn run(%{}) send(reference, {"insert", "Green Day", ["Dookie"]}) send(reference, {"get", "Green Day", ["Dookie"]}) # ["Dookie"] run(new_store) run(store)
  12. {:ok, reference} = Agent.start_link(fn -> %{} end) Agent.update( reference, fn

    store -> Map.put(store, "Green Day", ["Dookie"]) end ) Agent.get( reference, fn store -> Map.get(store, "Green Day") end )
  13. Agent.start_link(fn -> %{} end, name: :albums) Agent.update( :albums, fn store

    -> Map.put(store, "Green Day", ["Dookie"]) end ) Agent.get( :albums, fn store -> Map.get(store, "Green Day") end )