Skip to content

Instantly share code, notes, and snippets.

View pjrt's full-sized avatar
🤔
Thinking about why Github now has statuses

Pedro Rodriguez Tavarez pjrt

🤔
Thinking about why Github now has statuses
View GitHub Profile
@pjrt
pjrt / JsonAlternative.md
Last active August 29, 2015 14:02
A way of allowing backwards compatibility for Json field names using Play Json Combinators

The following shows how to use the alternative typeclass in JsResult

The following imports are assumed in all examples:

import play.api.libs.json._
import play.api.libs.functional.syntax._

Lets say we have the following case class

@pjrt
pjrt / TypeclassBox.scala
Last active August 29, 2015 14:05
A Typeclass Box in Scala. Allows for a single type to represent any type that responds to a typeclass.
import language.higherKinds
// Our typeclass with a simple foo :: a -> String func
trait Foo[A] {
def foo(a: A): String
}
object Foo {
implicit def apply[A](implicit ev: Foo[A]) = ev
}
// A Typeclass Box for Foo. It contains an abtract (existential) type A, a value of A and an instance for Foo[A]
@pjrt
pjrt / TypeclassReturns.hs
Created October 20, 2014 22:03
Returning functions and values restricted by typeclasses
class Writable a where
write :: a -> String
data Box = Box String
data Hax = Hax String
instance Writable Hax where
write (Hax str) = str
instance Writable Box where
@pjrt
pjrt / CoyonedaTypebox.hs
Created March 10, 2015 01:20
Turns out, you can represent a typeclass box as a simple constrained function returning a Coyoneda
{-# LANGUAGE GADTs #-}
import Data.Functor
import Control.Monad
import Control.Monad.Identity
-- Taken from Edward Kmett's definition
-- https://hackage.haskell.org/package/kan-extensions-3.7/docs/src/Data-Functor-Coyoneda.html#Coyoneda
data Coyoneda f a where
import language.higherKinds
// Scala doesn't have Identity, so here....
object Id {
type Identity[+A] = A
implicit val identityFunctor: Functor[Identity] = new Functor[Identity] {
def fmap[A, B](f: A => B)(fa: Identity[A]) = f(fa)
}
}
@pjrt
pjrt / TestNumericWidenImplicits.scala
Last active August 29, 2015 14:23
numeric widen on the implicit system
trait T[A]
object Main {
implicit val doubleInst: T[Double] = new T[Double] {}
def f[A](a: A)(implicit ev: T[A]): T[A] = ev
f(1: Int)
f(1: Float)
f(1: Double)
@pjrt
pjrt / equi.hs
Last active December 4, 2015 17:03
equi in Haskell
equi :: [Int] -> Maybe Int
equi [] = Nothing
equi ns = go 0 0 ns
where
fullSum = sum ns
go _ _ [] = Nothing
go n leftSum (x:xs) =
let rightSum = fullSum - leftSum - x
in if rightSum == leftSum
then Just n
@pjrt
pjrt / Test.scala
Last active December 15, 2015 22:19
"test1" should "test1" taggedAs(Tag("Test1")) in {
db.withSession {implicit s: Session =>
(A.ddl ++ B.ddl ++ AToB.ddl).drop
object A extends Table[(Int, String)]("a") {
def id = column[Int]("id", O.PrimaryKey)
def s = column[String]("s")
def * = id ~ s
def bs = AToB.filter(_.aId === id).flatMap(_.bFK)
}
{-# LANGUAGE MultiWayIf #-}
-- http://codercareer.blogspot.com/2013/03/no-45-closest-node-in-binary-search-tree_2.html
data BTree a = Nil | Leaf a | Node a (BTree a) (BTree a)
-- | Given an /a/, find the node whose value is closest to the given /a/.
findClosest :: (Num a, Ord a) => a -> BTree a -> a
findClosest _ Nil = error "Empty tree"
findClosest _ (Leaf v) = v -- If we a given a single node tree, the value
{-# LANGUAGE MultiWayIf #-}
-- http://codercareer.blogspot.com/2013/03/no-46-nodes-with-sum-in-binary-search.html
data Tree a = Leaf a | Branch a (Tree a) (Tree a)
deriving Show
getValue (Leaf v) = v
getValue (Branch v _ _) = v