scalaz

Functional equivalent of if (p(f(a), f(b)) a else b

I'm guessing that there must be a better functional way of expressing the following: def foo(i: Any) : Int if (foo(a) < foo(b)) a else b So in this example f == foo and p == _ < _. There's bound to be some masterful cleverness in scalaz for this! I can see that using BooleanW I can write: p(f(a), f(b)).option(a).getOrElse(b) But ...

Function syntax puzzler in scalaz

Following watching Nick Partidge's presentation on deriving scalaz, I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failu...

Scalaz Kleisli question

There is a trait called Kleisli in the scalaz library. Looking at the code: import scalaz._ import Scalaz._ type StringPair = (String, String) val f: Int => List[String] = (i: Int) => List((i |+| 1).toString, (i |+| 2).toString) val g: String => List[StringPair] = (s: String) => List("X" -> s, s -> "Y") val k = kleisli(f) >=> k...

Scalaz: request for use case for Cokleisli composition

This question isn't meant as flame-bait! As it might be apparent, I've been looking at Scalaz recently. I'm trying to understand why I need some of the functionality that the library provides. Here's something: import scalaz._ import Scalaz._ type NEL[A] = NonEmptyList[A] val NEL = NonEmptyList I put some println statements in my func...

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

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 Somes into a Some containing a list of the elements inside the elements Some's. On the other hand, if the input list has at lea...

How do I get the scalaz IDEA live templates working for the symbolic methods?

Many of the methods in scalaz have symbolic unicode equivalents, such as forever and ∞ (of course, I have this the wrong way round, the symbolic methods really have ASCII equivalents). The project contains a live templates XML file for IDEA so these can be auto-completed, I believe by using the forever+TAB shortcut (in the above instanc...

Compilation issues using scalaz's MA methods on Set but not List

The following compiles just fine using scala Beta1 and scalaz snapshot 5.0: val p1: Int => Boolean = (i : Int) => i > 4 val s: List[Int] = List(1, 2, 3) val b1 = s ∃ p1 And yet this does not: val s: Set[Int] = Set(1, 2, 3) val b1 = s ∃ p1 I get the following error: Found: Int => Boolean Required: Boolean => Boolean The sig...

Can I transform this asynchronous java network API into a monadic representation (or something else idiomatic)?

I've been given a java api for connecting to and communicating over a proprietary bus using a callback based style. I'm currently implementing a proof-of-concept application in scala, and I'm trying to work out how I might produce a slightly more idiomatic scala interface. A typical (simplified) application might look something like thi...

How to combine Option values in Scala?

Hi! I want to be able to apply an operation f: (T,T) => T to Option[T] values in Scala. I want the result to be None if any of the two values is None. More specifically, I want to know if is there a shorter way to do the following: def opt_apply[T](f: (T,T) => V, x: Option[T], y: Option[T]): Option[T] = { (x,y) match { case (Som...

Implicit parameter in Scalaz

I try to find out why the call ∅ in scalaz.ListW.<^> works def <^>[B: Zero](f: NonEmptyList[A] => B): B = value match { case Nil => ∅ case h :: t => f(Scalaz.nel(h, t)) } My minimal theory is: trait X[T]{ def y : T } object X{ implicit object IntX extends X[Int]{ def y = 42 } implicit object StringX extends X[Strin...

Scalaz splitting a computation into sub-parts

I have a very large List[A] and a function f: List[A] => List[B]. I would like to split my original list into sub-lists with a maximum size, apply the function to each sublist in turn and then unsplit the result into one big List[B]. This pretty easy: def split[T](l : List[T], max : Int) : List[List[T]] = //TODO def unsplit[T](l : List...

Different Scala Actor Implementations Overview

I'm trying to find the 'right' actor implementation for my thesis. I realized there is a bunch of them and it's a bit confusing to pick one. Personally I'm especially interested in remote actors, but I guess a complete overview would be helpful to many others. This is a pretty general question, so feel free to answer just for the impleme...

Reader Monad with Scalaz

I try to define the Reader monad with scalaz like this: import scalaz._ import Scalaz._ final class Reader[E,A](private[Reader] val runReader: E => A) object Reader { def apply[E,A](f: E => A) = new Reader[E,A](f) def env[E]: Reader[E,E] = Reader(identity _) implicit def ReaderMonad[E] = new Monad[PartialApply1Of2[Reader,E]#App...

Type inference question using Scalaz.ListW.<^>

I was playing around with ListW.<^>, the definition of which is as follows: def <^>[B: Zero](f: NonEmptyList[A] => B): B = value match { case Nil => ∅ case h :: t => f(Scalaz.nel(h, t)) } I cannot figure out how come Option is being chosen as the Zero type for this example scala> case class CC(v : Int) defined class CC scala> va...

What is a DList?

I tried googling for this but all I got were stories about minor celebrities. Given the lack of documentation, what is a DList? ...

How do I get (a, b) => c from a => b => c in Scala?

If I have: val f : A => B => C This is shorthand for: val f : Function1[A, Function1[B, C]] How do I get a function g with the signature: val g : (A, B) => C = error("todo") (i.e.) val g : Function2[A, B, C] //or possibly val g : Function1[(A, B), C] in terms of f? ...

New to 2.8 collections. What would this signature look like? Similar to scalaz sequence.

I found a blog post today that mention's scalaz's sequence function. Couldn't you do something as simple as: if (l contains None) None else l If so, what would this function signature look like? contains is in SeqLike, right? Also, from the blog post I thought sequence was going to be something similar to map, but one that would br...