July 25, 2008, Friday, 206

Scalaz

From Workingmouse Wiki

Jump to: navigation, search

Scalaz (Scar-lah-zed) is a library written in the Scala Programming Language. One mandate of the library is to depend only on the core Scala API and the core Java 2 Standard Edition API. The intention of Scalaz is to include general functions that are not currently available in the core Scala API. Scalaz is released under a BSD open source licence making it compatible with the licence of the Scala project.

Scalaz has been tested using the technique of Automated Specification-Based Testing which originated with a proposal called QuickCheck for the Haskell Programming Language. The original idea of QuickCheck has been ported to the Scala platform as a project called ScalaCheck. Scalaz is currently tested using ScalaCheck version 1.0.

Version 2.0 is the first public release of Scalaz (earlier versions were created internally as it evolved). Much of Scalaz evolved as a side-effect of working on commercial client projects or other domain-specific projects that required general libraries, which were unavailable in the core Scala API. The implementation of Scalaz is open to suggestions and patches are welcomed (BSD licence compatible of course).

Features

Either[A, B] = Left[A] | Right[B]

The Either ADT is typically used in preference to the Option ADT for representing early failure of a non-terminating function. In a Java context, null is to Scala's Option as exceptions are to Scalaz' Either. The Either ADT conventionally represents failure on the Left side and the result of successful function termination on the Right side. The Either type includes some fundamental functions but also many functions that would also be useful if they were included in the core scala.Option type. However, Scalaz includes many of these additional functions over Option (see below).


The enhanced Option type is provided using the technique known as Pimp My Library. Additional functions include lifting function application (of various arity) into Option (known as liftM for the more general Monad computational abstraction). This is akin to the existing map2 and map3 functions on object scala.List, which lift function application (binary (arity-2) and ternary (arity-3) respectively) into List. Note also that the more canonical and therefore, familiar map function on various types (T) performs unary (arity-1) function application through T.


  • Basic Prelude functions

Functions that are not specific to any Algebraic Data Type, but are general and useful.


  • Control package

The Scalaz control package is made possible using higher-kinded type polymorphism and includes computational abstractions such as:

    • The Functor abstraction
    • The Monad abstraction (as a specific type of Functor) and functions over all monads e.g.
      • ap Function application within a monad
      • mapM Sequencing a function across many arguments within a monad
      • liftM(n) Apply n-arity function within a monad (liftM1 is equivalent to Functor map)
    • The Foldable abstraction to represent a catamorphism and some functions over all catamorphisms
    • The Monoid and Semigroup abstractions
    • The MonadZero, MonadPlus (represent a final coalgebra or anamorphism) and MonadOr abstractions

  • Integration with existing Java libraries

This includes functions for integrating core Scala libraries with existing Java core types.


  • A package for transparent memoisation

This package was formerly known as Dynamic Scala before integration into Scalaz. It is loosely inspired by a paper written by Simon Peyton-Jones, Simon Marlow, and Conal Elliott (IFL '99) titled, "Stretching the storage manager: weak pointers and stable names in Haskell". It includes usage examples such as:

    • The Fibonacci number series algorithm
    • The Edit Distance backtracking algorithm (a.k.a. Levenshtein Distance)
    • The 0-1 Knapsack Problem

  • Validation package

This library is based on the Either Algebraic Data Type for validating multiple potentially failing functions. This package provides a type-safe and concise (thanks to the Scala language) method for web application form field validation. More information, including usage examples, can be found at the Scalaz Validation page.


Links