Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / Applicative allocation.scala
Created May 30, 2020 20:38
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.
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) =>
@johnynek
johnynek / sortmerge.scala
Last active October 14, 2022 10:32
merge sorted streams with fs2
import fs2.{Chunk, Stream, Pull}
import cats.collections.Heap
import cats.implicits._
object SortMerge {
def sortMerge[F[_], A: Ordering](streams: List[Stream[F, A]]): Stream[F, A] = {
implicit val ord: cats.Order[Stream.StepLeg[F, A]] =
new cats.Order[Stream.StepLeg[F, A]] {
val ordA = implicitly[Ordering[A]]
@johnynek
johnynek / brokenList.scala
Last active April 5, 2020 00:04
a dotty implementation of safe get using match types. This was done on 0.23.0-RC1. The first implementation, when the list is itself an opaque type, does not compile, the second, where List is the standard list, does compile.
// unfortunately
// opaque type Fix[F[_]] = F[Fix[F]]
// won't work (no recursion in opaque type), but this implementation is safe, but scary due to asInstanceOf
object FixImpl {
type Fix[F[_]]
inline def fix[F[_]](f: F[Fix[F]]): Fix[F] = f.asInstanceOf[Fix[F]]
inline def unfix[F[_]](f: Fix[F]): F[Fix[F]] = f.asInstanceOf[F[Fix[F]]]
}
@johnynek
johnynek / TimeWindow.scala
Last active April 2, 2020 01:49
A datatype to associatively and commutatively keep a time window of events.
package dev.posco.hiona
import cats.collections.PairingHeap
import cats.kernel.CommutativeSemigroup
import cats.{Eq, Order}
import cats.implicits._
/**
* This keeps a window of events at most Duration apart
@johnynek
johnynek / row.scala
Created March 31, 2020 23:07
a typeclass for CSV/TSV data supporting Optional or coproduct (Either) data.
package dev.posco.hiona
import cats.effect.{IO, Resource}
import java.io.{BufferedWriter, FileWriter, PrintWriter}
import java.nio.file.Path
import net.tixxit.delimited.{ DelimitedFormat, Row => DRow}
import shapeless._
/**
@johnynek
johnynek / decay.scala
Last active March 31, 2020 20:49
using literal types in scala 2.13 to track the half-life of an exponential decay
case class Timestamp(epochMillis: Long)
case class Decay[H <: Double](scaledTime: Double, value: Double) {
def timestampDouble(implicit v: ValueOf[H]): Double =
scaledTime * v.value
def timestamp(implicit v: ValueOf[H]): Timestamp =
Timestamp(timestampDouble.toLong)
def combine(that: Decay[H]): Decay[H] =
@johnynek
johnynek / labels.scala
Last active March 27, 2020 20:38
Design of labels for semblance like system.
sealed abstract class Label[K, V] {
final def lookForward(duration: Duration): Label[K, V] =
Label.LookForward(this, duration)
final def zip[W](that: Label[K, W]): Label[K, (V, W)] =
Label.Zipped(this, that)
final def map[W](fn: V => W): Label[K, W] =
mapWithKey(Feature.ValueMap(fn))
final def mapWithKey[W](fn: (K, V) => W): Label[K, W] =
@johnynek
johnynek / testrunner.scala
Last active January 30, 2020 03:54
A minimal example of a test runner for https://github.com/sbt/test-interface. Any comments or reviews welcome.
/**
* copyright 2020 P. Oscar Boykin <oscar.boykin@gmail.com>
* licensed under Apache V2 license: https://www.apache.org/licenses/LICENSE-2.0.html
* or, at your option, GPLv2 or later: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
package testrunner
import io.github.classgraph._
import java.io.PrintWriter
@johnynek
johnynek / staging_parser.scala
Created December 28, 2019 02:05
attempt at using staging to make a parser in scala 3
/**
* Error:
*
[info] Compiling 1 Scala source to /home/oscar/oss/olelo/target/scala-0.21/classes ...
[error] -- Error: /home/oscar/oss/olelo/src/main/scala/Olelo.scala:13:14 ---------------
[error] 13 | p.toExpr
[error] | ^
[error] | access to type A from wrong staging level:
[error] | - the definition is at level 0,
[error] | - but the access is at level 1.
@johnynek
johnynek / pattern.bosatsu
Created December 3, 2019 02:03
Example of complex string pattern matching with bosatsu
1 package PatternExamples
2
3 foo = "this is foo"
4 bar = "this is bar"
5
6 combine = "foo: ${foo} bar: ${bar}"
7
8 def operator ==(a, b):
9 match string_Order_fn(a, b):
10 EQ: True