Skip to content

Instantly share code, notes, and snippets.

View melvic-ybanez's full-sized avatar

Melvic Ybanez melvic-ybanez

View GitHub Profile
@melvic-ybanez
melvic-ybanez / what-i-didnt-know-about-fp-2021.md
Last active April 21, 2022 22:22
What I Didn't Know about Functional Programming until 2021

Previous List: What I Didn't Know about Functional Programming until 2020

What I Didn't Know about Functional Programming until 2021

  1. A hom-functor is usually implemented in Scala as the reader functor. The type itself is essentially a hom-set, but if we fix the domain to a particular type, we get a functor:
    // A hom-set Hom(A, X)
    type Reader[A, X] = A => X
    

// A hom-functor X => Hom(String, X) or Hom(String, -)

@melvic-ybanez
melvic-ybanez / what-i-didnt-know-about-fp-2020.md
Last active December 29, 2023 18:32
What I Didn't Know about Functional Programming until 2020

What I Didn't Know about Functional Programming until 2020

  1. Programming using a series of transformations and aggregations, something I've been doing for years, is known as programming in the map/reduce style.
  2. The more abstract the type is, the greater its cardinality, and the smaller the set of operations it supports. So make use of universal quantifiers, particularly by implementing fully parametric functions. They guide you on how to implement their term-level definitions by narrowing down the number of possible implementations. In other words, the type system of Scala (or Haskell, for that matter) is not only great for capturing compile-time errors, but is also capable of leading you to the correct solution.
  3. You can encode union types by combining different Scala features such as type constructors, subtyping and implicits, and by taking advantage of the Curry-Howard Isomorphism and De Morgan's Laws for neg
@melvic-ybanez
melvic-ybanez / akka_http_and_cats_validation.md
Last active January 22, 2024 18:29
Request Validation with Akka-HTTP and Cats

Request Validation with Akka-HTTP and Cats

Greg Beech wrote an article on Akka HTTP Entity Validation. This post is on the same topic, except we are going to use typeclass instances, as opposed to subtyping, for form validations.

Sample Problem: User Regsitration

To start with, let us suppose that we are working on some sort of user management service using Scala/Akka. However, here we are only going to focus on the registration part, in which the client is expected to send a user data, in JSON format; and the server needs to validate them, and save the user if the validations succeed, or reject the request with all the errors detected. That said, we should keep in mind that our validations may be used through out the entire codebase, not just in the user registration endpoint.

The rules for our validation are as follows: