elmcraft/core-extra/1.0.0

NOTE: the latest version is 2.3.0

Tuple.Extra

Convenience functions for 2-Tuples (also known as pairs).

type alias Tuple a b =
( a, b )

By type aliasing tuples into a "normal" type, we remove the (small) effort required in reading types and signatures that have tuples in. I've found this is most beneficial when a tuple is nested inside another type. Visually, the Tuple type is now no different to List, Maybe, or Result.

For example, this:

List (Maybe ( String, Int ))

becomes:

List (Maybe (Tuple String Int))

Paring

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

In certain situations, this proves more "pipe friendly" than the standard Tuple.pair. Fits nicely in your update function.

{ model | count = model.count + 1 }
    |> Tuple.Extra.pairWith Cmd.none
from : a -> ( a, a )

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

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

Manipulating

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

Given a function that takes two arguments, apply that function to the two values contained in a tuple.

Tuple.Extra.apply (+) ( 1, 2 )
    --> 3
flip : ( a, b ) -> ( b, a )

Flip the two values contained in a tuple.

join : appendable -> ( appendable, appendable ) -> appendable

Similar to String.join but for tuples instead of lists. Given some separator string, join together two strings in a tuple.

Tuple.Extra.join " " ( "Hello", "world" )
    --> "Hello world"
(a -> appendable)
-> (b -> appendable)
-> appendable
-> ( a, b )
-> appendable

Works just like join, but first converts the values of the tuple to strings. These means the function works with any type of tuple.

Tuple.Extra.joinBy String.fromInt suitToString " of " ( 7, Club )
    == "Seven of Clubs"
sum : ( number, number ) -> number

Similar to List.sum but for tuples instead of lists. Adds together two numbers contained in a tuple.

Tuple.Extra.sum ( 1, 2 )
    --> 3
product : ( number, number ) -> number

Similar to List.sum but for tuples instead of lists. Multiplies together two numbers contained in a tuple

Tuple.Extra.product ( 1, 2 )
    --> 2
sort : ( comparable, comparable ) -> ( comparable, comparable )

Similar to List.sort but for tuples instead of lists. Sort values contained in a tuple from lowest to highest

Tuple.Extra.sort ( 2, 1 )
    --> ( 1, 2 )
sortBy : (a -> comparable) -> ( a, a ) -> ( a, a )

Similar to List.sortBy but for tuples instead of lists. Sort values contained in a tuple by first converting both values to a comparable. The values are sorted lowest to highest

Tuple.Extra.sortBy String.length ( "mouse", "cat" )
    --> ( "cat", "mouse" )
sortWith : (a -> a -> Order) -> ( a, a ) -> ( a, a )

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

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

Mapping

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

Apply a function to both values contained in a tuple. This might also be known as mapBothWith or bimap.

Tuple.Extra.map negate ( -3, 10 )
    --> ( 3, -10 )

Deprecated functions

These functions are deprecated and will be removed in the next major version of this library.

sequenceMaybe : ( Maybe a, Maybe b ) -> Maybe ( a, b )

Occasionally you might find yourself in a situation where both values contained in a tuple are Maybes. Sometimes it makes more sense to take those values and make the tuple a Maybe instead.

Tuple.Extra.sequenceMaybe ( Just 10, Nothing )
    --> Nothing


Tuple.Extra.sequenceMaybe ( Just 10, Just "Cat" )
    --> Just ( 10, "Cat" )

@deprecated in favour of Maybe.Extra.combineBoth.

sequenceFirstMaybe : ( Maybe a, b ) -> Maybe ( a, b )

Similar to sequenceMaybe but only looks at the first value in a tuple to check for nothingness.

Tuple.Extra.sequenceFirstMaybe ( Just 10, "Cat" )
    --> Just ( 10, "Cat" )

@deprecated in favour of Maybe.Extra.combineFirst.

sequenceSecondMaybe : ( a, Maybe b ) -> Maybe ( a, b )

Similar to sequenceMaybe but only looks at the first value in a tuple to check for nothingness.

Tuple.Extra.sequenceSecondMaybe ( 10, Just "Cat" )
    --> Just ( 10, "Cat" )

@deprecated in favour of Maybe.Extra.combineSecond.