@ChShersh@ScottWlaschin Awesome book. @ScottWlaschin has a gift for simplifying complex FP concepts into practical business terms. His blog https://t.co/D8dxugaa1r is also great.
@xypress0 @savov @prajdabre1 I don’t understand this argument. If you are deploying critical code that has the potential to cause losses of that magnitude, why were preemptive steps not taken for failover contingencies and such? This seems like a “how did we get here” question rather than a job requirement.
What is the Reader Pattern in Functional Programming and Why is it Useful?
The strategy pattern is a staple in modern programming. In functional programming, we don't have the luxury of side effects. So how exactly do we decouple services from their dependencies? To start with, we can simply put our dependencies in the list of arguments to keep our functions pure:
We've achieved full type safety and we have the ability to change our implementation of UserRepo for different contexts (e.g. refactoring, unit testing). However, the downside to this approach is all of the noise that this brings. We are now forced to explicitly pass UserRepo and OrderRepo all the way down, and if we ever need additional dependencies or add functionality in between getOrdersForUser and main, we have a lot of places to update. This is where Reader can help.
As you can see, Reader allows us to define a return computation that depends on some dependency of type R. We can simply shift our dependency to the return value instead. By returning a function that accepts a dependency, we can similarly defer the construction of our dependencies to main and gain the benefit of not having to deal with all of the updates.
Higher-Kinded Types Demystified
Have you ever heard the words "Monad" or "Functor" thrown around and wondered what they were, only to look it up and be frightened by cryptic and intimidating category theory proofs that resembled hieroglyphs? Fear not, these ideas sound daunting but you're likely using them every day without even realizing it!
Let's begin with some familiar method and function signatures. In JavaScript promises are the de-facto way to represent and compose asynchronous code. In TypeScript, the type signature of ".then(...)" would look like this:
Now let's take a look at a seemingly unrelated type in C#, IEnumerable. The type signature on ".Select(...)" looks like this:
Rewritten in a different type signature format, these functions read:
Select :: IEnumerable A -> (A -> B) -> Enumerable B
then :: Promise A -> (A -> B) -> Promise B
Notice the only thing that varies in these functions is the "containing" type. Naturally since we love abstraction as programmers, wouldn't it be nice to formalize this pattern? This is where higher-kinded types come in! Think of higher-kinded types as generics that operate over other types, giving us the ability to formalize a pattern like the one we observed above. Let's replace "IEnumerable" and "Promise" in the above signature with a generic called "F":
map :: F a -> (a -> b) -> F b
Voila! We just defined a higher-kinded type. This definition here is known as a functor and in laymen's terms describes anything that can be mapped. Promises, lists, arrays, maybes, and eithers are all examples of functors.
Now that we have functors, why should we use them? One of the most compelling reasons in my experience is that your method and function signatures become honest. Instead of throwing exceptions or relying on null checks, we can wrap our values with contexts that give us additional information about the problem being solved. In this way, you don't have to wait for runtime only to find out that a certain field can be null or that you forgot a try/catch. You can then "map" the happy path and pattern match the sad path at the appropriate point in your application, with the compiler working with you the entire time to ensure you've handled all of these cases.
Hopefully this helped!
@MaslowJax@CodeOnTheBeach Yep, one of the really cool features of TypeScript known as parameter properties! It's shorthand for creating class properties and assigning in the constructor. https://t.co/HEmP94laa3
Thank you @CodeOnTheBeach for another amazing conference and the opportunity to speak! If anyone is looking for the slides or code referenced in my talk, they can be found here: https://t.co/dzwESQTomo
https://t.co/auzyQKTOaK