scalaz

Circe decoder for scalaz.Maybe

◇◆丶佛笑我妖孽 提交于 2019-12-11 00:30:56
问题 Here's a simple finch server, using circe as decoder: import com.twitter.finagle.http.RequestBuilder import com.twitter.io.Buf import io.circe.generic.auto._ import io.finch._ import io.finch.circe._ case class Test(myValue: Int) val api = post("foo" :: body.as[Test]) { test: Test => Ok(test) } val bodyPost = RequestBuilder() .url("http://localhost:8080/foo") .buildPost(Buf.Utf8("""{ "myValue" : 42 }""")) api.toService.apply(bodyPost).onSuccess { response => println(s"$response: ${response

How to close AsyncHttpClient in scalaz Task

梦想的初衷 提交于 2019-12-10 23:03:14
问题 I am trying to combine AsyncHttpClient and Scalaz Task together. Normally, if I am using AsyncHttpClient, I can invoke client.close to stop the client. val asyncHttpClient = new AsyncHttpClient() println(asyncHttpClient.prepareGet("http://www.google.com")) asyncHttpClient.close() So current will be stopped. However, if I wrap the api call into the Task. I dont know how to stop it. def get(s: String) = Task.async[Int](k => { asyncHttpClient.prepareGet(s).execute(toHandler) Thread.sleep(5000)

How do I make a Scalaz ZIO lazy?

 ̄綄美尐妖づ 提交于 2019-12-10 19:56:19
问题 I have a heavy side-effecting function (think database call) that I want to use as a lazy value, so that it gets called only on first use (and not at all if never used). How do I do this with ZIO? If my program looks like this, the function gets called only once (but even the result is not used at all): import scalaz.zio.IO import scalaz.zio.console._ object Main extends scalaz.zio.App { def longRunningDbAction: IO[Nothing, Integer] = for { _ <- putStrLn("Calling the database now") } yield 42

How to compose Future of Either/Disjunction in Scala

岁酱吖の 提交于 2019-12-10 18:46:03
问题 Suppose I have the following functions to compose: val mayFail1: Int => Error \/ Int = ??? val slowAndMayFail: Int => Error \/ String = ??? val mayFail2: String => Error \/ Int = ??? val mayFail3: String => Error \/ Int = ??? val mayFail: Int => Error \/ Int = {x => for { x1 <- mayFail1(x) s <- slowAndMayFail(x1) x2 <- mayFail2(s) x3 <- mayFail3(x2) } yield x3 } The function mayFail is slow because of slowAndMayFail , so I would like it to return a future of Error \/ Int . The straightforward

How to write a scalaz.IsEmpty parameter for generic types

我的未来我决定 提交于 2019-12-10 18:43:32
问题 I am trying to write a generic method that wraps anything that has an scalaz.IsEmpty typeclass instance into an Option . It should return None for empty values, and wrap it into Some if it is non-empty. Here's what I've come up so far: import scalaz._ import Scalaz._ def asOption0[C](c: C)(implicit ev: IsEmpty[({ type B[A] = C })#B]) = if (ev.isEmpty(c)) None else Some(c) def asOption1[A, C[_]](c: C[A])(implicit ev: IsEmpty[C]) = if (ev.isEmpty(c)) None else Some(c) asOption0 works for

How to compose functions that return Validation?

☆樱花仙子☆ 提交于 2019-12-10 18:15:52
问题 This is a follow-up to my previous question Suppose I have two validating functions that return either the input if it is valid or the error messages if it is not. type Status[A] = ValidationNel[String, A] val isPositive: Int => Status[Int] = x => if (x > 0) x.success else s"$x not positive".failureNel val isEven: Int => Status[Int] = x => if (x % 2 == 0) x.success else s"$x not even".failureNel Suppose also that I need to validate an instance of case class X : case class X(x1: Int, // should

Combining the elements of 2 lists

混江龙づ霸主 提交于 2019-12-10 17:38:12
问题 Assume we have two lists : val l1=List("a","b","c") val l2 = List("1","2","3") What I want is : List("a1", "b2", "c3") that is, adding the nth element of l1 with the nth element of l2 A way to achieve it is : (l1 zip l2).map (c => {c._1+c._2}) I just wonder if one could achieve it with an Applicative. I tried : (l1 |@| l2) { _+ _ } but it gives all the combinations : List(a1, a2, a3, b1, b2, b3, c1, c2, c3) Any idea? Thank you Benoit 回答1: You cannot do that with strict lists, so instead use

Scalaz unboxed tagged type not automatically unboxed

不羁的心 提交于 2019-12-10 16:53:29
问题 Reading http://eed3si9n.com/learning-scalaz/Tagged+type.html and trying out the sample code: import scalaz._; import Scalaz._ sealed trait KiloGram def KiloGram[A](a: A): A @@ KiloGram = Tag[A, KiloGram](a) val mass = KiloGram(20.0) 2 * mass according to the guide, should yield 40.0 , however, on Scala 2.11.2 I get: scala> 2 * mass <console>:17: error: overloaded method value * with alternatives: (x: Double)Double <and> (x: Float)Float <and> (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int

Scalaz: how does `scalaz.syntax.applicative._` works its magic

人走茶凉 提交于 2019-12-10 15:20:37
问题 This question is related to this one, where I was trying to understand how to use the reader monad in Scala. In the answer the autor uses the following code for getting an instance of ReaderInt[String] : import scalaz.syntax.applicative._ val alwaysHello2: ReaderInt[String] = "hello".point[ReaderInt] Which mechanisms does Scala use to resolve the type of the expression "hello".point[ReaderInt] so that it uses the right point function? 回答1: A good first step any time you're trying to figure

More haskell-like applicative syntax in scalaz

我与影子孤独终老i 提交于 2019-12-10 14:44:14
问题 I am experimenting with scalaz. I tried writing code in applicative code. I wrote code like this: val max: Option[Int] = (a |@| b) { math.max(_, _) } I didn't like this code very much. I would like to code which is closer to Haskell style, something like this: val max: Option[Int] = { math.max(_, _) } <$> a <*> b Is this possible. And why scalaz didn't implement it this way? 回答1: Scala's type inference, is much more limited than in Haskell (identifier overloading, which comes with the JVM