Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save programaker/35e45ab807184d6edc25957d25cb8138 to your computer and use it in GitHub Desktop.
Save programaker/35e45ab807184d6edc25957d25cb8138 to your computer and use it in GitHub Desktop.
Classes as modules of partially-applied functions
//Functional programming is all about:
//. Pure, total and deterministic functions
//. ADTs (Algebraic Data Types)
//. Typeclasses
//However, in hybrid languages like Scala, OOP classes can still play a role!
//---------------------------------------------------------
//Given:
//A simple domain class
case class Frunfles(id: Long, value: String)
//A package object acting as a module containing stand-alone functions
//for CRUD operations on Frunfles
package app.frunfles
package object crud {
def findAllFrunfles[F[_]: Monad](db: DatabaseConnection): F[List[Frunfles]] = ???
def findFrunflesById[F[_]: Monad](db: DatabaseConnection, id: Long): F[Option[Frunfles]] = ???
def findFrunflesByValue[F[_]: Monad](db: DatabaseConnection, value: String): F[Option[Frunfles]] = ???
def insertFrunfles[F[_]: Monad](db: DatabaseConnection, f: Frunfles): F[Unit] = ???
def updateFrunfles[F[_]: Monad](db: DatabaseConnection, f: Frunfles): F[Unit] = ???
def deleteFrunfles[F[_]: Monad](db: DatabaseConnection, id: Long): F[Unit] = ???
}
//These functions are quite repetitive; all of then require `[F[_]: Monad]` and `db: DatabaseConnection`,
//and this is a tame example! Techniques like Tagless Final or Free Monads can make function signatures
//have many context bounds (like `: Monad` in the example) or implicit parameters.
//---------------------------------------------------------
//Classes to the rescue!
package app.frunfles.crud
final class FrunflesRepository[F[_]: Monad](db: DatabaseConnection) {
def findAllFrunfles(): F[List[Frunfles]] = ???
def findFrunflesById(id: Long): F[Option[Frunfles]] = ???
def findFrunflesByValue(value: String): F[Option[Frunfles]] = ???
def insertFrunfles(f: Frunfles): F[Unit] = ???
def updateFrunfles(f: Frunfles): F[Unit] = ???
def deleteFrunfles(id: Long): F[Unit] = ???
}
//The class can group all the common parts in its declaration and constructor!
//Its like in Math when we factor by taking a common term => ab + ac = a(b + c)
//Making the `DatabaseConnection` available to all functions and removing the parameter
//is similar to apply only the `db` parameter to produce new functions.
//That's why the class is a "module of partially-applied functions"!
//Conclusion: you can start you code with stand-alone functions and
//"factor" them with a class when necessary.
@programaker
Copy link
Author

programaker commented Nov 19, 2020

A purely functional approach to this problem (without a class) is the Reader monad: https://gist.github.com/noelwelsh/7255b92a6f1faa1d77d277a1ac5b1595

However, you still need to re-declare the repetitive arguments (even if you group them in a single Environment value)
From the gist above, notice how we still need to write (db: DbConnection) => multiple times:

def getUser(id: Id): DbConnection => User =
  (db: DbConnection) => ???

def saveUser(user: User): DbConnection => Unit =
  (db: DbConnection) => ???

@programaker
Copy link
Author

programaker commented Nov 19, 2020

Another non-OOP solution: a function that returns a record containing functions that have access to the dependency as closures

package app.frunfles

package object crud {

  final case class FrunflesRepository[F: Monad](
    findAllFrunfles: () => F[List[Frunfles]],
    findFrunflesById: Long => F[Option[Frunfles]],
    findFrunflesByValue: String => F[Option[Frunfles]],
    insertFrunfles: Frunfles => F[Unit],
    updateFrunfles: Frunfles => F[Unit],
    deleteFrunfles: Long => F[Unit]
  )

  def makeFrunflesRepository[F: Monad](db: DatabaseConnection): FrunflesRepository[F] = FrunflesRepository(
    findAllFrunfles = () => ???,
    findFrunflesById = (id: Long) => ???,
    findFrunflesByValue = (value: String) => ???,
    insertFrunfles = (f: Frunfles) => ???,
    updateFrunfles = (f: Frunfles) => ???,
    deleteFrunfles = (id: Long) => ???
  )

}

It's basically what the class does, but using only records, closures and functions - constructions present in any hardcore FP language.
But in Scala, it looks more like a workaround to avoid true classes at all costs just for the sake of being "pure FP".

[UPDATE]

ADTs that contain functions come with some caveats as they don’t translate perfectly onto the JVM. For example, legacy Serializable, hashCode, equals and toString do not behave as one might reasonably expect.

In this case, we just want to use the case class to pass the environment, not as real data, so it might be fine.

@programaker
Copy link
Author

@programaker
Copy link
Author

Another discussion on the topic: https://twitter.com/alexelcu/status/1359034466572861441
A more succinct definition: Classes are closures with names

@programaker
Copy link
Author

Breaking news! Scala 3 context functions offer an alternative approach to classes and Reader: https://gist.github.com/programaker/51c31e8c1ca3d4715b872d610c1a636c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment