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 / 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)
}
@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 / TypeclassBox.hs
Last active July 26, 2018 08:55
A Typeclass Box in Haskell. Allows for a single type to represent any type that responds to a typeclass.
{-# LANGUAGE GADTs, ConstraintKinds, FlexibleInstances, RankNTypes #-}
-- Our typeclass with a simple foo :: a -> String func
class Foo a where
foo :: a -> String
-- Our test data types and their instances of Foo
data MyFoo = MyFoo String
data MyBar = MyBar Int
@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 / S3Puller.scala
Created May 14, 2015 19:15
S3 List Status ad-infinitude
import com.amazonaws.services.s3._, model._
import com.amazonaws.auth.BasicAWSCredentials
val request = new ListObjectsRequest()
request.setBucketName(bucket)
request.setPrefix(prefix)
request.setMaxKeys(pageLength)
def s3 = new AmazonS3Client(new BasicAWSCredentials(key, secret))
val objs = s3.listObjects(request) // Note that this method returns truncated data if longer than the "pageLength" above. You might need to deal with that.
@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