Skip to content

Instantly share code, notes, and snippets.

@Jazzatola
Jazzatola / Identity.scala
Created March 2, 2018 12:57
Identity Monad
package com.jamesmaggs.monads
trait Identity[A] { self =>
def run: A
def map[B](f: A => B): Identity[B] =
new Identity[B] { def run = f(self.run) }
def flatMap[B](f: A => Identity[B]): Identity[B] =
@Jazzatola
Jazzatola / Example.scala
Created April 25, 2015 16:11
How do I make this compile? I must be missing a type bound somewhere...
case class Foo[+A](p: A => Boolean) {
def contains[B >: A](b: B): Boolean = p(b)
}
package org.example
case class PimpMyPredicate[A](f: A => Boolean) {
def &&(g: A => Boolean): A => Boolean = a => g(a) && f(a)
}
object PimpMyPredicate {
implicit def enrichPredicate[A](f: A => Boolean): PimpMyPredicate[A] = PimpMyPredicate(f)
}
@Jazzatola
Jazzatola / CPS.scala
Created January 28, 2015 17:27
Continuation Passing
object CPS {
def chainCps[A, B, R](f: ((A => R) => R), g: (A => ((B => R) => R))): (B => R) => R =
(h: B => R) => f(a => g(a)(h))
}
@Jazzatola
Jazzatola / Mapper.scala
Created January 21, 2015 20:59
Tail recursive map implementation
package org.example
import scala.annotation.tailrec
object Mapper {
@tailrec
def foldLeft[A, B](as: List[A], z: B)(f: (B, A) => B): B = as match {
case Nil => z
case a :: as => foldLeft(as, f(z, a))(f)
@Jazzatola
Jazzatola / Liskov.scala
Last active August 29, 2015 14:13
Using covariant and contravariant types to demonstrate the Liskov substitution principle
package com.example
class GrandParent
class Parent extends GrandParent
class Child extends Parent
/*
* contravariant parameter p
* covariant result r
*/
@Jazzatola
Jazzatola / app.rb
Last active December 17, 2015 22:58
Minimal sinatra app exposing suspected bug in warden. The callback is not called when logging out.
require 'sinatra'
require 'warden'
class User
def initialize(id = nil)
@id = id
end
def self.find_by_username(username)
@Jazzatola
Jazzatola / random_colors.html
Created February 1, 2013 17:06
Random colours animated with jquery.
<html>
<head>
<title>Random Colors</title>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<style type="text/css">
body {
margin: 0;
}