TSFoster / elm-tuple-extra / Tuple2

Convenience functions for working with 2-tuples.

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)

Swapping values

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

Swap the values in a 2-tuple.

swap ( '1', "1" ) --> ("1", '1')

Applying functions

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

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

uncurry List.repeat ( 3, 'a' ) --> ['a', 'a', 'a']

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

Apply the second value in a 2-tuple to the function in the first position.

apply (String.toLower, "HELLO") --> "hello"

Tuples and Maybes

maybeMapFirst : (a -> Maybe a2) -> ( a, b ) -> Maybe ( a2, b )

Apply a function that produces a Maybe to the first value in a 2-tuple. If it returns Just something, pair the something back with the second value in the 2-tuple.

("1", "horse")
  |> maybeMapFirst String.toInt
--> Just (1, "horse")

("One", "horse")
  |> maybeMapFirst String.toInt
--> Nothing

maybeMapSecond : (b -> Maybe b2) -> ( a, b ) -> Maybe ( a, b2 )

Apply a function that produces a Maybe to the second value in a 2-tuple. If it returns Just something, pair the something back with the first value in the 2-tuple.

("King", ["Kong", "Kunta", "Henry VIII"])
  |> maybeMapSecond List.head
--> Just ("King", "Kong")