Skip to content

Instantly share code, notes, and snippets.

Nigel Benns nbenns

Block or report user

Report or block nbenns

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
@nbenns
nbenns / queries.sql
Last active Apr 16, 2019
DB Queries
View queries.sql
-- MySQL count same queries
SELECT substring_index(info, '/', 1), count(*) AS cnt
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE INFO IS NOT NULL
GROUP BY 1
HAVING cnt>1
ORDER BY cnt DESC;
-- Show locks
@nbenns
nbenns / errcompose.scala
Created Jan 2, 2019
Composing Errors with Coproduct
View errcompose.scala
import shapeless.{ :+:, CNil, Coproduct }
import shapeless._
val error1 = Coproduct[IllegalArgumentException :+: CNil](new IllegalArgumentException)
val error2 = Coproduct[NumberFormatException :+: CNil](new NumberFormatException)
type MyError = IllegalArgumentException :+: NumberFormatException :+: CNil
object MyErrorHandler extends Poly1 {
implicit def caseConfigNotFoundException: Case.Aux[IllegalArgumentException, String] =
@nbenns
nbenns / fixgadt.scala
Created Dec 20, 2018
Fix a GADT Based List
View fixgadt.scala
import scala.language.higherKinds
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
object Functor {
def apply[F[_]](implicit fun: Functor[F]): Functor[F] = fun
implicit class FunctorOps[F[_]: Functor, A](fa: F[A]) {
@nbenns
nbenns / Option.java
Created Oct 20, 2018
Transition to Option
View Option.java
import java.util.function.Function;
public abstract class Option<T> {
private static class Some<T> extends Option<T> {
private T val;
Some(final T val) {
this.val = val;
}
@nbenns
nbenns / timestuff.scala
Last active Oct 15, 2018
Using ZonedDateTime properly
View timestuff.scala
def fromISOString(d: String): ZonedDateTime =
ZonedDateTime.parse(d, DateTimeFormatter.ISO_OFFSET_DATE_TIME).withZoneSameInstant(ZoneOffset.UTC)
def toISOString(d: ZonedDateTime) = d.withZoneSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
@nbenns
nbenns / SizedMatrix.scala
Last active Jun 10, 2018
Play with adding size of col and row as type parameters to a matrix
View SizedMatrix.scala
import shapeless.ops.hlist.Length
import shapeless.{::, HList, HNil, LUBConstraint, Nat, Poly1, Succ}
import shapeless.Nat._
abstract class Matrix[Col <: Nat, Row <: Nat, A] {
type RowLst <: HList
type ConLst <: HList
val data: ConLst
}
View shapes.xml
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg width='1000' height='1000'
xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink'
onload='init(evt)' >
<style>
View flame.html
<html>
<body>
<canvas id="canvas1" width="500", height="500">
Random Canvas
</canvas>
<script type="text/javascript">
function setPixel(imageData, x, y, r, g, b, a) {
index = (x + (height - 1 - y) * imageData.width) * 4;
imageData.data[index+0] = r;
View quicksort.hs
import Data.List
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort xs = quicksort lessThanPivot ++ equalToPivot ++ quicksort greaterThanPivot
where
(lessThanPivot, equalToPivot, greaterThanPivot) = epart (head xs) xs
epart :: Ord a => a -> [a] -> ([a], [a], [a])
epart n xs = inner ([], [], []) xs
where
View door.hs
{-# LANGUAGE GADTs, DataKinds, TypeFamilies, FlexibleInstances #-}
-- from my stackoverflow question: https://stackoverflow.com/questions/40939508/translate-a-scala-type-example-to-haskell
data Status = Open | Closed deriving(Show)
data Door (status :: Status) = Door
--OpenDoor :: Door Open
--ClosedDoor :: Door Closed
You can’t perform that action at this time.