Giter VIP home page Giter VIP logo

scalaz's Introduction

Scalaz

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, Monad) and corresponding instances for a large number of data structures.

IRC

Getting Scalaz

The current stable version is 7.3.8, which is cross-built against Scala 2.12.x, 2.13.x, 3.x and Scala.js, scala-native.

If you're using SBT, add the following line to your build file:

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.3.8"

For Maven and other build tools, you can visit search.maven.org. (This search will also list all available modules of scalaz.)

To get sample configurations, click on the version of the module you are interested in. You can also find direct download links at the bottom of that page. Choose the file ending in 7.3.8.jar.

Quick Start

import scalaz._
import std.option._, std.list._ // functions and type class instances for Option and List

scala> Apply[Option].apply2(some(1), some(2))((a, b) => a + b)
res0: Option[Int] = Some(3)

scala> Traverse[List].traverse(List(1, 2, 3))(i => some(i))
res1: Option[List[Int]] = Some(List(1, 2, 3))

Use of the Ops classes, defined under scalaz.syntax.

import scalaz._
import std.list._ // type class instances for List
import syntax.bind._ // syntax for the Bind type class (and its parents)

scala> List(List(1)).join
res0: List[Int] = List(1)

scala> List(true, false).ifM(List(0, 1), List(2, 3))
res1: List[Int] = List(0, 1, 2, 3)

We've gone to great lengths to give you an a-la-carte importing experience, but if you prefer an all-you-can-eat buffet, you're in luck:

import scalaz._
import Scalaz._

scala> NonEmptyList(1, 2, 3).cojoin
res0: scalaz.NonEmptyList[scalaz.NonEmptyList[Int]] = NonEmptyList(NonEmptyList(1, 2, 3), NonEmptyList(2, 3), NonEmptyList(3))

scala> 1.node(2.leaf, 3.node(4.leaf))
res1: scalaz.Tree[Int] = <tree>

scala> List(some(1), none).suml
res2: Option[Int] = Some(1)

Resources

Let the types speak for themselves via the Scalaz Scaladocs!

The examples module contains some snippets of Scalaz usage.

The wiki contains release and migration information.

Talk with us by joining IRC: irc.libera.chat channel #scalaz, or join the Scalaz mailing list on Google Groups.

The typelevel blog has some great posts such as Towards Scalaz by Adelbert Chang.

Learning Scalaz is a great series of blog posts by Eugene Yokota. Thanks, Eugene!

Changes in Version 7

Scalaz 7 represents a major reorganization of the library. We have taken a fresh look at the challenges of encoding type classes in Scala, in particular at when and how to employ the implicit scope.

At a glance

  • scalaz.{effect, iteratee} split to separate sub-projects; scalaz.{http, geo} dropped.
  • Refined and expanded the type class hierarchy.
  • Type class instances are no longer defined in the companion objects of the type class. Instances for standard library types are defined under scalaz.std, and instances for Scalaz data types are defined in the companion object for those types. An instance definition can provide multiple type classes in a single place, which was not always possible in Scalaz 6.
  • Type class instances have been organized to avoid ambiguity, a problem that arises when instances are dependent on other instances (for example, Monoid[(A, B)])
  • Use of implicit views to provide access to Scalaz functionality as extension methods has been segregated to scalaz.syntax, and can be imported selectively, and need not be used at all.
  • Related functions are defined in the type class trait, to support standalone usage of the type class. In Scalaz 6, these were defined in Identity, MA, or MAB.
  • New data structures have been added, and existing ones generalized. A number of monad transformers have been provided, in some cases generalizing old data structures.

Modularity

Scalaz has been modularised.

  • scalaz-core: Type class hierarchy, data structures, type class instances for the Scala and Java standard libraries, implicit conversions / syntax to access these.
  • scalaz-effect: Data structures to represent and compose IO effects in the type system.
  • scalaz-iteratee: Experimental new Iteratee implementation

Type Class Hierarchy

  • Type classes form an inheritance hierarchy, as in Scalaz 6. This is convenient both at the call site and at the type class instance definition. At the call site, it ensures that you can call a method requiring a more general type class with an instance of a more specific type class:
def bar[M[_]: Functor] = ()

