scalaz

Merge maps by key

旧巷老猫 提交于 2019-11-27 07:06:48
Say I have two maps: val a = Map(1 -> "one", 2 -> "two", 3 -> "three") val b = Map(1 -> "un", 2 -> "deux", 3 -> "trois") I want to merge these maps by key, applying some function to collect the values (in this particular case I want to collect them into a seq, giving: val c = Map(1 -> Seq("one", "un"), 2 -> Seq("two", "deux"), 3 -> Seq("three", "trois")) It feels like there should be a nice, idiomatic way of doing this. scala.collection.immutable.IntMap has an intersectionWith method that does precisely what you want (I believe): import scala.collection.immutable.IntMap val a = IntMap(1 ->

Update operations on a Scala Case Class

情到浓时终转凉″ 提交于 2019-11-27 04:26:18
I have two instantiated case classes of the same type. case class Foo(x : Option[String], y : Option[String], z : Option[String]) Lets call the instantiated classes A and B. val a = Foo(x=Some("foo"), y=Some("bar"), z=Some("baz")) val b = Foo(x=None, y=Some("etch"), z=None) I'm wondering if its possible to update case class A with B in a single operation in a generic way. val c = b *oper* a // produces Foo(x=Some("foo"), y=Some("etch"), z=Some("baz")) with parameters that are set as None ignored. Ideally the operation should also be generic so it can act on any type of case class. I have some

How to chain Future[\/[A,B]] in scala?

假装没事ソ 提交于 2019-11-27 02:20:31
问题 How I can do a for comprehension with the data of type Future[\/[String,Int]] Here is a starting point, which doesn't compile. import scala.concurrent.{ExecutionContext,future,Future} import scalaz._ import Scalaz._ import ExecutionContext.Implicits.global def calculateStuff(i:Int):Future[\/[String,Int]] = future{\/-(i)} for { v1Either <- calculateStuff(1) v1Int <- v1Either v2Either < calculateStuff(v1Int) v2Int <- v2Either v3Either <- calculateStuff(v2Int) v3Int <- v3Either } yield { v1Int +

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

不羁的心 提交于 2019-11-27 01:46:54
问题 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))

Turning A => M[B] into M[A => B]

隐身守侯 提交于 2019-11-27 01:45:32
问题 For a monad M , Is it possible to turn A => M[B] into M[A => B] ? I've tried following the types to no avail, which makes me think it's not possible, but I thought I'd ask anyway. Also, searching Hoogle for a -> m b -> m (a -> b) didn't return anything, so I'm not holding out much luck. 回答1: In Practice No, it can not be done, at least not in a meaningful way. Consider this Haskell code action :: Int -> IO String action n = print n >> getLine This takes n first, prints it (IO performed here),

Good scalaz introduction [closed]

梦想与她 提交于 2019-11-26 23:44:32
问题 Recently scalaz caught my eye. It looks very interesting, but I have not found any good introduction to the library. Seems that scalaz incorporates a lot of ideas from haskell and mathematics. Most articles that I found assume that you already feel comfortable with these concepts. What I'm looking for is gradual introduction to the library and underlying concepts - from simple and basic concepts to more advanced (which basesd in basics). I also looked to the examples, but it's hard for me to

why isn't Validation a Monad? (scalaz7)

蹲街弑〆低调 提交于 2019-11-26 22:45:35
问题 an example use case: def div2(i: Int): Validation[String, Int] = if (i%2 == 0) Validation.success(i/2) else Validation.failure("odd") def div4(i: Int) = for { a <- div2(i) b <- div2(a) } yield b error : Unable to unapply type scalaz.Validation[String,Int] into a type constructor of kind M[_] that is classified by the type class scalaz.Bind I guess the error is caused by the compiler can't find a Monad instance for Validation[String, Int] I can make one for myself, like: object Instances {

How to reduce Seq[Either[A,B]] to Either[A,Seq[B]]?

我的梦境 提交于 2019-11-26 22:16:55
问题 Given a sequence of eithers Seq[Either[String,A]] with Left being an error message. I want to obtain an Either[String,Seq[A]] where I get a Right (which will be a Seq[A] ), if all elements of the sequence are Right . If there is at least one Left (an error message), I'd like to obtain the first error message or a concatenation of all error messages. Of course you can post scalaz code but I'm also interested in code not using it. Edit I've changed the title, which originally asked for an

How to convert A[B[C]] to B[A[C]] if A and B are monads?

好久不见. 提交于 2019-11-26 20:57:05
I'm looking for a more general solution which exploits monads (and monoids possibly) to achieve the same as if( xs.contains(None) ) None else Some(xs.flatten) does for xs of type Seq[Option[A]] . How can I do that with Scalaz? I feel like I'm missing something evident. Having two monads is both not enough (for M ) and more than enough (for N )—which adds up to not enough, of course—but if M has a Traverse instance and N has an Applicative instance, you can use sequence . For example: import scalaz._, Scalaz._ def foo[A](xs: List[Option[A]]): Option[List[A]] = xs.sequence This has the semantics

What is a DList?

扶醉桌前 提交于 2019-11-26 20:49:55
问题 I tried googling for this but all I got were stories about minor celebrities. Given the lack of documentation, what is a DList? 回答1: 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