Questions tagged [scalaz]

Scalaz provides type classes and purely functional data structures for Scala

Scalaz is a Scala library for functional programming.

It provides purely functional data structures to complement those from the Scala standard library. It defines a set of foundational type classes (e.g. Functor, Applicative, Monad) and corresponding instances for a large number of data structures.

More information is available on the Scalaz Project page

It is released under the BSD (open source) license.

1048 questions
18
votes
1 answer

Where is `sequence` in Scalaz7

I am learning Scalaz and I have a project that already makes use of Scalaz7. Following this question I would like to use the function sequence[T](l: List[Option[T]]): Option[List[T]] (not that it is hard to write it myself). But the aforementioned…
Andrea
  • 20,253
  • 23
  • 114
  • 183
18
votes
1 answer

reader writer state monad - how to run this scala code

Tony Morris gave a talk with this snippet. He's using ReaderWriterState monad to provide controlled read/write access to an implicit context. That makes sense. How do I use the code? I would like to see an example "main" program that uses this…
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
17
votes
2 answers

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…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
16
votes
2 answers

Using scalaz state in a more complicated computation

I'm trying to understand how to use scalaz State to perform a complicated stateful computation. Here is the problem: Given a List[Int] of potential divisors and a List[Int] of numbers, find a List[(Int, Int)] of matching pairs (divisor, number)…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
16
votes
1 answer

Will using Scala in a more functional way (scalaz) incur a performance/maintainability penalty?

I'm currently working on a small project (< 10k loc) which is mainly pure but relies on mutable optimizations mainly based on iterators and some data-structure reuse for heavy-duty calculations. I'd like to learn a bit more functional programming…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
15
votes
1 answer

Monad transformer for NonEmptyList?

It seems to me that Scalaz' NonEmptyList has a monad instance, so a monad transformer for it (a bit similar to ListT) should be possible. Is that correct? If so, is there one out there? (I couldn't find one in Scalaz 7 itself.) If not, i.e. a monad…
user500592
14
votes
5 answers

Compose Scalaz validations

I would like to use Scalaz for validations and like to be able to reuse the validation functions in different contexts. I'm totally new to Scalaz btw. Let's say I have these simple checks: def checkDefined(xs: Option[String]): Validation[String,…
chrsan
  • 3,390
  • 3
  • 19
  • 14
14
votes
3 answers

Scala PartialFunction can be Monoid?

I thought PartialFunction can be Monoid. Is my thought process correct ? For example, import scalaz._ import scala.{PartialFunction => -->} implicit def partialFunctionSemigroup[A,B]:Semigroup[A-->B] = new Semigroup[A-->B]{ def append(s1: A-->B,…
14
votes
1 answer

How can I use Kleisli composition with functions returning Validations?

How do I compose two functions returning Validations? Following are my attempts that didn't work: scala> def f: Int => Validation[String, Int] = i => if(i % 2 == 0) Success(i * 2) else Failure("Odd!") f: Int => scalaz.Validation[String,Int] scala>…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
14
votes
1 answer

scalaz List[StateT].sequence - could not find implicit value for parameter n: scalaz.Applicative

I'm trying to figure out how to use StateT to combine two State state transformers based on a comment on my Scalaz state monad examples answer. It seems I'm very close but I got an issue when trying to apply sequence. import scalaz._ import…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
14
votes
1 answer

My API is all returning Future[Option[T]], how to combine them nicely in a for-compr

All of my API methods return Future[Option[T]], trying to figure out how to elegantly perform the following: case class UserProfile(user: User, location: Location, addresses: Address) The below code currently doesn't compile because user, location,…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
14
votes
2 answers

Scalaz validation: convert sequence of validations to a single validation

I am using scalaz validation, and have some code to validate products. def validateProduct(product: Option[Product]): ValidationNel[String, Product] = ??? Given a list of products, I want to get a single validation containing the whole list as a…
triggerNZ
  • 4,221
  • 3
  • 28
  • 34
14
votes
6 answers

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!…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
14
votes
2 answers

Iteratees in Scala that use lazy evaluation or fusion?

I have heard that iteratees are lazy, but how lazy exactly are they? Alternatively, can iteratees be fused with a postprocessing function, so that an intermediate data structure does not have to be built? Can I in my iteratee for example build a 1…
Robin Green
  • 32,079
  • 16
  • 104
  • 187
14
votes
2 answers

Avoiding repetition using lenses whilst deep-copying into Map values

I have an immutable data structure where I have nested values in Maps, like so: case class TradingDay(syms: Map[String, SymDay] = Map.empty) case class SymDay(sym: String, traders: Map[String, TraderSymDay] = Map.empty) case class…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449