def foo[M[_]: Monad] = bar[M] // Monad[M] is a subtype of Functor[M]
  • The hierarchy itself is largely the same as in Scalaz 6. However, there have been a few adjustments, some method signatures have been adjusted to support better standalone usage, so code depending on these will need to be re-worked.

Type Class Instance Definition

  • Constructive implicits, which create a type class instance automatically based on instances of all parent type classes, are removed. These led to subtle errors with ambiguous implicits, such as this problem with FunctorBindApply
  • Type class instances are no longer declared in fragments in the companion objects of the type class. Instead, they are defined in the package scalaz.std, and must be imported. These instances are defined in traits which will be mixed together into an object for importing en-masse, if desired.
  • A single implicit can define a number of type class instances for a type.
  • A type class definition can override methods (including derived methods) for efficiency.

Here is an instance definition for Option. Notice that the method map has been overridden.

  implicit val option: Traverse[Option] with MonadPlus[Option] = new Traverse[Option] with MonadPlus[Option] {
    def point[A](a: => A) = Some(a)
    def bind[A, B](fa: Option[A])(f: A => Option[B]): Option[B] = fa flatMap f
    override def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f
    def traverseImpl[F[_], A, B](fa: Option[A])(f: A => F[B])(implicit F: Applicative[F]) =
      fa map (a => F.map(f(a))(Some(_): Option[B])) getOrElse F.point(None)
    def empty[A]: Option[A] = None
    def plus[A](a: Option[A], b: => Option[A]) = a orElse b
    def foldR[A, B](fa: Option[A], z: B)(f: (A) => (=> B) => B): B = fa match {
      case Some(a) => f(a)(z)
      case None => z
    }
  }

To use this, one would:

import scalaz.std.option.optionInstance
// or, importing all instances en-masse
// import scalaz.Scalaz._

val M = Monad[Option]
val oi: Option[Int] = M.point(0)

Syntax

We co-opt the term syntax to refer to the way we allow the functionality of Scalaz to be called in the object.method(args) form, which can be easier to read, and, given that type inference in Scala flows from left-to-right, can require fewer type annotations.

  • No more Identity, MA, or MAB from Scalaz 6.
  • Syntax is segregated from rest of the library, in a sub-package scalaz.syntax.
  • All Scalaz functionality is available without using the provided syntax, by directly calling methods on the type class or its companion object.
  • Syntax is available a-la-carte. You can import the syntax for working with particular type classes where you need it. This avoids flooding the autocompletion in your IDE with every possible extension method. This should also help compiler performance, by reducing the implicit search space.
  • Syntax is layered in the same way as type classes. Importing the syntax for, say, Applicative will also provide the syntax for Apply and Functor.

Syntax can be imported in two ways. Firstly, the syntax specialized for a particular instance of a type class can be imported directly from the instance itself.

// import the type class instance
import scalaz.std.option.optionInstance

// import the implicit conversions to `MonadOps[Option, A]`, `BindOps[Option, A]`, ...
import optionInstance.monadSyntax._

val oi: Option[Option[Int]] = Some(Some(1))

// Expands to: `ToBindOps(io).join`
oi.join

Alternatively, the syntax can be imported for a particular type class.

// import the type class instance
import scalaz.std.option.optionInstance

// import the implicit conversions to `MonadOps[F, A]`, `BindOps[F, A]`, ...
import scalaz.syntax.monad._

val oi: Option[Option[Int]] = Some(Some(1))

// Expands to: ToBindOps(io).join
oi.join

For some degree of backwards compatibility with Scalaz 6, the über-import of import scalaz.Scalaz._ will import all implicit conversions that provide syntax (as well as type class instances and other functions). However, we recommend to review usage of this and replace with more focussed imports.

Standalone Type Class Usage

Type classes should be directly usable, without first needing to trigger implicit conversions. This might be desirable to reduce the runtime or cognitive overhead of the pimped types, or to define your own pimped types with a syntax of your choosing.

  • The methods in type classes have been curried to maximize type inference.
  • Derived methods, based on the abstract methods in a type class, are defined in the type class itself.
  • Each type class companion object is fitted with a convenient apply method to obtain an instance of the type class.
    // Equivalent to `implicitly[Monad[Option]]`
    val O = Monad[Option]

    // `bind` is defined with two parameter sections, so that the type of `x` is inferred as `Int`.
    O.bind(Some(1))(x => Some(x * 2))

    def plus(a: Int, b: Int) = a + b

    // `Apply#lift2` is a function derived from `Apply#ap`.
    val plusOpt = O.lift2(plus)

