Skip to content
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
13 changes: 13 additions & 0 deletions src/Elm/Arg.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Elm.Arg exposing
, ignore, string, char, int
, list, item, items, listRemaining
, customType, customTypeWith
, map
)

{-| An `Arg` can be used to pattern match on the arguments of a function.
Expand Down Expand Up @@ -66,6 +67,11 @@ Will generate

@docs customType, customTypeWith


## Advanced

@docs map

-}

import Elm exposing (Arg, Expression)
Expand Down Expand Up @@ -296,3 +302,10 @@ customTypeWith :
-> Arg a
customTypeWith =
Internal.Arg.customTypeWith


{-| Transforms an argument using the given function.
-}
map : (a -> b) -> Arg a -> Arg b
map =
Internal.Arg.map
13 changes: 13 additions & 0 deletions src/Internal/Arg.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Internal.Arg exposing
, customType, customTypeWith
, item, items, list, listRemaining, record, field
, ignore, unit
, map
)

{-|
Expand Down Expand Up @@ -828,3 +829,15 @@ field name (Arg arg) =
toRecord.value fieldExpression
}
)


map : (a -> b) -> Arg a -> Arg b
map f (Arg toValue) =
Arg
(\index ->
let
value =
toValue index
in
{ details = value.details, value = f value.value, index = value.index }
)
37 changes: 36 additions & 1 deletion tests/Declare.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Declare exposing (makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite)
module Declare exposing (argMap, makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite)

{-| -}

Expand All @@ -9,7 +9,9 @@ import Elm.Declare
import Elm.Expect
import Elm.Op
import Expect
import Gen.Basics
import Gen.Debug
import Gen.Maybe
import Test exposing (Test, describe, test)


Expand Down Expand Up @@ -278,6 +280,39 @@ makeAndCaseGeneration =
()


argMap : Test
argMap =
test "Elm.Arg.map" <|
\_ ->
let
-- myMap : Elm.Declare.Function ((Elm.Expression -> Elm.Expression) -> Elm.Expression -> Elm.Expression)
call_myMap =
Elm.Declare.fnBuilder "myMap" (\fn maybe -> Gen.Maybe.call_.map fn maybe)
|> Elm.Declare.fnArg (Elm.Arg.var "fn")
|> Elm.Declare.fnArg (Elm.Arg.var "maybe")
|> Elm.Declare.fnDone
in
Expect.all
[ \_ ->
Elm.Expect.declarationAs
"""
myMap : (a -> b) -> Maybe a -> Maybe b
myMap fn maybe =
Maybe.map fn maybe
"""
call_myMap.declaration
, \_ ->
Elm.Expect.renderedAs
"myMap Basics.identity Nothing"
(call_myMap.call Gen.Basics.values_.identity Gen.Maybe.make_.nothing)
, \_ ->
Elm.Expect.renderedAs
"myMap (\\i -> i * i) Nothing"
(call_myMap.call (Elm.functionReduced "i" <| \i -> Elm.Op.multiply i i) Gen.Maybe.make_.nothing)
]
()


makeAndCaseGenerationFullyDynamic : Test
makeAndCaseGenerationFullyDynamic =
test "Elm.Declare.customTypeAdvanced - fully dynamic" <|
Expand Down