TSFoster/elm-tuple-extra/1.0.0

NOTE: the latest version is 2.0.0

Tuple.Extra

Convenience functions for working with tuples.

Tuple functions for 3-tuples

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

Extract the first value from a 3-tuple.

first ( 3, 4, 5 ) --> 3


first ( "John", "H.", "Doe" ) --> "John"
second : ( a, b, c ) -> b

Extract the second value from a 3-tuple.

second ( 3, 4, 5 ) --> 4


second ( "John", "H.", "Doe" ) --> "H."
third : ( a, b, c ) -> c

Extract the third value from a 3-tuple.

third ( 3, 4, 5 ) --> 5


third ( "John", "H.", "Doe" ) --> "Doe"
mapFirst : (a -> a2) -> ( a, b, c ) -> ( a2, b, c )

Transform the first value in a 3-tuple.

mapFirst String.reverse ( "stressed", 16, 1 ) --> ("desserts", 16, 1)


mapFirst String.length ( "stressed", 16, 1 ) --> (8, 16, 1)
mapSecond : (b -> b2) -> ( a, b, c ) -> ( a, b2, c )

Transform the second value in a 3-tuple.

mapSecond sqrt ( "stressed", 16, 1 ) --> ("stressed", 4, 1)


mapSecond negate ( "stressed", 16, 1 ) --> ("stressed", -16, 1)
mapThird : (c -> c2) -> ( a, b, c ) -> ( a, b, c2 )

Transform the second value in a 3-tuple.

mapThird (List.repeat 2) ( "stressed", 16, 1 ) --> ("stressed", 16, [1, 1])


mapThird String.fromInt ( "stressed", 16, 1 ) --> ("stressed", 16, "1")
mapAllThree : (a -> d) -> (b -> e) -> (c -> f) -> ( a, b, c ) -> ( d, e, f )

Transform all three parts of a 3-tuple.

mapAllThree String.reverse sqrt List.length ( "stressed", 16, [] ) --> ("desserts", 4, 0)


mapAllThree String.length negate List.isEmpty ( "stressed", 16, [] ) --> (8, -16, True)

Creating tuples

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

Create a 2-tuple.

{ foo = [1, 2], bar = Nothing }
    |> pairTo Cmd.none
--> ({ foo = [1, 2], bar = Nothing }, Cmd.none)
double : a -> ( a, a )

Create a 2-tuple.

double 0 --> (0, 0)
triple : a -> ( a, a, a )

Create a 3-tuple.

triple 0 --> (0, 0, 0)

Swapping values

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

Swap the values in a tuple.

swap ( '1', "1" ) --> ("1", '1')
reverse : ( a, b, c ) -> ( c, b, a )

Reverse values in a 3-tuple.

reverse ( 1, 2, 3 ) --> (3, 2, 1)
rotateLeft : ( a, b, c ) -> ( b, c, a )

Rotate values in 3-tuple to the left.

rotateLeft ( 1, 2, 3 ) --> (2, 3, 1)
rotateRight : ( a, b, c ) -> ( c, a, b )

Rotate values in 3-tuple to the right.

rotateRight ( 1, 2, 3 ) --> (3, 1, 2)
swapFirst : ( a, b, c ) -> ( b, a, c )

Swap first two values in a 3-tuple.

swapFirst ( 1, 2, 3 ) --> (2, 1, 3)
swapLast : ( a, b, c ) -> ( a, c, b )

Swap last two values in a 3-tuple.

swapLast ( 1, 2, 3 ) --> (1, 3, 2)

Applying functions

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

Apply each value in a 2-tuple to a function that takes at least 2 arguments.

uncurry2 List.repeat ( 3, 'a' ) --> ['a', 'a', 'a']
uncurry3 : (a -> b -> c -> d) -> ( a, b, c ) -> d

Apply each value in a 3-tuple to a function that takes at least 3 arguments.

type alias Person =
    { name : String
    , age : Int
    , hobbies : List String
    }


uncurry3 Person ("John Doe", 34, ["Hockey"])
    --> { name = "John Doe", age = 34, hobbies = [ "Hockey" ] }