scalaz

Shapeless: generic lens parameterized by case class or field

倖福魔咒の 提交于 2019-12-18 04:08:23
问题 Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to do? If not, the end result I'm trying to achieve is a generic method for updating nested Maps with case-class contents, e.g.: import scalaz._ import Scalaz._ import PLens._ import shapeless._ import shapeless.contrib.scalaz._ def nestedMapLens[R, T <:

Doing HTTP request in Scala

风流意气都作罢 提交于 2019-12-17 21:42:44
问题 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

Apply several string transformations in scala

↘锁芯ラ 提交于 2019-12-17 19:48:10
问题 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

Using context bounds “negatively” to ensure type class instance is absent from scope

落爺英雄遲暮 提交于 2019-12-17 09:35:20
问题 tl;dr : How do I do something like the made up code below: def notFunctor[M[_] : Not[Functor]](m: M[_]) = s"$m is not a functor" The ' Not[Functor] ', being the made up part here. I want it to succeed when the 'm' provided is not a Functor, and fail the compiler otherwise. Solved : skip the rest of the question and go right ahead to the answer below. What I'm trying to accomplish is, roughly speaking, "negative evidence". Pseudo code would look something like so: // type class for obtaining

Dependency injection into scala objects (not classes)

牧云@^-^@ 提交于 2019-12-14 03:26:57
问题 I have an import "import play.api.libs.ws.WSClient" which i want to use within my object Object X { ... } But this doesn't seem to be available inside my object. I see that dependency injection is only available for classes. How do i get this to work? 回答1: Injecting a dependency into an object is impossible. You have two options: Ugly and deprecated: Access the injector via the global application: val wsClient = Play.current.injector.instanceOf[WSClient] Way to go if your code needs to live

Merge maps in scalaz with a complex (double) operation

爱⌒轻易说出口 提交于 2019-12-13 18:43:27
问题 I am using a map to associate certain values with a tuple (Int, Double) where the int is the order they appeared and the double the number of times they show (it is not, but is clearer like this using int and double to distinguish) The tricky part is that I want to use different monoids for each element of the tuple, for the int I want to keep the min value, to remember first appearance, while for the double I want to use the addition monoid So for an existing key we have: val map1 = Map("a"

Reader monad in Scala: return, local, and sequence

a 夏天 提交于 2019-12-13 13:27:03
问题 I'm using the Reader monad in Scala as provided by the scalaz library. I'm familiar with this monad as defined in Haskell. The problem is that I cannot find the functions equivalent to return , local , and sequence (among others). Currently I use constructs that I do not like since I'm repeating myself or making my code a bit obscure. Regarding return , I'm currently using: Reader{_ => someValue} I'd rather just use a construct like unit(someValue) , but I could not find anything on the

Kleisli[Future, Context, \/] to Kleisli[EitherT, Context, …]

☆樱花仙子☆ 提交于 2019-12-13 01:22:10
问题 As I want to combine Kleisli that works on long methods Future that can fail Either , I need to stack the effect. Here is the resulting code to stack the effect in the Kleisli. Is there an existing combinator in scalaz ? type FutureEitherT[A] = EitherT[Future, String, A] def toKleisliEitherTFromDisjunction[A](f: Kleisli[Future, Context,String \/ A]) = Kleisli[FutureEitherT, Context, A] { ctx => EitherT(f(ctx)) } I've tried without success f.liftMK[FutureEitherT] , but unfortunately, the third

Example of Applicative composition in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-12 12:33:05
问题 This is a followup to my old questions: I know that monads are not composable, i.e. if M1[_] and M2[_] are monads M2[M1[_]] is not necessarily a monad. For instance, List[Int] and Option[Int] are monads but Option[List[Int]] is not automatically a monad and therefore I need a monad transformer to use it as a monad (as in here) I know that applicative functors are composable. I guess it means that if A1[_] and A2[_] are applicatives then A2[A1[_]] is always an applicative. Is it correct ?

Lifting a function which takes implicit parameter using functor (Scalaz7)

时光毁灭记忆、已成空白 提交于 2019-12-12 03:47:34
问题 Just started learning Scalaz. Here is my code trait Monoid[A] { def mappend(a1: A, a2: A): A def mzero: A } object Monoid { implicit val IntMonoid: Monoid[Int] = new Monoid[Int] { def mappend(a1: Int, a2: Int): Int = a1 + a2 def mzero: Int = 0 } implicit val StringMonoid: Monoid[String] = new Monoid[String] { def mappend(a1: String, a2: String): String = a1 + a2 def mzero: String = "" } } trait MonoidOp[A] { val F: Monoid[A] val value: A def |+|(a2: A): A = F.mappend(value, a2) } object