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