From f96f5fbfd01ec839b9e233c020557c04366218f0 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 20 Jan 2026 12:54:18 +0100 Subject: [PATCH] Add Arg.map --- src/Elm/Arg.elm | 13 +++++++++++++ src/Internal/Arg.elm | 13 +++++++++++++ tests/Declare.elm | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Elm/Arg.elm b/src/Elm/Arg.elm index 545b1e8..7751b61 100644 --- a/src/Elm/Arg.elm +++ b/src/Elm/Arg.elm @@ -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. @@ -66,6 +67,11 @@ Will generate @docs customType, customTypeWith + +## Advanced + +@docs map + -} import Elm exposing (Arg, Expression) @@ -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 diff --git a/src/Internal/Arg.elm b/src/Internal/Arg.elm index 8b2f574..a571b73 100644 --- a/src/Internal/Arg.elm +++ b/src/Internal/Arg.elm @@ -7,6 +7,7 @@ module Internal.Arg exposing , customType, customTypeWith , item, items, list, listRemaining, record, field , ignore, unit + , map ) {-| @@ -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 } + ) diff --git a/tests/Declare.elm b/tests/Declare.elm index f91331a..9eacb93 100644 --- a/tests/Declare.elm +++ b/tests/Declare.elm @@ -1,4 +1,4 @@ -module Declare exposing (makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite) +module Declare exposing (argMap, makeAndCaseGeneration, makeAndCaseGenerationFullyDynamic, suite) {-| -} @@ -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) @@ -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" <|