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
13
votes
3 answers

Conditional invocation of a method in Scala

I've found this pattern quite a few times in my code: if (doIt) object.callAMethod else object I'm wondering if there could be a syntactically more pleasing way to write the code above, especially to avoid the repetition of the object…
Eric
  • 15,494
  • 38
  • 61
13
votes
2 answers

switch function and object with scalaz' |>

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" |>…
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
13
votes
1 answer

How will Dotty change pure functional programming in Scala?

In this question from 2013, Mr. Odersky notes that "it's too early to tell" whether libraries like Scalaz will be able to exist (at least in their current state) under Dotty, due to the castration of higher-kinded and existential types. In the time…
kantianethics
  • 671
  • 5
  • 21
13
votes
3 answers

What's the difference between a lens and a partial lens?

A "lens" and a "partial lens" seem rather similar in name and in concept. How do they differ? In what circumstances do I need to use one or the other? Tagging Scala and Haskell, but I'd welcome explanations related to any functional language that…
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
13
votes
4 answers

Scala Option object inside another Option object

I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class…
psisoyev
  • 2,118
  • 1
  • 25
  • 35
13
votes
1 answer

Round-up of Scalaz type class instances for other libraries

I often find myself wanting (and then usually writing) Scalaz type class instances for classes in other Scala or Java libraries. To give just a few examples: A monoid instance for Shapeless's HList gives you monoid instances for case classes with…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
12
votes
1 answer

Basic Scalaz State question

How do I use State to mimic the behaviour of List.zipWithIndex? What I have come up with so far (which doesn't work) is: def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match { case x :: xs => (init[Int] <* modify((_:Int) + 1))…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
12
votes
1 answer

Scala fast text file read and upload to memory

In Scala, for reading a text file and uploading it into an array, a common approach is scala.io.Source.fromFile("file.txt").getLines.toArray Especially for very large files, is there a faster approach perhaps by reading blocks of bytes into memory…
elm
  • 20,117
  • 14
  • 67
  • 113
12
votes
2 answers

Convert scala 2.10 future to scalaz.concurrent.Future // Task

did anybody come to piece of code how to properly convert scala's Future (2.10) to new scalaz7 future ? I know hot to convert scalaz future via scala Promise to scala Future, but not sure how to do it properly around For example import…
Pavel Chlupacek
  • 864
  • 5
  • 8
12
votes
1 answer

Distinction between type aliases and type lambdas

This question is about a limitation of Scala's implicit resolution system that I've run into a few times when using Scalaz and that doesn't make a lot of sense to me. I've distilled the problem to a Scalaz-less version below, but I'm happy to…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
12
votes
1 answer

Managing imports in Scalaz7

I am using scalaz7 in a project and sometimes I run into issues with imports. The simplest way get started is import scalaz._ import Scalaz._ but sometimes this can lead to conflicts. What I have been doing until now the following slightly painful…
Andrea
  • 20,253
  • 23
  • 114
  • 183
12
votes
1 answer

How to convert A[B[C]] to B[A[C]] if A and B are monads?

I'm looking for a more general solution which exploits monads (and monoids possibly) to achieve the same as if( xs.contains(None) ) None else Some(xs.flatten) does for xs of type Seq[Option[A]]. How can I do that with Scalaz? I feel like I'm…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
12
votes
2 answers

Update operations on a Scala Case Class

I have two instantiated case classes of the same type. case class Foo(x : Option[String], y : Option[String], z : Option[String]) Lets call the instantiated classes A and B. val a = Foo(x=Some("foo"), y=Some("bar"), z=Some("baz")) val b =…
Rasputin Jones
  • 1,427
  • 2
  • 16
  • 24
11
votes
2 answers

How to compose function to applicatives with scalaz

While learning Scalaz 6, I'm trying to write type-safe readers returning validations. Here are my new types: type ValidReader[S,X] = (S) => Validation[NonEmptyList[String],X] type MapReader[X] = ValidReader[Map[String,String],X] and I have two…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
11
votes
2 answers

What's the relation of fold on Option, Either etc and fold on Traversable?

Scalaz provides a method named fold for various ADTs such as Boolean, Option[_], Validation[_, _], Either[_, _] etc. This method basically takes functions corresponding to all possible cases for that given ADT. In other words, a pattern match shown…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365