Skip to content

added mapBothSame and mapAllThreeSame with examples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Tuple2.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Tuple2 exposing
( pairTo, double
, swap
, uncurry, apply
, mapBothSame
, maybeMapFirst, maybeMapSecond
)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Tuple3.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down