jsuder-xx / elm-review-reducible-lambdas / NoEtaReducibleLambdas

Provides elm-review rules to detect reducible lambda expressions using different techniques.

rule : Config -> Review.Rule.Rule

This rule detect reducible lambda expressions using different techniques.

Fail

example1 =
    List.map (\a -> f a)

example2 =
    List.map (\a -> f (g 10) a)

Success

example1 =
    List.map f

example2 =
    List.map (f (g 10))

When (not) to enable this rule

This rule can change the performance characteristics of your program which is why LambdaReduceStrategy offers different levels of reduction.

Configuration

module ReviewConfig exposing (config)

import NoEtaReducibleLambdas
import Review.Rule exposing (Rule)


-- Below is an example of the strongest configuration. It will reduce the most.
config : List Rule
config =
    [ NoEtaReducibleLambdas.rule
        { lambdaReduceStrategy = NoEtaReducibleLambdas.AlwaysRemoveLambdaWhenPossible
        , argumentNamePredicate = always True
        }
    ]

-- Conservative configuration: Only reduces single argument lambdas with a single letter argument name.
conservativeConfig : List Rule
conservativeConfig =
    [ NoEtaReducibleLambdas.rule
        { lambdaReduceStrategy = NoEtaReducibleLambdas.OnlyWhenSingleArgument
        , argumentNamePredicate = \argumentName -> String.length argumentName <= 1
        }
    ]

Try it out

You can try this rule out by running the following command:

elm-review --template jsuder-xx/elm-review-eta-reduction/example --rules NoEtaReducibleLambdas


type LambdaReduceStrategy
    = RemoveLambdaWhenNoCallsInApplication
    | OnlyWhenSingleArgument
    | AlwaysRemoveLambdaWhenPossible

Control how aggressively lambda expressions are reduced.

See the module documentation.


type alias Config =
{ argumentNamePredicate : String -> Basics.Bool
, lambdaReduceStrategy : LambdaReduceStrategy 
}

Configuration options for determining under what circumstances this rule will perform a reduction.

canRemoveLambda : ErrorMessage

Error message when the lambda can be removed (for unit testing only).

canRemoveSomeArguments : ErrorMessage

Error message when arguments can be removed (for unit testing only).

reducesToIdentity : ErrorMessage

Error message when the lambda reduces to identity (for unit testing only).