Type Class Instance Dependencies

Type class instances may depend on other instances. In simple cases, this is as straightforward as adding an implicit parameter (or, equivalently, a context bound), to the implicit method.

  implicit def optionMonoid[A: Semigroup]: Monoid[Option[A]] = new Monoid[Option[A]] {
    def append(f1: Option[A], f2: => Option[A]): Option[A] = (f1, f2) match {
      case (Some(a1), Some(a2)) => Some(Semigroup[A].append(a1, a2))
      case (Some(a1), None) => f1
      case (None, Some(a2)) => f2
      case (None, None) => None
    }

    def zero: Option[A] = None
  }

Type class instances for 'transformers', such as OptionT, present a more subtle challenge. OptionT[F, A] is a wrapper for a value of type F[Option[A]]. It allows us to write:

val ot = OptionT(List(Some(1), None))
ot.map((a: Int) => a * 2) // OptionT(List(Some(2), None))

The method OptionT#map requires an implicit parameter of type Functor[F], whereas OptionT#flatMap requires one of type Monad[F]. The capabilities of OptionT increase with those of F. We need to encode this into the type class instances for [a]OptionT[F[A]].

This is done with a hierarchy of type class implementation traits and a corresponding set of prioritized implicit methods.

In case of ambiguous implicits, Scala will favour one defined in a sub-class of the other. This is to avoid ambiguity when in cases like the following:

type OptionTList[A] = OptionT[List[A]]
implicitly[Functor[OptionTList]]

// Candidates:
// 1. OptionT.OptionTFunctor[List](implicitly[Functor[List]])
// 2. OptionT.OptionTMonad[List](implicitly[Functor[List]])
// #2 is defined in a subclass of the enclosing class of #1, so #2 is preferred.

Transformers and Identity

A stronger emphasis has been placed on transformer data structures (aka Monad Transformers). For example State is now a type alias for StateT[Id, A, B].

Id is defined in the scalaz package object as:

type Id[A] = A

Contributing

Documentation for contributors

Credits

Support for Scalaz development is provided by Jetbrains.

Thanks to Mark Harrah and the sbt contributors for providing our build tool.

scalaz's People

Contributors

adelbertc avatar alexeyr avatar arjanblokzijl avatar atry avatar bmjames avatar ceedubs avatar dependabot[bot] avatar ekmett avatar enzief avatar fommil avatar gseitz avatar jbgi avatar justjoheinz avatar larsrh avatar lemastero avatar monkey-mas avatar mpilquist avatar pchiusano avatar puffnfresh avatar purefn avatar retronym avatar runarorama avatar s11001001 avatar scalaz-bot[bot] avatar stew avatar tomasmikula avatar tonymorris avatar tpolecat avatar vmarquez avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scalaz's Issues

TreeLoc.findChild doesn't find leaf nodes

scala> val t1 = node(1, Stream(leaf(2), node(3, Stream(leaf(4), leaf(5)))))
t1: scalaz.Tree[Int] =

scala> t1.flatten.tail.map(x => t1.loc.findChild(_.rootLabel == x)).force
res6: scala.collection.immutable.Stream[Option[scalaz.TreeLoc[Int]]] = Stream(Some(), Some(), None, None)

res6 should contain only Somes. And maybe remarking, that findChild will NOT find the root node would be nice. I expected it to also find the root.

There is no MA[Traversable, A]

I was not expecting this to fail:

