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
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
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] |
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 |
{-# 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) | |
} | |
} |
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) |
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 |
"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 |