Skip to content

Instantly share code, notes, and snippets.

@non
non / Tag.scala
Last active September 10, 2015 14:50
Quick little demo of type tagging in Scala that doesn't seem to have appreciable overhead for non-primtive types. For primtive types (e.g. `Int`) the overhead is equivalent to the kind of boxing you get with `.sum`.
package cats
import cats.macros.Tags
object Tag {
type @@[A, T] = Tags.Tagged[A, T]
implicit class TagOps[A](a: A) {
def tag[T]: A @@ T = macro Tags.tagMacro[A, T]
@non
non / half.scala
Last active June 10, 2022 03:11
Scala implementation of 16-bit floating point numbers. Also, a test prgoram.
package half
import scala.math.{pow, round, signum}
import scala.util.Random.nextInt
import java.lang.{Float => JFloat}
/**
* Float16 represents 16-bit floating-point values.
*
* This type does not actually support arithmetic directly. The
@non
non / operators-for-cats.md
Last active January 5, 2018 15:26
Basic write-up of how Cats supports using symbolic operators with type classes.

Symbolic operators and type classes for Cats

One of the simplest and most recognizable type classes is the semigroup. This type class abstracts over the ability to combine values of a certain type in an associative manner.

Cats provides cats.Semigroup[A] to model semigroups [1]. The combine method takes two values of the type A and returns an A value.

@non
non / AdHocTest.scala
Created July 29, 2015 15:02
Test demonstrating possible scala.js/algebra bug.
package algebra
package laws
import org.scalatest.FunSuite
class AdHocTest extends FunSuite {
test("literal ops") {
val x0 = -2147483648
val y0 = 2147483647

These are some errors I got while running test in non/algebra. It seems like maybe scala.js's Int emulation is not perfect.

[info] - [Int].order.antisymmetry *** FAILED ***
[info]   GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
[info]    (algebra-laws-test-fastopt.js:25526)
[info]     Falsified after 22 successful property evaluations.
[info]     Location: (algebra-laws-test-fastopt.js:25526)
[info]     Occurred when passed generated values (
[info]       arg0 = -2147483648,
@non
non / updater.py
Created July 28, 2015 22:57
Script to use with [awesome-scala](https://github.com/lauris/awesome-scala) to format README.md and add useful metadata.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# by Erik Osheim
#
# Reads README.md, and writes a README.md.new. If the format of
# README.md changes, this script may need modifications.
#
# Currently it rewrites each section, doing the following:
# 1. alphabetizing
@non
non / ueq.md
Last active August 29, 2015 14:25

test.scala

package test

case class UByte(val byte: Byte) extends AnyVal {
  def ==(rhs: UByte): Boolean = byte == rhs.byte
}

class Test {
public <A extends java/lang/Object> java.lang.String withImplicitly(scala.math.Ordering<A>);
Code:
0: getstatic #16 // Field scala/Predef$.MODULE$:Lscala/Predef$;
3: aload_1
4: invokevirtual #20 // Method scala/Predef$.implicitly:(Ljava/lang/Object;)Ljava/lang/Object;
7: invokevirtual #24 // Method java/lang/Object.getClass:()Ljava/lang/Class;
10: invokevirtual #30 // Method java/lang/Class.getName:()Ljava/lang/String;
13: areturn
public <A extends java/lang/Object> java.lang.String withExplicitParam(scala.math.Ordering<A>);
package cats
import cats.implicits._
// this is translation of the work at:
//
// https://ku-fpg.github.io/files/Sculthorpe-13-ConstrainedMonad.pdf
//
// into Scala using the Cats library
@non
non / scores.py
Created July 4, 2015 22:12
Simple external score parser for Angband.
#!/usr/bin/python
#
# by Erik Osheim
#
# External score parser/printer for Angband. It hardcores some
# values from p_races.txt and classes.txt for simplicitly.
# Should basically mimic the built-in format.
#
# One caveat: does not currently display the current (living)
# character, since this character's information is not in the