scala> (List(1, 2, 3): Traversable[Int]).asMA
:14: error: type mismatch;
found : Traversable[Int]
required: ?{val asMA: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method maImplicit in trait MAsLow of type [M[], A](a: M[A])scalaz.MA[M,A]
and method mab in trait MABLow of type [M[
,_], A, B](a: M[A,B])scalaz.MAB[M,A,B]
are possible conversion functions from Traversable[Int] to ?{val asMA: ?}
(List(1, 2, 3): Traversable[Int]).asMA
^

frq: Documentation explaining correct use of monad transformer stacks.

It would be useful to have an example of correct use of a monad transformer stack.

My specific use case was an attempt to create a library that exposed an 'application' style monad stack.
In haskell the stack would be:
newtype FBukkit s c a = FB { runFB :: StateT s (ReaderT c IO) a } deriving (Monad, MonadIO, MonadReader c, MonadState s)

The intent is to expose to client code a single monad template that could be used by specifying s and c (concrete state and configuration types) and allow access to the functionality them via read, local, get, put, modify, and liftIO.
Execution of functions in this monad would be via runFB, execFB, and evalFB.

I got as far as determining that I need to make use of StateT, Kleisli, and IO, but I wasn't able to puzzle out the syntax to actually build the stack I wanted while providing those functions to client code.

StackOverflowError when traversing a Stream

This code sample causes a SOE

Stream.from(1).take(10000).traverse(x => putStrLn(x.toString)).unsafePerformIO

Here is a stacktrace snippet:

at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.TraversableOnce$class.addString(TraversableOnce.scala:285)
at scala.collection.immutable.List.addString(List.scala:45)
at scala.collection.TraversableOnce$class.mkString(TraversableOnce.scala:263)
at scala.collection.immutable.List.mkString(List.scala:45)
at scala.collection.TraversableOnce$class.mkString(TraversableOnce.scala:265)
at scala.collection.immutable.List.mkString(List.scala:45)
at scala.collection.TraversableOnce$class.mkString(TraversableOnce.scala:267)
at scala.collection.immutable.List.mkString(List.scala:45)
at scalaz.Identity$class.shows(Identity.scala:50)
at scalaz.Identity$$anon$1.shows(Identity.scala:169)
at filemanip.Gis$$anonfun$main$1.apply(Gis.scala:29)
at filemanip.Gis$$anonfun$main$1.apply(Gis.scala:29)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
at scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
at scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
at scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)

Regards

Overridable multiply and multiply for Semigroup and Group

This issue is taken from the discussion in the mailing list: https://groups.google.com/forum/?fromgroups#!topic/scalaz/3yI9rJ2Srhw

Basically, the request is to have an overridable multiply for Semigroup, Monoid, and Group. This allows alternative implementation of multiply (e.g by directly doing integer multiplication).

Another side of the request is to have default implementation for Semigroup and Group. For Semigroup, it should accept positive multiplier only, while for Group it should accept also negative multiplier.

The return value of multiply can be an Option[A], this will give the signature: def multiply(value: F, n: Int): Option[F]

NonEmptyList lacks equals and hashCode methods.

Was caught by this when running some unit tests, means that in ScalaTest the following expression fails:
NonEmptyList(1) must equal(NonEmptyList(1))

Would be useful to have both as otherwise NonEmptyList can't be used in Sets or as the key in a Map.

Length instance for NonEmptyList

I can't find a Length instance for NonEmptyList. Something like:

new Length[NonEmptyList] {
def length[A](fa: NonEmptyList[A]) = fa.list.length + 1
}

Equal for Map and Set are wrong

scala> Set(1,2,3) == Set(3,2,1)
res16: Boolean = true

scala> Set(1,2,3) === Set(3,2,1)
res17: Boolean = false

Same for Map

HList

Add Heterogeneous lists in scalaz.hlist

ListW#groupByM incorrect

 scala> val is = List(1, 2, 2, 3, 3, 4, 4, 5)
is: List[Int] = List(1, 2, 2, 3, 3, 4, 4, 5)
scala> is.groupByM[Identity]((a, b) => a == b)
res10: scalaz.Identity[List[List[Int]]] = List(List(1, 1), List(2, 2, 2), List(3, 3, 3), List(4, 4, 4), List(5, 5))

property:
(is: List[A]) => is.groupByM((a, b) => a == b)).join === is

parMap on a large List in 6.0.4 fails with a StackOverflowError.

