Purify v0.11

September 20, 2018
Not sure what purify is? Check out the Getting Started page. The package was renamed from `pure-ts` because of NSFW search results.


The new NonEmptyList ADT is a list that is guaranteed to have at least one value. Because of it's utility there is an implementation of this data structure in pretty much all ML languages, which is why it's now a part of purify too. Let's look at some example code:
import { NonEmptyList, head } from 'purify-ts/adts/NonEmptyList'

// Create functions with a contract - the caller has to verify that the input is valid instead of the callee
// Since the list parameter is guaranteed to have at least one element, this function will always return a value
const getRandomElement = <T>(list: NonEmptyList<T>): T =>
    list[Math.floor(Math.random() * list.length)]

// Doesn't compile

// Compiles, you don't need to check for elements if the list length is known at compile time

// For runtime values, you have to deal with a Maybe
const numbers: number[] = getArrayFromForm()
const randEl: Maybe<number> = NonEmptyList.fromArray(numbers).map(getRandomElement)

Maybe and Either predicates narrow the type

v0.11 makes a lot of improvements to type safety. Using one of TypeScript's more unique features - type predicates, the compiler can now know when it's safe to extract a value from a Maybe or Either.
const sometimesValue: Maybe<number> = ...

sometimesValue.extract() // number | null

if (sometimesValue.isJust()) {
    // Because extract() is in a block that guarantees the presence of a value, it's safe to return a number instead of a nullable number
    sometimesValue.extract() // number

Wildcard pattern for pattern matching

You can now use a wildcard when pattern matching a Maybe, Either or any other ADT that supports pattern matching.
 // v0.10
adt.caseOf({ Just: value => 0, Nothing: () => 0})

// Now
adt.caseOf({ _: () => 0 })

Tuple support for more array behaviour

Tuples now implement the Iterable and ArrayLike interfaces.
 const [ fst, snd ] = Tuple(1, 2)

New Maybe and Either methods

Check out the docs for Maybe and Either to find out more about the following methods:
  • Maybe.fromPredicate, Maybe#join and Maybe#orDefaultLazy
  • Either#join and Either#orDefaultLazy

Improved pretty printing

When using toString on ADT instances now it displays the constructor name. Keep in mind that this behaviour is strictly for pretty printing, in the case of JSON.stringify it strips out any ADT info and leaves only relevant JSON data.
const val = Just(5)
console.log(val.toString()) // "Just(5)"
console.log(JSON.stringify(val)) // "5"

All functions with multiple arguments support partial application

Added partial application support to: List#at
Improved partial application for: Tuple.fanout, Maybe.mapMaybe

Other changes

  • Removed Semigroup and Ord instances because they were not sound and making them typesafe required using very confusing type definitions.
  • Fixed Either#isRight type definition (thanks sledorze)
  • Made the value property inside the Maybe class private
  • Reduced package size by excluding the tests
  • Many improvements (rewordings, corrections and clarifications) made to the docs (thanks squirly)