Skip to content

Instantly share code, notes, and snippets.

package com.example
// Define the State monad
case class State[S, A](run: S => (S, A)) {
// Execute the state computation and return the final state
def exec(s: S): S = run(s)._1
// Evaluate the state computation and return the result
def eval(s: S): A = run(s)._2
}
@depareddy
depareddy / json-macro-example.scala
Last active March 2, 2025 13:32
Compile-Time JSON Encoding for Scala Case Classes
package com.example.macros
import scala.deriving.Mirror
import scala.compiletime.{erasedValue, constValue, summonInline}
// Domain Model
case class Address(street: String, postalcode: String, city: String, country: String)
case class Student(name: String, age: Int, address: Address)
case class Classroom(name: String, students: List[Student])
case class School(name: String, classrooms: List[Classroom])
@depareddy
depareddy / FoldableInstances.scala
Created February 8, 2025 17:39
Implementing a Generic Foldable Type Class in Scala with List and Tuple Instances
package com.example
type TupleMy2 = [X] =>> (X, X)
trait Foldable[C[_]]:
def foldr[A, B](xs: C[A])(init: B)(fx: (A, B) => B): B
object FoldableInstances:
given listFoldable: Foldable[List] with
@depareddy
depareddy / ChainingPartialFunction.scala
Last active December 25, 2019 10:40
scala example of chaining partial functions
package sparktutorial
object ChainingPartialFunction {
type Parser = PartialFunction[Seq[String], ((String,String), Seq[String])]
val inputPath: Parser = {
case ("-i" | "--in" | "--inpath") +: path +: tail => (("input-path", path), tail)