Skip to content

Instantly share code, notes, and snippets.

@jammerwock
jammerwock / align.scala
Created March 7, 2025 13:01 — forked from programaker/align.scala
The Align typeclass
import scala.util.Try
import cats.Functor
import cats.Align
import cats.data.Ior
type Decimal = Int
type Roman = String
def f[F[_]: Align: Functor](fi: F[Decimal], fs: F[Roman]): F[String] =
import cats.syntax.align.* // <- HERE
@jammerwock
jammerwock / exportClause.scala
Created March 7, 2025 12:47 — forked from programaker/exportClause.scala
Scala 3 export clauses
/*
* The good practices say we shouldn't use the same type for everything (domain, json, persistence, ...), but
* most of the time those types are very similar - if not exactly identical - creating a lot of duplication.
*
* Inheritance to the rescue? Nah... not very FP and even the OOP folks are aware that we should
* prefer composition over it. But composition has it's own challenges (Law of Demeter for instance).
*
* Let's see how the new `Export Clauses` in Scala 3 can help with that:
* https://docs.scala-lang.org/scala3/reference/other-new-features/export.html
* */
@jammerwock
jammerwock / index.md
Created September 5, 2024 15:35 — forked from dacr/index.md
David's programming examples knowledge base / published by https://github.com/dacr/code-examples-manager #fecafeca-feca-feca-feca-fecafecafeca/8fece09821a16cac30da15528e3fcfa03e050e32

David's programming examples knowledge base

akka-pekko

@jammerwock
jammerwock / RecursionScheme.scala
Created May 17, 2020 10:57 — forked from zliu41/RecursionScheme.scala
Factorial computation with recursion schemes in Scala
import cats.Functor
import cats.implicits._
import scala.language.higherKinds
sealed trait StackR
final case class DoneR(result: Int = 1) extends StackR
final case class MoreR(acc: StackR, next: Int) extends StackR
sealed trait Stack[A]
sealed trait Expr
case class Const(d: Double) extends Expr
case class Var(a: String) extends Expr
case class Times(l: Expr, r: Expr) extends Expr
case class Plus(l: Expr, r: Expr) extends Expr

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA

@jammerwock
jammerwock / ExecutionContextExecutorServiceBridge.scala
Created February 12, 2020 16:37 — forked from viktorklang/ExecutionContextExecutorServiceBridge.scala
Turning an ExecutionContext to an ExecutorService (or rather and ExecutorService AND an ExecutionContext) using Scala 2.10+
/*
Copyright 2013 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
@jammerwock
jammerwock / ec.scala
Created February 12, 2020 12:04 — forked from jriecken/ec.scala
Prepare ExecutionContext without prepare method
// Something like this will likely allow propagation of thread-local information
val originalEc: ExecutionContext = ... // the actual execution context
// This will get called every time an implicit EC is necessary, effectively preparing the context
implicit def wrappedEc: ExecutionContext = {
val ctx = ...// capture thread local context here
new ExecutionContext {
override def execute(r: Runnable): Unit = originalEc.execute(new Runnable() {
override def run(): Unit {
val oldCtx = ... // get old context
@jammerwock
jammerwock / Factorial.scala
Created September 5, 2019 16:42 — forked from Algiras/Factorial.scala
The Trial of Types
package sandbox
import Nat._
trait Factorial[N <: Nat] {
type Out <: Nat
}
trait FactorialInstances0 {
type Aux[I <: Nat, O <: Nat] = Factorial[I] { type Out = O}
@jammerwock
jammerwock / zio-test.scala
Created July 4, 2019 14:47 — forked from jdegoes/zio-test.scala
Simple example of testing with ZIO environment
import zio._
type UserID = String
case class UserProfile(name:String)
// The database module:
trait Database {
val database: Database.Service
}
object Database {