Skip to content

Commit 2329fe5

Browse files
committed
Add Arg.map
1 parent 94c4a63 commit 2329fe5

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/Elm/Arg.elm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Elm.Arg exposing
66
, ignore, string, char, int
77
, list, item, items, listRemaining
88
, customType, customTypeWith
9+
, map
910
)
1011

1112
{-| An `Arg` can be used to pattern match on the arguments of a function.
@@ -66,6 +67,11 @@ Will generate
6667
6768
@docs customType, customTypeWith
6869
70+
71+
## Advanced
72+
73+
@docs map
74+
6975
-}
7076

7177
import Elm exposing (Arg, Expression)
@@ -296,3 +302,10 @@ customTypeWith :
296302
-> Arg a
297303
customTypeWith =
298304
Internal.Arg.customTypeWith
305+
306+
307+
{-| Transforms an argument using the given function.
308+
-}
309+
map : (a -> b) -> Arg a -> Arg b
310+
map =
311+
Internal.Arg.map

src/Internal/Arg.elm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Internal.Arg exposing
77
, customType, customTypeWith
88
, item, items, list, listRemaining, record, field
99
, ignore, unit
10+
, map
1011
)
1112

1213
{-|
@@ -828,3 +829,15 @@ field name (Arg arg) =
828829
toRecord.value fieldExpression
829830
}
830831
)
832+
833+
834+
map : (a -> b) -> Arg a -> Arg b
835+
map f (Arg toValue) =
836+
Arg
837+
(\index ->
838+
let
839+
value =
840+
toValue index
841+
in
842+
{ details = value.details, value = f value.value, index = value.index }
843+
)

tests/Declare.elm

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Declare exposing (makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite)
1+
module Declare exposing (argMap, makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite)
22

33
{-| -}
44

@@ -9,7 +9,9 @@ import Elm.Declare
99
import Elm.Expect
1010
import Elm.Op
1111
import Expect
12+
import Gen.Basics
1213
import Gen.Debug
14+
import Gen.Maybe
1315
import Test exposing (Test, describe, test)
1416

1517

@@ -278,6 +280,36 @@ makeAndCaseGeneration =
278280
()
279281

280282

283+
argMap : Test
284+
argMap =
285+
test "Elm.Arg.map" <|
286+
\_ ->
287+
let
288+
-- myMap : Elm.Declare.Function ((Elm.Expression -> Elm.Expression) -> Elm.Expression -> Elm.Expression)
289+
call_myMap =
290+
Elm.Declare.fnBuilder "myMap" (\fn maybe -> Gen.Maybe.call_.map fn maybe)
291+
|> Elm.Declare.fnArg (Elm.Arg.var "fn")
292+
|> Elm.Declare.fnArg (Elm.Arg.var "maybe")
293+
|> Elm.Declare.fnDone
294+
in
295+
Expect.all
296+
[ \_ ->
297+
Elm.Expect.declarationAs call_myMap.declaration
298+
"""
299+
myMap : (a -> b) -> Maybe a -> Maybe b
300+
myMap fn maybe =
301+
Maybe.map fn maybe
302+
"""
303+
, \_ ->
304+
Elm.Expect.renderedAs (call_myMap.call Gen.Basics.values_.identity Gen.Maybe.make_.nothing)
305+
"myMap Basics.identity Nothing"
306+
, \_ ->
307+
Elm.Expect.renderedAs (call_myMap.call (Elm.functionReduced "i" <| \i -> Elm.Op.multiply i i) Gen.Maybe.make_.nothing)
308+
"myMap (\\i -> i * i) Nothing"
309+
]
310+
()
311+
312+
281313
makeAndCaseGenerationFullyDynamic : Test
282314
makeAndCaseGenerationFullyDynamic =
283315
test "Elm.Declare.customTypeAdvanced - fully dynamic" <|

0 commit comments

Comments
 (0)