scala> val list = (1 to 1000000).toList
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
scala> list.parMap(value => List(value, value * 2, value % 4)).get
java.lang.StackOverflowError
    at scalaz.Identity$$anon$1.value(Identity.scala:170)
    at scalaz.Functor$$anon$4$$anonfun$fmap$1.apply(Functor.scala:24)
    at scalaz.Identity$$anon$1.value(Identity.scala:170)
    at scalaz.Functor$$anon$4$$anonfun$fmap$1.apply(Functor.scala:24)
    at scalaz.Identity$$anon$1.value(Identity.scala:170)
    at scalaz.Functor$$anon$4$$anonfun$fmap$1.apply(Functor.scala:24)

Verification

Create Verification trait or add mechanizm in existing Validation, so there will be feasible validation chanins on some expression resulting in Validation. See test specs:

import scalaz._
import Scalaz._
import scalaz.syntax._
import org.scalatest.FreeSpec

class VerificationSpec extends FreeSpec {
  type S = String

  "Expresions can be verified" - {
    "if all verifications pass, result should be Success[NonEmptyList[E],A]" in {

      "abc"
        .verifyThat("The 'abc' must have 'a'.")(_.contains("a"))
        .verifyThat("The 'abc' must have 'b'.")(_.contains("b"))
        .verifyThat("The 'abc' must have 3 chars.")(_.size == 3)
        .getValidation
      assert_=== "abc".success[NonEmptyList[S]]
    }

    "if any verifications doesn't pass, result should be Failure[NonEmptyList[E],A] collecting all fails" in {
      "abc"
        .verifyThat("The 'abc' must have 'X'.")(_.contains("X"))
        .verifyThat("The 'abc' must have 'Y'.")(_.contains("Y"))
        .verifyThat("The 'abc' must have 3 chars.")(_.size == 3)
        .getValidation
      assert_=== NonEmptyList ("The 'abc' must have 'X'.", "The 'abc' must have 'Y'.").failure[NonEmptyList[S]]
    }
  }
}

Remaind that the specs is only idea explanation and it is not required to implement that feature using proposed api.

v7: NonEmptyList.size() is off-by-one

The definition in Scalaz 7,

def list: List[A] = head :: tail
def size: Int = 1 + list.size

