scalaz

Converting a tuple of options to an option of tuple with Scalaz or Shapeless

好久不见. 提交于 2019-11-28 23:17:16
Having (Some(1), Some(2)) I expect to get Some((1, 2)) and having (Some(1), None) I expect to get None You can use the fact that Scalaz 7 provides a Bitraverse instance for tuples and then sequence as usual (but with bisequence instead of sequence ): scala> import scalaz._, std.option._, std.tuple._, syntax.bitraverse._ import scalaz._ import std.option._ import std.tuple._ import syntax.bitraverse._ scala> val p: (Option[Int], Option[String]) = (Some(1), Some("a")) p: (Option[Int], Option[String]) = (Some(1),Some(a)) scala> p.bisequence[Option, Int, String] res0: Option[(Int, String)] = Some(

How to combine Option values in Scala?

廉价感情. 提交于 2019-11-28 22:42:44
I want to be able to apply an operation f: (T,T) => T to Option[T] values in Scala. I want the result to be None if any of the two values is None . More specifically, I want to know if there is a shorter way to do the following: def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = { (x,y) match { case (Some(u),Some(v)) => Some(f(u,v)) case _ => None } } I have tryied (x zip y) map {case (u,v) => f(u,v)} but the result is an Iterator[T] not an Option[T] . scala> val (x, y) = (Some(4), Some(9)) x: Some[Int] = Some(4) y: Some[Int] = Some(9) scala> def f(x: Int, y: Int) = Math

How to handle `Reader` monad and `Try`?

我与影子孤独终老i 提交于 2019-11-28 20:37:15
问题 I'm reading this great article about dependency injection in scala with Reader monad. The original example is working well, but I did a little bit change on the return types of the UserRepository.get/find . It was User , but I changed it to Try[User] . Then the code won't be compiled, I had tries many times, but still without lucky. import scala.util.Try import scalaz.Reader case class User(email: String, supervisorId: Int, firstName: String, lastName: String) trait UserRepository { def get

Finding my way through Scalaz [duplicate]

ぃ、小莉子 提交于 2019-11-28 16:01:17
问题 Possible Duplicate: Good scalaz introduction I would like to learn more about Scalaz, possibly using Scalaz7 to be avoid rewiring my brain once it is declared stable. My problem is that Scalaz contains a lot of functionality. While most of it is meant to be independent of other parts, I would like to have a bird's eye view of the global funcitonality offered by Scalaz and how it is organized. As far as I know, Scalaz offers, among other things, Functor , Applicative and Monad traits, new

Doing HTTP request in Scala

主宰稳场 提交于 2019-11-28 15:56:58
I am trying to issue a simple POST request to a webservice which returns some XML in Scala. It seems that Dispatch is the standard library used for this task, but I cannot find documentation for it. The main site, which I link above, explains at length what is a promise and how to do asynchronous programming, but does not actually document the API. There is a periodic table - which looks a bit scary - but it only seems useful to people who already know what to do and only need a reminder for the cryptic syntax. It also seems that Scalaz has some facility for HTTP , but I cannot find any

Different Scala Actor Implementations Overview

 ̄綄美尐妖づ 提交于 2019-11-28 15:23:11
I'm trying to find the 'right' actor implementation. I realized there is a bunch of them and it's a bit confusing to pick one. Personally I'm especially interested in remote actors, but I guess a complete overview would be helpful to many others. This is a pretty general question, so feel free to answer just for the implementation you know about. I know about the following Scala Actor implementations (SAI). Please add the missing ones. Scala 2.7 (difference to) Scala 2.8 Akka ( http://www.akkasource.org/ ) Lift ( http://liftweb.net/ ) Scalaz ( http://code.google.com/p/scalaz/ ) What are the

Reader Monad for Dependency Injection: multiple dependencies, nested calls

五迷三道 提交于 2019-11-28 13:34:22
问题 When asked about Dependency Injection in Scala, quite a lot of answers point to the using the Reader Monad, either the one from Scalaz or just rolling your own. There are a number of very clear articles describing the basics of the approach (e.g. Runar's talk, Jason's blog), but I didn't manage to find a more complete example, and I fail to see the advantages of that approach over e.g. a more traditional "manual" DI (see the guide I wrote). Most probably I'm missing some important point,

Function syntax puzzler in scalaz

*爱你&永不变心* 提交于 2019-11-28 13:30:54
问题 Following watching Nick Partidge's presentation on deriving scalaz, I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5)) I was trying to understand what the <|*|> method was doing, here is the source code: def <|*|>[B](b: M[B])(implicit t:

What is a DList?

限于喜欢 提交于 2019-11-28 08:59:05
I tried googling for this but all I got were stories about minor celebrities. Given the lack of documentation, what is a DList ? retronym It's a Difference List, along the lines of "Difference List as functions" scala> val (l1, l2, l3) = (List(1, 2, 3), List(4, 5, 6), List(7, 8, 9)) l1: List[Int] = List(1, 2, 3) l2: List[Int] = List(4, 5, 6) l3: List[Int] = List(7, 8, 9) Efficient prepending: scala> l1 ::: l2 ::: l3 res8: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9) Inefficient appending. This creates an intermediate list (l1 ++ l2), then ((l1 ++ l2) ++ l3) scala> l1 ++ l2 ++ l3 // inefficient

Convert a List of Options to an Option of List using Scalaz

白昼怎懂夜的黑 提交于 2019-11-28 07:16:57
I want to transform a List[Option[T]] into a Option[List[T]] . The signature type of the function is def lo2ol[T](lo: List[Option[T]]): Option[List[T]] The expected behavior is to map a list that contains only Some s into a Some containing a list of the elements inside the elements Some 's. On the other hand, if the input list has at least one None , the expected behavior is to just return None . For example: scala> lo2ol(Some(1) :: Some(2) :: Nil) res10: Option[List[Int]] = Some(List(1, 2)) scala> lo2ol(Some(1) :: None :: Some(2) :: Nil) res11: Option[List[Int]] = None scala> lo2ol(Nil : List