diff --git a/README.md b/README.md index 983074e..a95bdda 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,11 @@ Convenience functions for working with tuples. * `pairTo`, `double` and `swap` for creating and arranging tuples * `uncurry` and `apply` for using functions with values in a tuple * `maybeMapFirst` and `maybeMapSecond` for lifting a `Maybe` out of a tuple +* `mapBothSame` for convenience to apply a function to a same-typed tuple ## `Tuple3` -* Copies and extensions of all `Tuple` functions to work with 3-tuples (`first`, `second`, `third`, `mapFirst`, `mapSecond`, `mapThird`, `mapAllThree`) +* Copies and extensions of all `Tuple` functions to work with 3-tuples (`first`, `second`, `third`, `mapFirst`, `mapSecond`, `mapThird`, `mapAllThree`, `mapAllThreeSame`) * Functions for creating 3-tuples (`join`, `joinTo`, `triple`, `splitFirst`, `splitSecond`) * Functions for rearranging values in 3-tuples (`reverse`, `rotateLeft`, `rotateRight`, `swapFirst`, `swapLast`) * `uncurry` and `apply` for using functions with values in a 3-tuple diff --git a/src/Tuple2.elm b/src/Tuple2.elm index fea4bc0..c8ea67e 100644 --- a/src/Tuple2.elm +++ b/src/Tuple2.elm @@ -2,6 +2,7 @@ module Tuple2 exposing ( pairTo, double , swap , uncurry, apply + , mapBothSame , maybeMapFirst, maybeMapSecond ) @@ -81,6 +82,18 @@ apply : ( a -> b, a ) -> b apply ( fn, a ) = fn a +{-| Apply a function to both elements of a 2-tuple where both entries are the same type. +Convenience function to help with repetitive usage in Tuple.mapBoth() + + ("1", "1000") + |> mapBothSame (\s -> s ++ "0") + --> ("10", "1000) + +-} +mapBothSame : (a -> a2) -> ( a, a ) -> ( a2, a2 ) +mapBothSame fn ( a, b ) = + (fn a, fn 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 diff --git a/src/Tuple3.elm b/src/Tuple3.elm index f0f5300..810f95e 100644 --- a/src/Tuple3.elm +++ b/src/Tuple3.elm @@ -108,6 +108,17 @@ mapThird fn ( a, b, c ) = ( a, b, fn c ) +{-| Transform all values in a 3-Tuple. + + mapAll (List.repeat 2) ( "stressed", 16, 1 ) --> ( ["stressed", "stressed"], [16, 16], [1, 1]) + + mapAll String.fromInt ( 10 16 1 ) -> ( "10", "16", "1" ) +-} +mapAll : (a -> a2) -> ( a, a, a ) -> ( a2, a2, a2) +mapAll fn ( a, b, c ) = + ( fn a, fn b, fn c) + + {-| Create a 3-tuple with three values. join 1 2 3 --> ( 1, 2, 3 )