Skip to content

Instantly share code, notes, and snippets.

@keynmol
keynmol / labeled-breaks.scala
Created September 6, 2024 15:06
Labeled nested breaks in Scala
object lib:
import scala.annotation.implicitNotFound
import scala.util.boundary, boundary.*
opaque type Lab[T <: Singleton] >: Unit = Unit
object Lab:
inline def apply[T <: Singleton]: Lab[T] = ()
inline def label[T <: Singleton & String](
inline f: Label[Lab[T]] ?=> Unit
@Daenyth
Daenyth / CachedResource-Blog.md
Last active March 26, 2024 17:19
CachedResource for cats-effect

Concurrent resource caching for cats

Motivation

cats-effect Resource is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:

trait Resource[F[_], A] {
  /** - Acquire resource
    * - Run f
 * - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
@lemastero
lemastero / ProfunctorOptics.scala
Last active January 7, 2025 17:54
Profunctor Optics in Scala
import scala.language.higherKinds
/*
Mainline Profunctor Heirarchy for Optics: https://r6research.livejournal.com/27476.html
Profunctor Optics: The Categorical Approach - Bartosz Milewski: https://www.youtube.com/watch?v=l1FCXUi6Vlw
*/
object ProfunctorOpticsSimpleImpl {
trait Functor[F[_]] {
def map[A, B](x: F[A])(f: A => B): F[B]
@ejconlon
ejconlon / hkd.scala
Last active January 13, 2021 16:17
Higher-Kinded Data Pattern in Scala
import scala.languageFeature.higherKinds
import cats.{Applicative, Id, ~>}
object Shapes {
case class Child[F[_]](nickname: F[String], age: F[Int])
case class Adult[F[_]](job: F[String], innerChild: Child[F])
class ApplicativeTrans[F[_]](implicit applicativeF: Applicative[F]) extends ~>[Id, F] {
override def apply[A](value: A): F[A] = applicativeF.pure(value)
@maurizi
maurizi / mikes-timesheet-improvements.user.js
Last active March 15, 2019 18:23
Mike's Timesheet Improvements
// ==UserScript==
// @name Mike's timesheet improvements
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Add plus time buttons to the new GetMyTime
// @author Michael Maurizi
// @match https://app.getmytime.com/timesheet.aspx
// @grant none
// ==/UserScript==
@ilijaljubicic
ilijaljubicic / gist:78402207284f96767c61401d91143a1f
Created May 24, 2017 19:10
Monad Laws Example on scala List Monad
/*
Monads laws examples with List monads
Summary from article: https://devth.com/2015/monad-laws-in-scala
*/
val f: (Int => List[Int]) = x => List(x - 1, x, x + 1)
@non
non / laws.md
Last active February 20, 2022 00:26
I feel like conversations around laws and lawfulness in Scala are often not productive, due to a lack of rigor involved. I wanted to try to be as clear and specific as possible about my views of lawful (and unlawful) behavior, and what I consider a correct and rigorous way to think about laws (and their limits) in Scala.

Laws

A law is a group of two or more expressions which are required to be the same. The expressions will usually involve one or more typed holes ("inputs") which vary.

Some examples:

x.map(id) === x
@travisbrown
travisbrown / queens.scala
Last active October 4, 2017 07:38
Aphyr's n-queens solution from Typing the technical interview, but Scala
class Nil
class Cons[X, Xs]
class First[List] { type X }
object First {
type Aux[List, X0] = First[List] { type X = X0 }
implicit val nilFirst: Aux[Nil, Nil] = ???
implicit def consFirst[X0, Xs]: Aux[Cons[X0, Xs], X0] = ???
}