scalaz

create Scalaz equal instance on class with subtypes

亡梦爱人 提交于 2019-12-23 13:19:12
问题 I have the following simple ADT, how would I implement an instance of the equality typeclass without resorting to explicitly pattern matching all possible combinations? import scalaz._ import Scalaz._ sealed trait Billinginfo case class CreditCard(number: Int, holder: String, Address: String) extends Billinginfo case object COD extends Billinginfo case class Invoice(cId: String) extends Billinginfo object Billinginfo{ implicit val BillingEqual = Equal.equal[Billinginfo]{(b1,b2) => (b1,b2)

How do I use Name as an applicative?

ぐ巨炮叔叔 提交于 2019-12-23 13:14:05
问题 scala> val a = Need(20) a: scalaz.Name[Int] = scalaz.Name$$anon$2@173f990 scala> val b = Need(3) b: scalaz.Name[Int] = scalaz.Name$$anon$2@35201f scala> for(a0 <- a; b0 <- b) yield a0 + b0 res90: scalaz.Name[Int] = scalaz.Name$$anon$2@16f7209 scala> (a |@| b) res91: scalaz.ApplicativeBuilder[scalaz.Name,Int,Int] = scalaz.ApplicativeBuilde r@11219ec scala> (a |@| b) { _ + _ } <console>:19: error: ambiguous implicit values: both method FunctorBindApply in class ApplyLow of type [Z[_]](implicit

Free ~> Trampoline : recursive program crashes with OutOfMemoryError

两盒软妹~` 提交于 2019-12-23 09:36:37
问题 Suppose that I'm trying to implement a very simple domain specific language with only one operation: printLine(line) Then I want to write a program that takes an integer n as input, prints something if n is divisible by 10k, and then calls itself with n + 1 , until n reaches some maximum value N . Omitting all syntactic noise caused by for-comprehensions, what I want is: @annotation.tailrec def p(n: Int): Unit = { if (n % 10000 == 0) printLine("line") if (n > N) () else p(n + 1) } Essentially

Provide implicits for all subtypes of sealed type

老子叫甜甜 提交于 2019-12-23 03:41:21
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type

Provide implicits for all subtypes of sealed type

蓝咒 提交于 2019-12-23 03:41:08
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type

switch function and object with scalaz' |>

邮差的信 提交于 2019-12-22 02:21:48
问题 I can use scalaz |> operator when I want to switch function and object so there can be a little more readability acquired. Let me introduce you a model function : def length2(x:String) = x.length * 2 Now, I can write it in both ways: "aoeu" |> length2 length2("aoeu") But if I define this function more generic, it stops working. def length2(x:SeqLike[ _ , _ ]) = x.length * 2 length2("aoeu") // ok "aoeu" |> length2 // doesn't work Why the compiler doesn't understand this? There is definitely an

Weird nested structural type in generics

爱⌒轻易说出口 提交于 2019-12-21 20:36:28
问题 Can someone explain weird construction of structural type nested in generics: implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] = new Functor[({type λ[α]=(R) => α})#λ] .... This example comes from Scalaz library: Functor.scala Why this construction is needed there? Wouldn't be simpler to write: implicit def Function1Functor[R,A]: Functor[R =>A] or implicit def Function1Functor[R,A]: Functor[Function1[R,A]] 回答1: The signature of the Functor type constructor shows that it is

Generic transform/fold/map over tuple/hlist containing some F[_]

℡╲_俬逩灬. 提交于 2019-12-21 05:27:07
问题 I recently asked Map and reduce/fold over HList of scalaz.Validation and got a great answer as to how to transform a fixed sized tuple of Va[T] (which is an alias for scalaz.Validation[String, T] ) into a scalaz.ValidationNel[String, T] . I've since then been studying Shapeless and type level programming in general to try to come up with a solution that works on tuples of any size. This is what I'm starting out with: import scalaz._, Scalaz._, shapeless._, contrib.scalaz._, syntax.std.tuple._

Convert a Seq[String] to a case class in a typesafe way

安稳与你 提交于 2019-12-21 04:54:18
问题 I have written a parser which transforms a String to a Seq[String] following some rules. This will be used in a library. I am trying to transform this Seq[String] to a case class. The case class would be provided by the user (so there is no way to guess what it will be). I have thought to shapeless library because it seems to implement the good features and it seems mature, but I have no idea to how to proceed. I have found this question with an interesting answer but I don't find how to

Scalaz Kleisli usage benefits

别来无恙 提交于 2019-12-21 04:34:15
问题 In scalaz Kleisli[M[_], A, B] is a wrapper of A => M[B] , which allows composition of such functions. For instance, if M[_] is monad I can compose Kleisli[M, A, B] and Kleisli[M, B, C] with >=> to get Kleisli[M, A, C] . In a nutshell, Kleisli provides fancy andThens depending on M . Is it correct ? Are there other benefits of using Kleisli ? 回答1: Here are two benefits as examples—I'm sure you could come up with others. First, it can be useful to abstract over different arrows, such as Kleisli