PS Unscripted - Code Reuse in PS: Fns, Classes, and Interpreters
Channel: PureScript Community
Date: 2018-06-09

PureScript Unscripted, June 9, 2018

Thanks to @natefaubion for organizing and presenting the topic! It is very interesting!

Recommended to watch at 1.5x speed.

## Code Reuse in PureScript: Functions, Type classes, and Interpreters

0:25 - Intro
1:53 - Example function: `sayHello`
3:08 - Extract "Hello, "
4:40 - Extract prepend operation
5:30 - Extract String type
7:42 - Support type-changing in transformer
8:40 - Extract List type using `Transform` synonym
10:20 - Move `a` & `b` polymorphism to `Transform` synonym
11:20 - Parameterize a function over a `Transform`
12:06 - `sayHello`, before & after
13:10 - Reducing redundancy in function using `Transform`

13:55 - From type synonym to type class
17:19 - Reducing redundancy in related type classes
22:55 - A world without (compiler-assisted) type classes
23:40 - Let's try implementing related (entailed) type classes with type synonyms...
25:35 - `Traversable` - when the value of compiler-assisted type classes becomes clear
29:12 - Experience report: simulating type classes in Elm
31:00 - Compiler-assisted type classes enable easily moving code around. `Traversable` and `Compose` classes are real-world examples of value

32:57 - "Real Code" example: caching an effectful data request function
34:08 - Extract data location, extract cache location, extract effectful functions
36:20 - Bundle related effects into a single parameter, as a record
37:40 - Make polymorphic the requested data type
38:40 - Q: Iterating on a function to make it more reusable, is this what happens in real life, real apps?
41:04 - Experience report: Pure FP languages can enable writing more reusable functions
42:28 - Extract `MonadCache` and `MonadRequest` type classes
43:48 - Implementation after refactoring to make it more reusable
44:48 - Examples of reusable classes in "transformers" library

45:30 - Which code is more reusable? `Maybe`? Or callbacks for failure and success?
47:39 - Q: Interesting that `forall r.` can be considered equivalently reusable to `Maybe`
49:09 - Final encoding vs Initial encoding
50:30 - Opaque representation example: "Real Code", which is effectful
50:40 - Make effectful functions more reusable using a data type
51:33 - Can't encode "Real Code" using a simple data type, but *can* if adding "callback" parameter to data type's tags
52:10 - Note: Error in code sample, each should return `RealCode a`, not just `a`
53:51 - Example writing "Real Code" using effects represented as a data type
55:00 - Running the effects by interpreting the data type
57:17 - Can write non-effectful interpreter, useful for using "Real Code" in tests
57:40 - Can run "Real Code" in `Aff` or in `Identity`, similar to type classes

58:22 - A positive aspect of interpreter is writing in concrete data types, which feel easier to use and are more intuitive. Don't need to extract to type parameters and type classes

59:56 - Not so nice aspect of interpreter is the compiler doesn't help wire things together, like it does with type class hierarchies
1:00:08 - Not so aspect of interpreter is it doesn't have nice compositional properties
1:00:50 - While the code using it *is* reusable, the concrete data type itself *isn't* extensible. Also, can have diamond problem when composing/nesting them
1:01:35 - Not so aspect of interpreter is the syntax is not so good. Can use `do-notation` by defining `Monad` instance for all data types
1:02:20 - Libraries can help with syntax: `Free`, `Variant`, and `Run`. Can recover aspects of type classes

1:03:35 - @natefaubion's earlier talk, "PS Unscripted - Free From Tree & Halogen VDOM":
https://www.youtube.com/watch?v=eKkxmVFcd74

## Audience questions

1:04:55 - Q: How to compose these concrete data types using `Run` library?
1:07:47 - Q: Composing concrete data types with tags having same name?
1:09:15 - Q: Wrapper/tag? Read `Variant`'s readme first, then read `Run`'s readme.
1:10:40 - Q: Intuition for the `a` in the `RealCode` type?
1:12:55 - Pointing out typo in `RealCode` type.
1:13:53 - Q: Squiggly arrow still keeps the `a`? Yes, it's a synonym for `NaturalTransformation`
1:16:51 - Q: Initial encoding vs Final encoding? Yes, final encoding was covered in first half of the talk.
1:18:57 - Q: Initial encoding vs Final encoding differences? Yeah, might have runtime/performance differences, but they are nearly equivalent in expressivity.
1:22:00 - Q: Have you ever struggled to adapt software to new requirements which disturbs the uniformity of the abstraction that you use?
1:26:55 - Q: Do you ascribe laws to tags, like we do with methods in a type class?
1:33:00 - Thanks Nate!

## Post-talk chatter

1:33:26 - PureScript 0.12 thoughts - `Effect` type and effect row
1:47:05 - PureScript Unscripted and its schedule
1:49:20 - `VariantF` and extensible sum types is the best thing in a programming language

Thanks all for attending!