Skip to content

Instantly share code, notes, and snippets.

View gregghz's full-sized avatar

Gregg Hernandez gregghz

  • Portland, OR
View GitHub Profile
@gregghz
gregghz / poly.scala
Last active May 26, 2022 17:30
Polymorphic Method Macro
object PolymorphicMethod {
inline def make[T]: Unit = ${ impl[T] }
def impl[T: Type](using Quotes): Expr[Unit] = {
import quotes.reflect.*
def decls(owner: Symbol): List[Symbol] = {
val targetSymbol = TypeRepr.of[T].typeSymbol
val abstractDef: DefDef = targetSymbol.declaredMethod("poly").head.tree.asInstanceOf[DefDef]
@gregghz
gregghz / monads.md
Last active February 22, 2018 18:00

Function composition typically looks like this:

let x = 1;
f(g(h(x)));

This works as long as the result of h(1) is an acceptable parameter to g and if the results of g(h(1)) is an acceptable parameter to f.

To make this more concrete let's define these functions:

function f(num) { return num * 2; }

@gregghz
gregghz / build.sbt
Created July 4, 2016 18:09
build.sbt for Play 2.5 + Angulate2
import Angulate2Plugin._
lazy val commonSettings = Seq(
organization := "com.gregghz",
version := "1.0.0",
scalaVersion := "2.11.8",
scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-Xfatal-warnings"
package main
object Main {
trait Thing[A] {
def f(a: Int): A
}
def g[A: Thing](i: Int): A = implicitly[Thing[A]].f(i)
def g[A](i: Int, f: Int => A): A = f(i)
;; .emacs: (add-hook 'ensime-mode-hook 'ensime-aux-mode)
(defun ensime-aux-sbt-do-reload ()
(interactive)
(sbt-command "reload"))
(defun ensime-aux-sbt-do-scoverage ()
(interactive)
(sbt-command "scoverage:test"))
@gregghz
gregghz / gist:7677880
Created November 27, 2013 15:46
The code here: http://graphics-geek.blogspot.com/2013/09/lazy-lists.html compared in Java and Scala.
// Java:
public class LazyListManager {
public static <T> List<T> add(List<T> list, T item) {
if (list == null) {
list = new ArrayList<T>();
list.add(item);
} else if (!list.contains(item)) {
list.add(item);
}
package main
import (
"fmt"
"log"
"net/http"
"reflect"
"strings"
)
@gregghz
gregghz / events.go
Created May 7, 2013 21:35
An example of a primitive way to implement an event system in Go using channels and goroutines.
package main
import (
"fmt"
)
type SomeEvent struct {
callbacks []func(int)
cc chan int
}
@gregghz
gregghz / gist:4693573
Last active December 12, 2015 01:48
Convert rgb or hex colors to something close. See the comments at the top for instructions. You'll get a new file with .test appended to the filename. This should be considered very alpha. While it *probably* won't harm anything in place, the output might not be what's expected.
#!/usr/bin/env python
# to run it:
# python <input file> 123 123 123
#
# This will take the input file, and convert any colors close to rgb(123, 123, 123) to rgb(123, 123, 123). Close means that all three values are within 2 or your target colors. You can adjust this by tweaking the rrange, grange and brange values (line 24).
#
# You can also pass a hex color (#efefef) by just splitting the three colors on the command line:
# python <input file> ef ef ef
#