Skip to content

Instantly share code, notes, and snippets.

object Nats {
//we define natural numbers
//using peano numbers
//inspired from shapeless and https://gist.github.com/jto/2dc882c455b79378289f
trait Nat
case object Z extends Nat
case class Succ[N <: Nat](n: N) extends Nat
@manojo
manojo / Gen.scala
Created July 6, 2017 09:11
Generators using type classes and derivations
import shapeless._
object enumerate {
trait Gen[T] {
def gen: Stream[T]
}
object Gen {
def mkGen[T](st: Stream[T]): Gen[T] = new Gen[T] {
@manojo
manojo / tweet.json
Last active February 4, 2016 10:13
JSON formatted response for tweets
{
"statuses": [
{
"metadata": {
"result_type": "popular",
"iso_language_code": "en"
},
"created_at": "Tue Sep 03 00:04:11 +0000 2013",
"id": 3.7468420748358e+17,
"id_str": "374684207483584512",
@manojo
manojo / Collapsible.scala
Last active February 4, 2016 00:27
Implementing a type-level collapsible function
/**
* For type-level calculations, it might help to think of a prolog implementation
* of `collapse`. We inline the use of `splitAt` and `reduceLeft`, for saving
* some lines
*
* // it is true that collapsing a list with no ints gives the list back as remainder
* collapse(ls, nil, op, nil, ls).
*
* // we can collapse a list for a given `Nat n`, if we can split the input
* // list, reduce the prefix (over the `op` operator), recursively collapse the
@manojo
manojo / TestStruct.scala
Created May 22, 2015 21:03
LMS Structs
import scala.virtualization.lms.common._
import scala.reflect.SourceContext
import scala.virtualization.lms.internal.GenericCodegen
import lms._
import java.io.PrintWriter
import java.io.StringWriter
import java.io.FileOutputStream
trait TestStruct extends Base with IfThenElse with Equal {
@manojo
manojo / TestNestedClasses.scala
Created March 9, 2015 21:47
Nested Options and Slick
package models
import slick.lifted.Tag
trait Test {
val profile: slick.driver.JdbcProfile
import profile.simple._
/**
* The wrapper class has an optional quantity field
* which itself has an optional size field
@manojo
manojo / LambdaCalculus.scala
Created January 12, 2015 09:53
A direct embedding of the lambda calculus
/**
*
* t ::= x
* | \x. t
* | t t
*/
object LambdaCalculus {
/**
* The type that represents
@manojo
manojo / Fixed.scala
Last active August 29, 2015 14:06
An attempt at implementing the fix combinator, in Scala
trait Fixed {
type U
type T = U => U
case class Rec(val f: Rec => T) {
def apply(x: Rec): T = f(x)
}
/** the recursively typed definition of fix