Skip to content

Instantly share code, notes, and snippets.

Avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / ioloop.scala
Last active Jul 14, 2021
An example of a Free-monad which also includes ability to do general recursion.
View ioloop.scala
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 Jun 25, 2021
An example of zero cost addition of types on top of `Map[String, Any]` to model generic records in scala 3
View typed_record.scala
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 Jun 23, 2021
implementing typelevel Nat tracking in scala 3
View NatInc.scala
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
An attempt to use match types for a safe apply in scala 3
View sized_list.scala
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 Aug 16, 2020
first example of (terrible) python code generation from bosatsu.
View Nat.py
def times2(___bn2):
if ___bn2 == 0:
return 0
else:
___a0 = ___bn2 - 1
___bprev2 = ___a0
return times2(___bprev2) + 2
@johnynek
johnynek / wordcount.scala
Last active Jul 23, 2020
and example of using fs2 (and some private CSV row typeclass code) to do wordcount.
View wordcount.scala
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 Jul 23, 2020
a toy example of CSV wordcount.
View Cargo.toml
[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 Jul 13, 2020
A short sketch of how we might bring java's checked exceptions to scala using union types.
View checked_exceptions_in_scala.scala
/*
* 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 Jun 4, 2020
A pattern for binding a typeclass to a value, but forgetting the container type in scala.
View WithTC.scala
package WTC
import cats._
import cats.implicits._
abstract class WithTC[TC[_[_]], A] {
type F[_]
val value: F[A]
@johnynek
johnynek / Applicative allocation.scala
Created May 30, 2020
Here is an interesting pattern where you want to call an allocation function of a given size once for an entire graph, but you want to individually use alloc as you are building up the graph.
View Applicative allocation.scala
import cats._
import cats.implicits._
final case class Allocator[F[_], -T, A] private (slotCount: Int, builder: List[T] => F[A]) {
def map[B](fn: A => B)(implicit F: Functor[F]): Allocator[F, T, B] =
Allocator(slotCount, builder.andThen(_.map(fn)))
def pipeTo[T1 <: T, B](that: Allocator[F, T1, A => B])(implicit F: Applicative[F]): Allocator[F, T1, B] =
Allocator(slotCount + that.slotCount,
{ (slots) =>