Skip to content

Instantly share code, notes, and snippets.

@tonymorris
tonymorris / gist:3087504
Created July 11, 2012 02:09
Comonad/Monad
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait Extend[F[_]] extends Functor[F] {
// coflatmap
def extend[A, B](f: F[A] => B): F[A] => F[B]
}
trait Comonad[F[_]] extends Extend[F] {
@puffnfresh
puffnfresh / WriterTExample.scala
Created August 14, 2012 01:57
Example of scalaz' WriterT for Atlassian (pure functional logging)
import scalaz.WriterT
import scalaz.NonEmptyList
import scalaz.syntax.id._
import scalaz.std.option._
import scalaz.syntax.std.option._
type OptionLogger[A] = WriterT[Option, NonEmptyList[String], A]
val two: OptionLogger[Int] = WriterT.put(2.some)("The number two".wrapNel)
@puffnfresh
puffnfresh / currying.js
Created September 7, 2012 07:13
Support for both curried and uncurried application in functional JavaScript
function curry(f) {
return function(x) {
var g = f.bind(this, x);
if(g.length == 0) return g();
if(arguments.length > 1) return curry(g).apply(this, [].slice.call(arguments, 1));
return curry(g);
};
}
var sum = curry(function(x, y) {
@travisbrown
travisbrown / kata-bank-ocr.scala
Created September 21, 2012 18:09
KataBankOCR checksum at the type level
/**
* We can use the Scala type system (with help from Miles Sabin's Shapeless
* library) to solve the bank account number validation problem in the second
* user story of the KataBankOCR kata on Coding Dojo:
*
* http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR
*
* By Travis Brown in response to a question by Paul Snively on the Shapeless
* Dev mailing list:
*
@channingwalton
channingwalton / FizzBuzz.scala
Created October 4, 2012 20:03
FizzBuzz with Scalaz
// Inspired by http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html
object FizzBuzz extends App {
import scalaz._
import Scalaz._
def fizzbuzz(i: Int) = ((i % 3 == 0) option "fizz") |+| ((i % 5 == 0) option "buzz") | i.shows
for (i <- 1 to 100) {println(i + " " + fizzbuzz(i))}
}
@tonymorris
tonymorris / FizzBuzz.hs
Created October 10, 2012 01:37
FizzBuzz
module Main where
import Control.Applicative ((<*>))
import Data.Semigroup ((<>)) -- http://hackage.haskell.org/package/semigroups
import Data.Maybe (fromMaybe, listToMaybe, maybe)
import System.Environment (getArgs)
import Data.Lens.Partial.Common(getorPL, headLens) -- http://hackage.haskell.org/package/data-lens
fizzbuzz ::
Integral a =>
@tonymorris
tonymorris / TypeClass.hs
Last active September 15, 2020 13:17
Type-class hierarchy
Moved to https://github.com/tonymorris/type-class
@tonymorris
tonymorris / TReader.scala
Created October 13, 2012 11:16
Reader monad in Scala
case class T() // your data type that you want to "implicitly" thread through
case class TReader[+A](run: T => A) {
def map[B](f: A => B): TReader[B] =
sys.error("VFJlYWRlcihmIGNvbXBvc2UgcnVuKQ==")
def flatMap[B](f: A => TReader[B]): TReader[B] =
sys.error("VFJlYWRlcih0ID0+IGYocnVuKHQpKSBydW4gdCk=")
def &&&[B](x: TReader[B]): TReader[(A, B)] =
(use '[clojure.core.logic])
(require '[clojure.core.logic.fd :as fd])
(defn simple []
(run* [x y]
(fd/in x y (fd/interval 0 9))
(fd/eq
(= (+ x y) 9)
(= (+ (* 4 x) (* 2 y)) 24))))
@fogus
fogus / gist:5163546
Last active December 14, 2015 23:09
How do I say this Haskell?
-- THE SEARCH CONTINUES
data GenericThing = SpecificThing Char
| DifferentSpecificThing Double
| AnotherSpecificThing String
| YetAnotherDifferentSpecificThing Integer
foo :: [GenericThing] -> GenericThing
foo [] = return $ AnotherSpecificThing ""
foo [SpecificThing one] = -- do something