scalaz

Replace elements in List by condition

假装没事ソ 提交于 2019-12-08 12:29:02
问题 I have a pretty much large val s: List[Int] = //... , a function f: Int => Boolean and a function transform: Int => Int . The problem : I want to create another List[Int] such that all elements e: Int of the s: List[Int] such that f(e) = true are replaced with transform(e) . I looked at cats-mtl FunctorEmpty (to adhere to functional programming style), but it does not seem to work in my case. Maybe some cats / scalaz data structures can be useful here? Or any other way? 回答1: s.map{ e => if(f

Scalaz ValidationNel is not working

与世无争的帅哥 提交于 2019-12-08 11:04:34
问题 I tried to use Scalaz(7.2.18) to validate data. I tried following code: def hasDob: ValidationNel[AdtError, String] = enc.dob.map(_.success).getOrElse(MissingAdmitDate(enc).failureNel) def hasAdmitDt: ValidationNel[AdtError, Timestamp] = enc.admitDT.map(_.success).getOrElse( MissingAdmitDate(enc).failureNel ) def hasTimezone: Validation[AdtError, DateTimeZone] = fac.timezone.map(_.success).getOrElse( UndefinedTimezone(fac).failureNel ) (hasDob |@| hasAdmitDt |@| hasTimezone) { (dob, admitTime

Scala partially applied type constructor inference

巧了我就是萌 提交于 2019-12-08 10:19:35
问题 I'm using scala-2.8.1 and scalaz-5.0 . Can anyone explain exactly why a PartialApply1Of2 can be inferrred in the one case but not in the other? scala> 1.success[String] <|*|> "Bah".fail[Int] res1: scalaz.Validation[String,(Int, Int)] = Failure(Bah) That worked even though (as has been asked before!) the method <|*|> is on MA which has one type parameter, not two (as Validation has). I cannot get unicode working in my IDEA REPL, so here goes: object testy { def main(args: Array[String]) {

Clean up signatures with long implicit parameter lists

天大地大妈咪最大 提交于 2019-12-07 12:14:40
问题 Is there an elegant solution to somehow clean up implicit parameter lists making signatures more concise? I have code like this: import shapeless._ import shapeless.HList._ import shapeless.ops.hlist._ import shapeless.poly._ trait T[I, O] extends (I => O) trait Validator[P] object Validator{ def apply[P] = new Validator[P]{} } object valid extends Poly1 { implicit def caseFunction[In, Out] = at[T[In, Out]](f => Validator[In]) } object isValid extends Poly2 { implicit def caseFolder[Last, New

How to use lift from ToFunctorOps

落爺英雄遲暮 提交于 2019-12-07 11:13:50
问题 ToFunctorOps defines a lift method via the ToLiftV implicit, but I can't seem to make it find my functor instances: import scalaz.std.option._ import scalaz.syntax.functor._ import scalaz.syntax.id._ import scalaz.syntax.std.option._ def inc(x: Int) = x + 1 1.some |> (inc _).lift <console>:16: error: could not find implicit value for parameter F: scalaz.Functor[F] 1.some |> (inc _).lift The functor instance for option is visible but the compiler can't seem to find it. Any suggestions as to

Handling fail-fast failures when the return type is Option[Error]

試著忘記壹切 提交于 2019-12-07 07:37:57
问题 I have posted quite several questions about failure handling in Scala and I really thank you all for your answers. I understand my options when dealing with Either and Scalaz or a for comprehension, and I have another (last?) question: How to do a fail-fast sequence of operations when the operations are dealing with the outside non-functional world, like a DB? I mean I have a method like that: def insertItem(item: Item): Either[Error,Item] Thanks to Either and these answers, I know how to do

Composing validating functions in Scala

此生再无相见时 提交于 2019-12-07 07:10:10
问题 Suppose I need to write a validating function Validate[A] : type Status[A] = Validation[List[String], A] type Validate[A] = A => Status[A] // should be Kleisli The function returns either a Success with the input if the input is valid or a Failure with the list of errors if it is not. For example, val isPositive: Validate[Int] = {x: Int => if (x > 0) x.success else List(s"$x is not positive").failure } val isEven: Validate[Int] = {x: Int => if (x % 2 == 0) x.success else List(s"$x is not even

scalaz validation and list monad

白昼怎懂夜的黑 提交于 2019-12-07 03:58:47
问题 I am trying to come up with something similar to the following: val s: Validation[String, Int] = 1.success def s2(i: Int): Validation[String, Int] = i.success val result = for { i <- s j <- List(1, 2) k <- s2(j) } yield "fine"; The above code does not compile and I understand, syntactically it does not make sense. I am trying to execute a list of validations in a monadic way. How do I achieve that? 回答1: If you have a list of validations of A , you can turn it into a validation of lists of A

Asynchronous iteratee processing in Scalaz

左心房为你撑大大i 提交于 2019-12-07 03:22:35
问题 I've been using Scalaz 7 iteratees to process a large (i.e., unbounded) stream of data in constant heap space. In code, it looks something like this: type ErrorOrT[M[+_], A] = EitherT[M, Throwable, A] type ErrorOr[A] = ErrorOrT[IO, A] def processChunk(c: Chunk): Result def process(data: EnumeratorT[Chunk, ErrorOr]): IterateeT[Chunk, ErrorOr, List[Result]] = Iteratee.fold[Chunk, ErrorOr, List[Result]](Nil) { (rs, c) => processChunk(c) :: rs } &= data Now I'd like to perform the processing in

How to use >=> in Scala?

喜欢而已 提交于 2019-12-06 21:47:57
问题 I am trying to use >=> (Kleisli arrow) in Scala. As I understand, it composes functions returning monads. Now I am trying it as follows: scala> val f = {i:Int => Some(i + 1)} f: Int => Some[Int] = <function1> scala> val g = {i:Int => Some(i.toString)} g: Int => Some[String] = <function1> scala> val h = f >=> g <console>:15: error: value >=> is not a member of Int => Some[Int] val h = f >=> g ^ Why does not it compile ? How to compose f and g with >=> ? 回答1: There are two problems here. The