Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / NatMatch.scala
Created January 10, 2023 18:40
Attempt to use scala match types to make a Nat
package dev.posco.nui
import scala.compiletime.{erasedValue, error, ops, requireConst}
object NatExample {
sealed trait Nat
object Nat {
case object Zero extends Nat
case class Succ[N <: Nat](prev: N) extends Nat
@johnynek
johnynek / ioloop.scala
Last active July 14, 2021 02:50
An example of a Free-monad which also includes ability to do general recursion.
package ioloop
object IOLoop {
enum Res[+A] {
case Done(a: A)
case FlatMap[A, B](prev: Res[B], fn: B => Res[A]) extends Res[A]
case Recurse[A, B](arg: A, loop: (A => Res[B]) => (A => Res[B])) extends Res[B]
def flatMap[B](fn: A => Res[B]): Res[B] =
FlatMap(this, fn)
@johnynek
johnynek / typed_record.scala
Created June 25, 2021 19:05
An example of zero cost addition of types on top of `Map[String, Any]` to model generic records in scala 3
package example
object RecMap {
object Record {
// use this scope to bound who can see inside the opaque type
opaque type Rec[A <: Tuple] = Map[String, Any]
object Rec {
type HasKey[A <: Tuple, K] =
@johnynek
johnynek / NatInc.scala
Created June 23, 2021 00:38
implementing typelevel Nat tracking in scala 3
package example
sealed trait Nat
case object Zero extends Nat
case class Succ[N <: Nat](n: N) extends Nat
type Inc[N <: Nat] <: Nat =
N match {
case Zero.type => Succ[Zero.type]
case Succ[n] => Succ[Inc[n]]
@johnynek
johnynek / sized_list.scala
Created May 22, 2021 19:54
An attempt to use match types for a safe apply in scala 3
package sizetypes
import scala.compiletime.ops.int
type LessThan[A <: Int] =
A match
case 0 => Nothing
case _ =>
int.-[A, 1] | LessThan[int.-[A, 1]]
@johnynek
johnynek / Nat.py
Last active August 16, 2020 02:57
first example of (terrible) python code generation from bosatsu.
def times2(___bn2):
if ___bn2 == 0:
return 0
else:
___a0 = ___bn2 - 1
___bprev2 = ___a0
return times2(___bprev2) + 2
@johnynek
johnynek / wordcount.scala
Last active July 23, 2020 12:42
and example of using fs2 (and some private CSV row typeclass code) to do wordcount.
package dev.posco.hiona.jobs
import cats.effect.{Blocker, IOApp, IO, ExitCode}
import dev.posco.hiona._
import java.nio.file.Paths
object WordCountSimple extends IOApp {
case class Record(key: String, value: Long)
def run(args: List[String]): IO[ExitCode] =
@johnynek
johnynek / Cargo.toml
Last active February 1, 2024 11:38
a toy example of CSV wordcount.
[package]
name = "hello"
version = "0.1.0"
authors = ["P. Oscar Boykin <boykin@pobox.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
csv = "1.1"
@johnynek
johnynek / checked_exceptions_in_scala.scala
Created July 13, 2020 01:09
A short sketch of how we might bring java's checked exceptions to scala using union types.
/*
* Scala 3 is getting union types, so it occurs to me we could use these to model checked exceptions
* on the JVM.
* This is a sketch of how this could work
*/
// on the JVM, any operation could through some Error (OOM for instance)
// so annotating should be some kind of Exception, not any Throwable
type Throws[E <: Exception, +A]
@johnynek
johnynek / WithTC.scala
Last active June 4, 2020 23:47
A pattern for binding a typeclass to a value, but forgetting the container type in scala.
package WTC
import cats._
import cats.implicits._
abstract class WithTC[TC[_[_]], A] {
type F[_]
val value: F[A]