Skip to content

Instantly share code, notes, and snippets.

@nbenns
nbenns / dsl.js
Last active January 12, 2016 18:02
'use strict';
const R = require('ramda');
const rules = {"&": [
{">": [ "!products.blah", 2 ]},
{">": [ "!products.meow", 1 ]}
]};
const basket = { products: { meow: 2, blah: 3 } };
'use strict';
const R = require('ramda');
/*
* Mathematically, Monoids are a SemiGroup with an identity element.
*
* Monoids are defined under a certain operation, they require 3 things:
* the operation must be associative, so op(op(A,B),C) == op(A,op(B,C))
* an identity (sometimes called the Zero) such that type op(A, I) = A and op(I, A) = A,
@nbenns
nbenns / betterenum.scala
Created June 19, 2017 15:03
better scala enum with play reads/writes
import play.api.libs.json._
sealed trait Color
object Color {
type Red = Red.type
type Green = Green.type
type Blue = Blue.type
case object Red extends Color
case object Green extends Color
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder
import com.amazonaws.services.dynamodbv2.model._
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType._
import scala.collection.convert.decorateAsJava._
val credentials = new BasicAWSCredentials("dev", "dev")
val credProvider = new AWSStaticCredentialsProvider(credentials)
val endpointConfiguration = new EndpointConfiguration("http://localhost:8000", "local")
import scala.language.higherKinds
import cats.Functor
import cats.implicits._
/*
* A Free Functor is a Functor definition that doesn't care what F[_] is.
* We are Free from having to know about F[_] until we run() it.
* It uses the second Functor Law: fmap (g . f) = fmap g . fmap f
* FreeF is just a container that composes functions
* When you run it, you give it an actual container and the intial value
sealed trait Json
case class JsonString(value: String) extends Json
sealed trait Yaml
case class YamlString(value: String) extends Yaml
object Symbols {
type |>[From, To] = Converter[From, To]
type <|[To, From] = Converter[From, To]
}
// From http://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/
type ∧[A, B] = A with B
type ¬[A] = A => Nothing
type ¬¬[A] = ¬[¬[A]]
type ∨[A, B] = ¬[¬[A] ∧ ¬[B]] // De Morgan equivalence
type <|[X, T] = ¬¬[X] <:< T // lift and check subtype
def size[T: ? <| (Int ∨ String)](t: T): Int =
t match {
{-# 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
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
@nbenns
nbenns / flame.html
Last active October 15, 2020 13:41
<html>
<body>
<canvas id="canvas1" width="500", height="500">
Random Canvas
</canvas>
<script type="text/javascript">
element = document.getElementById("canvas1");
c = element.getContext("2d");