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

PMaps

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 PMaps

Principled and Powerful Maps

Presented at NE Scala 2018

Avatar for Jason Liberman

Jason Liberman

March 19, 2018
Tweet

Other Decks in Programming

Transcript

  1. INTRO • My name is Jason Liberman • Studied Pure

    Math and Philosophy at UPenn • Previously worked at an investment bank • Self-taught, Scala was the first programming language I learned in formal setting • Life outside of Scala: • Avid reader (lots of philosophy) • Book store dreams
  2. BRIDGEWATER • Investment Engineer at Bridgewater Associates • Largest hedge

    fund in the world • Manage $160bn for our clients • Author / implement investment logic in code (primarily Scala) • Team of ~50 engineers, we are incredibly passionate about Scala training and generally building a great community • Come talk to me if interested in learning more!
  3. MAPS - CONTEXT • Maps / Key-Value pair collections are

    used everywhere • Scala’s immutable Map collection is fast and incredibly useful • Scala Map shortcomings? • Birth of PMaps
  4. AGENDA • Define some important qualities I think key-value pair

    collections should have • Some ways in which Maps seem to be falling short • Introduce PMaps and how they compensate • Questions
  5. TAKEAWAYS • Often easy to build something small that makes

    code authoring a lot simpler! • Define algebra • Enforce algebra • Add syntax
  6. WHAT IS THE KEY TO KEY-VALUE COLLECTIONS? • Performant •

    Principled: • Operations are well-defined, transparent, and reflected in the types • Powerful: • Clean, concise, expressive logic • Lift algebra / logic to collection level
  7. MAPS THAT GET YOU LOST • Defaults on Scala Maps:

    • Not principled: • I want a strongly typed identification that I have something fundamentally different • Function vs. Partial Function
  8. MAPS THAT GET YOU LOST • Dropped Keys: • Not

    principled: • Silently dropping colliding keys • Not powerful: • Would ideally want to use the fact that quantity sold can be summed for a given author!
  9. VERBOSE KEY ITERATIONS • Not powerful • I know that

    my quantities can be added, all I really want to do is this: • In other words LIFT the algebra on QuantitySold to the collection level
  10. JOINS • Maps are just functions (as long as they

    have a default) • So can’t I just do this?
  11. JOINS • Can apply logic to joined values at collection

    level: • Joins only defined when it makes sense, e.g. no leftJoins on PMapWDefaults
  12. LIFTING ALGEBRA TO COLLECTION • Lifting algebra of V to

    PMap level • If V is a Monoid, so is PMap[K, V] • If V is a Vector Space, so is PMap[K, V] • Prove this, and you get syntax for free (e.g. from Spire)
  13. HOW THIS WORKS • Type Classes • Prove if V

    is Monoid, then PMapWDefault[K, V] is Monoid • Numeric type classes and algebraic syntax and from Spire • Dependent types to avoid nested tuples and return reasonable types from Joins
  14. TAKEAWAYS • Often easy to build something small that makes

    code authoring a lot simpler! • Define algebra • Enforce algebra • Add syntax
  15. IN CONCLUSION • You can find the code on github:

    • https://github.com/jasonliberman8/PMaps • Suggestions? Thoughts? Please find me after.