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

Love your data

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Love your data

Intro to data structures and functional programming using Python.

Avatar for Mikhail Bohdan

Mikhail Bohdan

September 22, 2018

Other Decks in Programming

Transcript

  1. SMART DATA STRUCTURES AND DUMB CODE WORKS A LOT BETTER

    THAN THE OTHER WAY AROUND Eric S. Raymond
  2. WHY SHOULD I LEARN DATA STRUCTURES? ▸ Well known trade-offs

    ▸ A lot of algorithms for any purposes ▸ Language independent ▸ They are fun!
  3. COMMON DATA STRUCTURES ▸ Lists and Arrays ▸ Trees (Binary,

    Red-Black, B-Tree) and Heaps ▸ Maps or Dicts ▸ Graphs ▸ Queues, Stacks, Deques ▸ Sets
  4. DO YOU KNOW WELL SIMPLE LIST? ▸ Collection size 10kk

    elements ▸ foo 3.08 s ± 169 ms per loop ▸ bar 3.51 s ± 56.7 ms per loop ▸ baz 5.15 s ± 364 ms per loop
  5. TREES VS LISTS LIST TREE ACCESS O(n) O(log(n)) SEARCH O(n)

    O(log(n)) INSERTION O(1) O(log(n)) DELETION O(1) O(log(n))
  6. TEXT IT’S NOT HARD AT ALL ▸ Only for spins

    on insert and delete ▸ Still O(log(n)) ▸ Sorting for free ▸ Average sorting algorithm 
 O(n log(n)) ▸ Worst sorting algorithm
 O(n^2)
  7. WHAT ABOUT PROCESSING ALL THESE GRAPHS AND TREES? ▸ Complex

    is better than complicated. ▸ You shouldn’t do it every time. ▸ Most of algorithms are recursive. ▸ Complex code for iterating.
  8. NOT AGAIN. WHY DO WE NEED THIS? ▸ We already

    have first class functions ▸ Higher abstraction ▸ Code that obeys strict laws ▸ Clearer code
  9. MAP, FILTER, REDUCE OVER DATA STRUCTURE Imperative Functional List For

    cycle with some mutable state map / filter / fold Graph Complex cycle with mutable state and stack or queue map / filter / fold Tree Complex cycle with mutable state and stack or queue map / filter / fold Set For cycle with some mutable state map / filter / fold
  10. AND WHERE ARE STRICT LAWS? ▸ Monoid (mempty, mappend) ▸

    Applicative (pure, liftA2, apply) ▸ Foldable (foldl, foldr) ▸ Functor (map)
  11. APPLICATIVE class Applicative(Functor): @classmethod def pure(cls): raise NotImplementedError def apply(self,

    other): return self.liftA2( other, lambda f, x: f(x)) def liftA2(self, other, f): return other \ .map(partial(partial, f)) \ .apply(self)
  12. TREE class Tree: class Leaf: pass class Branch: pass def

    __iter__(self): stack = deque() stack.append(self) while stack: x = stack.pop() if isinstance(x.val, self.Leaf): yield x.val.val else: stack.append(x.val.right) stack.append(x.val.left)
  13. BASE LIST class List(Monoid, Foldable, Applicative): def map(self, f): return

    self.foldr( lambda r, x: \ r.cons(f(x)), self.mempty()) @classmethod def pure(cls, x): return List(x) def liftA2(self, other, f): return List(*[f(s, o) for s in iter(self) for o in iter(other)])
  14. SHOW ME REAL MAGIC! a = List(1, 2, 3) #

    => [1, 2, 3] b = a.map(partial(mul, 3)) # => [3, 6, 9] c = a.liftA2(b, add) # => [4, 7, 10, 5, 8, 11, 6, 9, 12] d = a.map(partial(partial, add)).apply(b) # => [4, 7, 10, 5, 8, 11, 6, 9, 12] c == d # True