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