An example usage that works (naively appears to, but types aren't right),

scala> val nel = NonEmptyList(1, List(2, 3))
nel: scalaz.NonEmptyList[Any] = NonEmptyList(1, List(2, 3))

scala> nel.list.size
res2: Int = 2

scala> nel.size
res3: Int = 3

scala> nel.head
res4: Any = 1

scala> nel.tail
res5: List[Any] = List(List(2, 3))

An example usage that fails,

// Same as: val nel = NonEmptyList(1, List(2, 3):_*)
scala> val nel = NonEmptyList(1, 2, 3)
nel: scalaz.NonEmptyList[Int] = NonEmptyList(1, 2, 3)

scala> nel.list.size
res3: Int = 3

scala> nel.size
res4: Int = 4

scala> nel.head
res5: Int = 1

scala> nel.tail
res6: List[Int] = List(2, 3)

Stackoverflow error when traversing a Stream with a State

I have the following example (similar to issue #40, also with scalaz-core-6.0.4-SNAPSHOT):

    val counter = state((i:Int) => (i+1, i+1))
    val traversed: State[Int, Stream[Int]] = (Stream.from(5)).take(10000).traverse[({type l[a]=State[Int, a]})#l, Int](i => counter)
    val started: Stream[Int] = traversed ! 5
    val result: Stream[Int] = started.take(3)
    result.toList must_== List(6, 7, 8)

And I get a SOE:

[error] scalaz.Foldable$$anon$13.foldRight(Foldable.scala:95)
[error] scalaz.Foldable$$anon$13$$anonfun$foldRight$6.apply(Foldable.scala:95)
[error] scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)
[error] scalaz.Traverse$$anon$4$$anonfun$traverse$6.apply(Traverse.scala:31)

At first I thought that foldRight was causing the issue and I should just use foldLeft:

  implicit def StreamTraverse: Traverse[Stream] = new Traverse[Stream] {
    def traverse[F[_]: Applicative, A, B](f: A => F[B], as: Stream[A]): F[Stream[B]] =
      as.foldl[F[Stream[B]]]((Stream.Empty: Stream[B]) pure)((ys, x) => implicitly[Apply[F]].apply(f(x) map ((a: B) => (b: Stream[B]) => a #:: b), ys))
  }

Alas, I still get a SOE:

[error] scalaz.States$$anon$1.apply(State.scala:45)
[error] scalaz.State$$anonfun$map$1.apply(State.scala:8)
[error] scalaz.State$$anonfun$map$1.apply(State.scala:8)
[error] scalaz.States$$anon$1.apply(State.scala:45)

I think that something escapes my understanding here.

I also note that I'm not the only one to face this issue but I couldn't find out if others have solved it already.

Thanks.

Bug in StreamT.isEmpty

The definition here.

I think it should be:

def isEmpty(implicit M:Monad[M]) = uncons map { !_.isDefined }

IO.except not working correctly

putStrLn("first!") >|> throwIO(new Exception("help!")) except (e => putStrLn(e.getMessage)) unsafePerformIO

results in the output

first!
java.lang.Exception: help!
at $anonfun$1.apply(<console>:17)
at $anonfun$1.apply(<console>:17)
at scalaz.MA$$anonfun$$greater$great

instead of the expected

first!
help!

import Scalaz._ no longer works in latest snapshots

scala> import scalaz._
import scalaz._

scala> import scalaz.   <pressing tab>
effect     iteratee   syntax     

scala> import Scalaz._
<console>:10: error: not found: value Scalaz
       import Scalaz._
              ^

This works with M2, but not the latest snapshots

scalaz.geo.Coord should return useful azimuth/reverse azimuth for inverse Vincenty

The function inverse (defined in the Coord trait) implements Vincenty's inverse method:
http://en.wikipedia.org/wiki/Vincenty's_formulae

The results returned for (azi, reverseAzi) are:
(0D, 0D)
(180D, 0D)
(0D, 180D)
(Double.NaN, Double.NaN)

It seems to me that it should really return the result of calculating Vincenty's algorithm, e.g.

    (
      atan2(cosu2 * sin(ed.lambda), cosu1 * sinu2 - sinu1 * cosu2 * cos(ed.lambda)).fromRadians[Double],
      atan2(cosu1 * sin(ed.lambda), -sinu1 * cosu2 + cosu1 * sinu2 * cos(ed.lambda)).fromRadians[Double]
    )

I also don't see that it's reasonable to offer different options depending on whether the algorithm converges within 20 steps or not. The most recent iteration (hopefully) offers the most accurate estimation of the values.

Does this seem reasonable? Would you like a patch/pull request along these lines? (I'm not sure what scalaz code submission policies are).

DSL notation with <*> won't compile without parens

I couldn't come with a better issue title.

Let's create a small validation DSL

scala> val validator = new { def fail(msg: String) = failure[String, Int](msg) }
validator: java.lang.Object{def fail(msg: String): scalaz.Validation[String,Int]} = $anon$1@1e818d28

This will compile and run:

scala> (validator fail "foo") <*> (validator fail "bar" map ((_: Int) => (_: Int) => 1))
res30: scalaz.Validation[String,Int] = Failure(barfoo)

But this won't compile:

scala> validator fail "foo" <*> (validator fail "bar" map ((_: Int) => (_: Int) => 1))
<console>:15: error: type mismatch;
  found   : scalaz.Validation[String,Int => Int]
  required: java.lang.Comparable[java.lang.String => ?]
          validator fail "foo" <*> (validator fail "bar" map ((_: Int) => (_: Int) => 1))

Note that I omitted the parens (validator fail "foo"). It seams the compiler treats the first char of the operator <*> as a lessThan.
It can be worked around by adding parens, or by aliasing the <*> function.

Lens Families

See http://comonad.com/reader/2012/mirrored-lenses/ for an explanation and http://hackage.haskell.org/package/lens for an implementation, although I am not suggesting that we need to abandon the coalgebr(a|oid) of the (indexed) store comonad model, since Scala doesn't comfortably support higher-rank polymorphism. Basically:

type IxStoreT[+F[+_], +I, -B, +A] = (F[B => A], I)
type IxStore[+I, -B, +A] = IxStoreT[Id, I, B, A]
type StoreT[+F[+_], B, +A] = IxStoreT[F, B, B, A]
type Store[B, +A] = StoreT[Id, B, A]



type LensFamilyT[+F[+_], -A, +B, +C, -D] = A => F[IxStore[C, D, B]]
type LensFamily[-A, +B, +C, -D] = LensFamilyT[Id, A, B, C, D]
type LensT[+F[+_], A, B] = LensFamilyT[F, A, A, B, B]
type Lens[A, B] = LensT[Id, A, B]

type PLensFamilyT[+F[+_], -A, +B, +C, -D] = A => F[Option[IxStore[C, D, B]]]
type PLensFamily[-A, +B, +C, -D] = PLensFamilyT[Id, A, B, C, D]
type PLensT[+F[+_], A, B] = PLensFamilyT[F, A, A, B, B]
type PLens[A, B] = PLensT[Id, A, B]

type TraversalFamilyT[+F[+_], -A, +B, +C, -D] = A => F[List[IxStore[C, D, B]]]
type TraversalFamily[-A, +B, +C, -D] = TraversalFamilyT[Id, A, B, C, D]
type TraversalT[+F[+_], A, B] = TraversalFamilyT[F, A, A, B, B]
type Traversal[A, B] = TraversalT[Id, A, B]

type NETraversalFamilyT[+F[+_], -A, +B, +C, -D] = A => F[NonEmptyList[IxStore[C, D, B]]]
type NETraversalFamily[-A, +B, +C, -D] = NETraversalFamilyT[Id, A, B, C, D]
type NETraversalT[+F[+_], A, B] = NETraversalFamilyT[F, A, A, B, B]
type NETraversal[A, B] = NETraversalT[Id, A, B]

Lens families support polymorphic update, as well as a more natural variance behavior than simple lenses. This is fairly exploratory, but I would be interested to see them added to Scalaz nonetheless. It would greatly expand the range of capabilities that Scalaz's lenses could cover.

If functions

Some handy functions to write related to if-branching. The haskell version is below.

import Data.Monoid

if' ::
  (Eq p, Monoid p) =>
  a
  -> a
  -> p
  -> a
if' t f p =
  if p == mempty then t else f

xif ::
  (Eq p, Monoid p) =>
  (a -> a)
  -> p
  -> a
  -> a
xif =
  if' id

ifx ::
  (Eq p, Monoid p) =>
  (a -> a)
  -> p
  -> a
  -> a
ifx = 
  flip if' id

zif ::
  (Eq p, Monoid p, Monoid a) =>
  a
  -> p
  -> a
zif =
  if' mempty

ifz ::
  (Eq p, Monoid p, Monoid a) =>
  a
  -> p
  -> a
ifz =
  flip if' mempty

Scalaz7 - Monoid instances for TupleX incorrect

The current Monoid instances for Tuples cause stack overflow.

A working implementation for Tuple2:

implicit def Tuple2Monoid[A, B](implicit ma: Monoid[A], mb: Monoid[B]): Monoid[(A, B)] = {
  implicit val sa = ma.semigroup
  implicit val za = ma.zero
  implicit val sb = mb.semigroup
  implicit val zb = mb.zero
  monoid[(A, B)]
}

foldRightM is wrong

The signature and implementation of MA.foldRightM is wrong. The subsequent values in the test are incorrect.

Pair lacks Comonad for left and right projections

You should be able to project a pair to its left and right sides, much like
with Either. The left and right projections of a Pair are Comonads (vs being
Monads for Either). I think this is preferable to arbitrarily making the
Comonad for pairs right-biased. People who really want that behavior could
always define an implicit conversion from a pair to its right projection.

alternative applicative builder syntax doesn't work for type-classes of multiple arity

given that the applicative builder syntax |@| now has deprecation warnings:

val (a, b) = 3.right[String] -> 4.right[String]
(a |@| b)(_+_)
    warning: method |@| in trait ApplyOps is deprecated: Use `^(f1,f2..fN)((a,b,c) => ..)` instead
               (a |@| b)(_+_)
                   ^
  scalaz.Unapply[scalaz.Apply,scalaz.\/[String,Int]]{type M[X] = scalaz.\/[String,X]; type A = Int}#M[Int] = \/-(7)

the documented alternative doesn't work for greater than arity type constructors such as / and Validation:

scala> ^(a, b)(_ + _)
<console>:16: error: type mismatch;
 found   : scalaz.\/[String,Int]
 required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
 both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
 and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
 are possible conversion functions from scalaz.\/[String,Int] to ?F[?A]
              ^(a, b)(_ + _)
               ^

Reducer arrow

Reducer and Generator arrows as per Ed Kmett's presentation on monoids.

Reducer may not be general enough. What we really want is an arrow: (Monoid m) => a -> m-> b, or a triple of:

Monoid m
unit :: a -> m
counit :: m -> b

sbt include

The current ReadMe.md on the scalaz-seven branch does not include a preferred sbt include statment.

(ie. "org.scalaz" %% "scalaz-core" % "7.0.0")

version 7.0-SNAPSHOT is not resolving for me.

difficulty resolving Unapply instance for \/

for some reason under some circumstances the inferencer won't work out how to sequence a List[Foo / String] to a Foo / List[String]

in the REPL:

case class Foo(s: String); type foo[A] = Foo \/ A; val as : List[Foo \/ String] = List("one".right, "two".right, "three".right); val sa = as.sequence

gives:

error: could not find implicit value for parameter ev: scalaz.Leibniz.===[scalaz.\/[Foo,String],G[B]]

If however we alias this to a single kind:

case class Foo(s: String); type foo[A] = Foo \/ A; val as : List[foo[String]] = List("one".right, "two".right, "three".right); val sa = as.sequence

then all is well.

Hypothesis is that there's still some issue as hinted in the top of the Unapply_2 trait:

// Things get tricky with type State[S, A] = StateT[Id, S, A], both unapplyMAB2 and unapplyMFAB2 are applicable
// Without characterizing this fully, I'm using the standard implicit prioritization to avoid this.

but wat would I know?

Effect JAR has typo in OSGi metadata

The scalaz-effect JAR has a typo in its package export list, exporting scalaz.std.sffect instead of scalaz.std.effect.

I've tested the other JARs in Apache Karaf using the following commands:

bundle:install mvn:org.scala-ide/scala-library/2.9.2.v20120330-163119-949a4804e4-vfinal
bundle:install mvn:org.scalaz/scalaz-core_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-effect_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-iteratee_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-concurrent_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-typelevel_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-xml_2.9.2/7.0.0-M3

After fixing the typo in the effect manifest, everything in this list can be resolved successfully.

Something is broke in FunctionABMonoid (6.0.1)

scala> List(1 + (_: Int), 2 * (_: Int)).sumr.apply(7)
java.lang.StackOverflowError
    at $anonfun$1.apply(<console>:12)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$33.apply(Semigroup.scala:136)
    at scalaz.Identity$$anon$1.value(Identity.scala:170)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(Abstrac...
scala> lastException.printStackTrace                 
java.lang.StackOverflowError
    at line6$object$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:12)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$33.apply(Semigroup.scala:136)
    at scalaz.Identity$$anon$1.value(Identity.scala:170)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroups$$anon$1.append(Semigroup.scala:21)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroups$$anon$1.append(Semigroup.scala:21)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroups$$anon$1.append(Semigroup.scala:21)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroups$$anon$1.append(Semigroup.scala:21)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroups$$anon$1.append(Semigroup.scala:21)
    at scalaz.Identity$class.$bar$plus$bar(Identity.scala:16)
    at scalaz.Identity$$anon$1.$bar$plus$bar(Identity.scala:169)
    at scalaz.IdentitySugar$class.$u22B9(Identity.scala:192)
    at scalaz.Identity$$anon$1.$u22B9(Identity.scala:169)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32.apply(Semigroup.scala:136)
    at scalaz.Semigroup$$anonfun$Function1ABSemigroup$1$$anonfun$apply$32$$anonfun$apply$34.apply(Semigroup.scala:136)
    at scala.Function0$class.apply$mcI$sp(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcI$sp(AbstractFunction0.scala:17)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)
    at scalaz.Semigroup$$anonfun$IntSemigroup$1.apply(Semigroup.scala:54)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.