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.

NonEmptyList

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 getRandomElement(NonEmptyList([])) // Compiles, you don't need to check for elements if the list length is known at compile time getRandomElement(NonEmptyList([1])) // For runtime values, you have to deal with a Maybe const numbers: number[] = getArrayFromForm() const randEl: Maybe<number> = NonEmptyList.fromArray(numbers).map(getRandomElement) 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:

Maybe and Either predicates narrow the type

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 } 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.

Wildcard pattern for pattern matching

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

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

Maybe.fromPredicate , Maybe#join and Maybe#orDefaultLazy

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

Improved pretty printing

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" When usingon ADT instances now it displays the constructor name. Keep in mind that this behaviour is strictly for pretty printing, in the case ofit strips out any ADT info and leaves only relevant JSON data.

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