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,
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
<?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>
@nbenns
nbenns / SizedMatrix.scala
Last active June 10, 2018 17:09
Play with adding size of col and row as type parameters to a matrix
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
}