scalaz

Updating a scala case class [duplicate]

拜拜、爱过 提交于 2019-12-11 14:11:36
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Update operations on a Scala Case Class This question came to me this evening. I have two instantiated case classes of the same type. case class Foo(a : Option[String], b : Option[String], c : Option[String]) Lets call the instantiated classes A and B. val a = Foo(a=Some("foo"), b=Some("bar"), c=Some("baz")) val b = Foo(a=None, b=Some("etch"), c=None) I'm wondering if its possible to update case class A with B

Tagged type : type mismatch

≯℡__Kan透↙ 提交于 2019-12-11 12:28:06
问题 I try to use Tagged Type from scalaz to strengthen type safety. I encountered a warning and an error that I don't understand. Can you explain me both ? Here is the output of the console : scala> sealed trait PostId defined trait PostId scala> def PostId(l: Long) : Long @@ PostId = Tag[Long, PostId](l) PostId: (l: Long)scalaz.@@[Long,PostId] warning: previously defined trait PostId is not a companion to method PostId. Companions must be defined together; you may wish to use :paste mode for

Scalaz code highlighted with red in Intellij Idea 12 and 13

余生长醉 提交于 2019-12-11 10:34:55
问题 We have a project with Play framework 2 coded with Scala. We are using Scalaz for validation in Play controllers like "validateSomething |@| validateSomethingElse |@| validateYetSomethingElse". Idea (v.12 and v. 13 ) doesn't recognise for some reason the syntax at all, even though Scalaz imports are fine. Is there anything we could try to do to fix the situation? It also seems to be making the Idea really laggy and slow when editing the classes sontaining the "unrecognised" scalaz symbols.

How do I define a function for a Monad Stack of State, Disjunction, and List?

半世苍凉 提交于 2019-12-11 08:53:48
问题 I've got a list monad List[Int] . I want to generate effects (Disjunction, and state) based on the values of the list. Here is my monad stack and the code to run the monad stack. My question is what should be proper way define checkNum so that I can generate the correct effects? My expected output should List(("", \/-(1), ("Error: 2", -\/(Throwable())), ("",\/-(3)), ("Error: 4", -\/(Throwable()))) import scalaz._ import Scalaz._ type EitherTH[F[_], A] = EitherT[F, Throwable,A] type StateTH[F[

Apply several string transformations in scala

北城以北 提交于 2019-12-11 06:26:24
问题 I want to perform several ordered and successive replaceAll(...,...) on a string in a functional way in scala. What's the most elegant solution ? Scalaz welcome ! ;) 回答1: First, let's get a function out of the replaceAll method: scala> val replace = (from: String, to: String) => (_:String).replaceAll(from, to) replace: (String, String) => String => java.lang.String = <function2> Now you can use Functor instance for function, defined in scalaz. That way you can compose functions, using map (or

scalaz, Disjunction.sequence returning a list of lefts

◇◆丶佛笑我妖孽 提交于 2019-12-11 06:05:06
问题 In scalaz 7.2.6, I want to implement sequence on Disjunction , such that if there is one or more lefts, it returns a list of those, instead of taking only the first one (as in Disjunction.sequenceU ): import scalaz._, Scalaz._ List(1.right, 2.right, 3.right).sequence res1: \/-(List(1, 2, 3)) List(1.right, "error2".left, "error3".left).sequence res2: -\/(List(error2, error3)) I've implemented it as follows and it works, but it looks ugly. Is there a getRight method (such as in scala Either

Scalaz validation with Argonaut

一笑奈何 提交于 2019-12-11 04:04:09
问题 I have a case class and companion object: case class Person private(name: String, age: Int) object Person { def validAge(age: Int) = { if (age > 18) age.successNel else "Age is under 18".failureNel } def validName(name: String) = { name.successNel } def create(name: String, age: Int) = (validAge(age) |@| validName(name))(Person.apply) } I want to use Argonaut to parse some JSON and return a Person OR some errors, as a list. So I need to: Read the JSON from a string, and validate the string is

Printing Each Character Typed w/ Scalaz

。_饼干妹妹 提交于 2019-12-11 03:32:42
问题 I'm trying to write the following Haskell function, but in Scala (using scalaz): ghci>let f = do { x <- getChar; _ <- putChar(x); return () } ghci>f -- then type '4' 44ghci> Here's what I tried: def showInput: IO[Unit] = for { c <- getChar _ <- putChar(c) } yield () Then I ran it: scala> net.repl.Foo.showInput res2: scalaz.effect.IO[Unit] = scalaz.effect.IOFunctions$$anon$6@73e3ae3a After running the method, I typed 1234 . But, I had to press enter before the output (of 1 ) showed up: scala>

Scalaz 7 - why using type alias results in ambigous typeclass resolution for Reader

喜欢而已 提交于 2019-12-11 03:08:31
问题 Code to test with: import scalaz.{Reader, Applicative} class ReaderInstanceTest { type IntReader[A] = Reader[Int, A] val a = Applicative[({type l[A] = Reader[Int, A]})#l] // fine val b = Applicative[IntReader] // ^ ambigous implicit values // both method kleisliMonadReader .. // and method kleisliIdMonadReader .. } Is this related to Scala's higher-order unification for type constructor inference ticket? If so (and even if not), could you describe what happens here in the a and b cases? Do

How to implement simple validation in Scala

徘徊边缘 提交于 2019-12-11 02:31:33
问题 Suppose I need to validate request parameters. The validation result is either Success or Failure with NonEmptyList[String] . I can probably use ValidationNel[String, Unit] but it seems a bit overkill. I guess I need a simpler abstraction (see below). trait ValidationResult object Success extends ValidationResult class Failure(errors: NonEmptyList[String]) extends ValidationResult and a binary operation andAlso to combine two results: trait ValidationResult { def andAlso(other: