Skip to content

Instantly share code, notes, and snippets.

P. Oscar Boykin johnynek

Block or report user

Report or block johnynek

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@johnynek
johnynek / dotty_list.scala
Last active Sep 11, 2019
Implementation of linked list using dotty features (opaque type, union types, extension methods, type lambda).
View dotty_list.scala
// 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 / map_module.scala
Last active Mar 13, 2019
A module based encoding of Map which keeps the ordering associated with the Map. Then we can talk about `mymod.Map` as a type, knowing that `mymod.ordering` is the ordering.
View map_module.scala
abstract class MapModule {
type K
def ordering: Ordering[K]
type Map[_]
def empty[V]: Map[V]
def updated[V](m: Map[V], key: K, v: V): Map[V]
def get[V](m: Map[V], key: K): Option[V]
def items[V](m: Map[V]): Stream[(K, V)]
def remove[V](m: Map[V], key: K): Map[V]
View Array covariance unsoundness.java
class B { }
class Main {
public static void main(String[] args) {
B strMain = Main.<String>cast("this is not a b");
System.out.println(strMain.toString());
}
public static <A extends Object> B cast(A a) {
B[] ary = new B[1];
@johnynek
johnynek / y.py
Last active Mar 3, 2019
Here is a port of this example y-combinator code: https://rosettacode.org/wiki/Y_combinator#Python except manually rewriting the lambdas into top-level functions accessing dictionaries to create closures.
View y.py
def make_lambda(captured, fn, arity):
return {
"cap": captured,
"fn": fn,
"arity": arity,
"applied": [],
}
def apply_lambda(lam, arg):
app0 = lam["applied"]
View buildcart.scala
package org.bykn.buildcart
import cats.{Alternative, Applicative, Id, Eq, Monad, Order, Traverse}
import cats.data.{Chain, Const, State}
import scala.collection.immutable.SortedSet
import cats.implicits._
object BuildCart {
trait Hash[A]
@johnynek
johnynek / TreeList.scala
Created Dec 14, 2018
Implementation of "Purely Functional Random Access Lists" by Chris Okasaki in scala. This gives O(1) cons and uncons, and 2 log_2 N lookup.
View TreeList.scala
package org.bykn.list
import cats.Applicative
import cats.implicits._
/**
* Implementation of "Purely Functional Random Access Lists" by Chris Okasaki.
* This gives O(1) cons and uncons, and 2 log_2 N lookup.
*/
@johnynek
johnynek / defer_stack_overflow
Created Oct 30, 2018
Hit a stack overflow in Eval.defer in cats 1.4.0
View defer_stack_overflow
[info] at cats.Eval$.loop$1(Eval.scala:367)
[info] at cats.Eval$.cats$Eval$$evaluate(Eval.scala:372)
[info] at cats.Eval$Defer.value(Eval.scala:258)
[info] at cats.Eval$.loop$1(Eval.scala:351)
[info] at cats.Eval$.cats$Eval$$evaluate(Eval.scala:372)
[info] at cats.Eval$Defer.value(Eval.scala:258)
[info] at cats.Eval$.loop$1(Eval.scala:351)
[info] at cats.Eval$.cats$Eval$$evaluate(Eval.scala:372)
[info] at cats.Eval$Defer.value(Eval.scala:258)
[info] at cats.Eval$.loop$1(Eval.scala:351)
View lint_output.txt
--- /Users/oscar/oss/rules_scala/scala/private/rule_impls.bzl (original)
+++ /Users/oscar/oss/rules_scala/scala/private/rule_impls.bzl (reformatted)
@@ -99,9 +99,7 @@
outs = [ctx.outputs.jar, ctx.outputs.statsfile]
- inputs = ctx.files.resources + [
- ctx.outputs.manifest, zipper_arg_path
- ]
+ inputs = ctx.files.resources + [ctx.outputs.manifest, zipper_arg_path]
@johnynek
johnynek / immutable vs mutable parser.txt
Last active Aug 6, 2018
comparing an immutable based parser combinator to a mutable based on that threads a mutable state through during a parse. Probably many optimizations to make on both, which are both very simple.
View immutable vs mutable parser.txt
immutable: timeParsing
mutable: timeParsing2
fastparse: timeParsing3
[info] Benchmark (depth) (length) Mode Cnt Score Error Units
[info] JsonBenchmark.timeParsing 0 5 thrpt 4 1647.064 ± 144.773 ops/s
[info] JsonBenchmark.timeParsing 0 25 thrpt 4 1662.836 ± 23.331 ops/s
[info] JsonBenchmark.timeParsing 0 125 thrpt 4 1675.115 ± 45.713 ops/s
[info] JsonBenchmark.timeParsing 1 5 thrpt 4 273.196 ± 12.285 ops/s
[info] JsonBenchmark.timeParsing 1 25 thrpt 4 37.293 ± 3.452 ops/s
View genfn.scala
package gradfn
import shapeless.{HNil, :: => HCons, HList, Nat}
import shapeless.ops.nat.ToInt
import Nat.{_0, _1, _2}
import Vect.VectOps
trait NatTrans[F[_], G[_]] {
def apply[A](fn: F[A]): G[A]
You can’t perform that action at this time.