Skip to content

Instantly share code, notes, and snippets.

@non
non / Wild.scala
Last active April 9, 2020 00:26
Interesting trick to lift existentials into values, so we can later refer to them.
package demo
import cats.{Applicative, Functor, Id, Semigroupal, Traverse}
import cats.arrow.FunctionK
/**
* Type-lifting operation to replace the wildcard type (i.e. _).
*
* In some cases we end up with code like: List[Option[_]]. This is
* fine unless you later need to write code in terms of a particular
package cats.collections
sealed abstract class LogicalBitSet { lhs =>
import LogicalBitSet.{Absent, Present}
def apply(n: Int): Boolean =
this match {
case Present(bits) => bits(n)
case Absent(bits) => !bits(n)
package ack
import cats.Eval
import spire.math.SafeLong
object Ackermann {
import SafeLong.{zero, one}
// requires m >= 0 and n >= 0
def run(m: SafeLong, n: SafeLong): Eval[SafeLong] =
@non
non / seeds.md
Last active April 12, 2024 09:18
Simple example of using seeds with ScalaCheck for deterministic property-based testing.

introduction

ScalaCheck 1.14.0 was just released with support for deterministic testing using seeds. Some folks have asked for examples, so I wanted to produce a Gist to help people use this feature.

simple example

These examples will assume the following imports:

@non
non / seethru.scala
Last active February 13, 2018 20:17
nested opaque types demo
package object demo {
opaque type Collection[A] = Array[A]
object Collection {
opaque type Pointer[A] = Int
object Pointer {
def first(c: Collection[A]): Option[Pointer[A]] =
if (c.length > 0) Some(0) else None
@non
non / Diagonal.scala
Last active February 13, 2018 19:29
Initial thoughts on creating efficient ways to index into arbitrary enumerations of all values of a type.
package kronecker
import spire.implicits._
/**
* Utilities for doing diagonalization in N dimensions.
*
* The goal here is to be able to support diagonalizations for
* arbitrary tuples, e.g. Tuple2, Tuple3, Tuple9, etc. The "dimension"
* (or "dim") represents the arity of the tuple: dim=2 would
@non
non / JavapPlugin.scala
Created January 29, 2018 02:19
Slouching toward a functioning SBT plugin.
package sbtjavap
import sbt._
import Keys._
import complete.DefaultParsers.StringBasic
import scala.sys.process.Process
object Plugin extends AutoPlugin {
object autoImport {
@non
non / analysis.md
Last active January 2, 2018 18:31
Some notes about the interaction of SIP-23 with specialization.

introduction

I was asked to consider how specialization should interact with SIP-23. Here is a quick test I did with some thoughts on where we are now versus where we should be.

See tlc.scala for the code and tlc.bytecode for the Java bytecode that code produced (using Typelevel Scala with the -Yliteral-types flag enabled).

@non
non / sipzz.md
Last active August 6, 2017 04:24

SIP-ZZ - NewType Classes

Introduction

This is a proposal to introduce syntax for classes in Scala that can get completely inlined, so operations on these classes have zero overhead compared to external methods. Some use cases for inlined classes are:

  • Inlined implicit wrappers. Methods on those wrappers would be
@non
non / builtins.txt
Last active May 12, 2017 19:33
List of classes/traits in Scala's standard library that are probably worth supporting via type classes. The implication being that other types (e.g. GenSeq) are not worth supporting directly. Suggested additions welcome!
scala
Array
BigDecimal
BigInt
Boolean
Byte
Char
Double
Float
FunctionN...