elmcraft/core-extra/2.2.0

NOTE: the latest version is 2.3.0

Triple.Extra

Convenience functions for 3-Tuples (also known as Triple).

Please remember the following from the Tuple documentation:

For more complex data, it is best to switch to records. So instead of representing a 3D point as (3,4,5) and not having any helper functions, represent it as { x = 3, y = 4, z = 5 } and use all the built-in record syntax!

This is good advice! However triples do have their usecases for anonymous groupings and as such having some helper functions can be quite helpful.

type alias Triple a b c =
( a, b, c )

By type aliasing Triples into a "normal" type, we remove the (small) effort required in reading types and signatures that have Triples in. This is most beneficial when a Triple is nested inside another type. Visually, the Triple type is now no different to List, Maybe, or Result.

For example, this:

List (Maybe ( String, Int, Char ))

becomes:

List (Maybe (Triple String Int Char))

Create

triple : a -> b -> c -> ( a, b, c )

Create a triple.

from : a -> ( a, a, a )

Occasionally you might want to create a Triple from a single value. This does just that.

Triple.Extra.from 1 --> ( 1, 1, 1 )

Access

first : ( a, b, c ) -> a

Extract the first value from a triple.

second : ( a, b, c ) -> b

Extract the second value from a triple.

third : ( a, b, c ) -> c

Extract the third value from a triple.

Manipulating

apply : (a -> b -> c -> d) -> ( a, b, c ) -> d

Given a function that takes three arguments, apply that function to the three values contained in a Triple.

( 1, 2, Array.fromList [0,1,2,3,4] )
    |> Triple.Extra.apply Array.slice
    --> Array.fromList [1,2,3]
sortWith : (a -> a -> Order) -> ( a, a, a ) -> ( a, a, a )

Similar to List.sortWith but for Triples instead of lists. Instead of converting values contained in a Triple to comparables, instead supply a function that will produce an Order directly.

Triple.Extra.sortWith Basics.compare ( 2, 1, 3 )
    --> ( 1, 2, 3 )

Mapping

map : (a -> b) -> ( a, a, a ) -> ( b, b, b )

Apply a function to all values contained in a Triple.

Triple.Extra.map negate ( -3, 10, -7 )
    --> ( 3, -10, 7 )
mapFirst : (a -> x) -> ( a, b, c ) -> ( x, b, c )

Transform the first value in a triple.

Triple.Extra.mapFirst negate ( -3, 10, '9' )
    --> ( 3, 10, '9' )
mapSecond : (b -> x) -> ( a, b, c ) -> ( a, x, c )

Transform the second value in a triple.

Triple.Extra.mapSecond negate ( -3, 10, '9' )
    --> ( -3, 010, '9' )
mapThird : (c -> x) -> ( a, b, c ) -> ( a, b, x )

Transform the third value in a triple.

Triple.Extra.mapThird String.toUpper ( -3, 10, "a" )
    --> ( -3, 10, "A" )

Lists

toList : ( a, a, a ) -> List a

Turns a triple into a list of three elements.

Triple.Extra.toList ( 1, 2, 3 )
    --> [ 1, 2, 3 ]