Skip to content

Instantly share code, notes, and snippets.

View johnhungerford's full-sized avatar

johnhungerford

  • Two Six Labs
  • Mt. Laurel Township, NJ
View GitHub Profile
@johnhungerford
johnhungerford / pylayer.py
Last active October 11, 2024 16:37
Dependency injection for Python
"""
This file contains a POC dependency-injection framework based on Scala's ZIO library. The idea is that
each dependency can have one or more "layer," which specifies how to construct that type of dependency
from other dependencies. These layers can be added to an "environment," which can construct a value
for any type that you query, so long as layers have been added for it and and its upstream dependencies.
Toward the end of the file there's a simple example demonstrating how this framework can be used for
configuring an application.
"""
@johnhungerford
johnhungerford / dependency-injection.md
Last active October 17, 2024 08:16
ZIO-like dependency injection using implicit resolution

ZIO-like dependency injection using implicit resolution

Daniel Ciocîrlan recently published a video showcasing a dependency-injection (DI) approach developed by Martin Odersky that uses Scala's implicit resolution to wire dependencies automatically. (See also his reddit post.)

The basic pattern for defining services in Odersky's approach is as follows:

class Service(using Provider[(Dep1, Dep2, Dep3)])
@johnhungerford
johnhungerford / getEither.scala
Last active October 15, 2024 07:21
Kotlin/TypeScript -like syntax for accessing nested optional types
//> using scala 3.3
import scala.util.{Failure, NotGiven, Success, Try, boundary}
import boundary.{Label, break}
import scala.annotation.targetName
/**
* Proof of concept implementation of a syntax similar to Kotlin and
* typescript. Within the context provided by [[getEither]], you can call
* `?` on any optional/failable type (currently supports [[Option]],
@johnhungerford
johnhungerford / ZeroOverHeadOption.scala
Last active November 10, 2023 18:35
Zero overhead Option in Scala 3
//> using scala 3.3.0
import scala.util.{Try, Success, Failure}
import scala.reflect.Typeable
import scala.util.NotGiven
import scala.annotation.implicitNotFound
type Opt[T] = Opt.Opt[T]
object Opt:
@johnhungerford
johnhungerford / TicTacToe.Scala
Last active March 26, 2023 11:13
Type level tic tac toe using match types
sealed trait Bool
object Bool:
sealed trait True extends Bool
sealed trait False extends Bool
sealed trait Nat
object Nat:
sealed trait _0 extends Nat
sealed trait Succ[N <: Nat] extends Nat