Giter Club home page Giter Club logo

elm-review-always's Introduction

elm-review-always

elm package elm-review 2.0 elm 0.19 Tests

Provides an elm-review rule to forbid the use of always.

Use an anonymous function \_ -> instead of always.

It's more concise, more recognizable as a function, and makes it easier to change your mind later and name the argument.

-- Don't do this --
List.map (always 0) [ 1, 2, 3, 4 ]

-- Instead do this --
List.map (\_ -> 0) [ 1, 2, 3, 4 ]

Example configuration

import NoAlways
import Review.Rule exposing (Rule)


config : List Rule
config =
    [ NoAlways.rule
    ]

Caution: Heavy Computation

If the value you always want is the result of some heavy computation then you will not want that within an anonymous function as the work will be done every time. Instead, do the calculation in a nearby let..in block first.

-- Don't do this --
List.map (always (heavyComputation arg1 arg2)) [ 1, 2, 3, 4 ]

-- Don't do this either --
List.map (\_ -> heavyComputation arg1 arg2) [ 1, 2, 3, 4 ]

-- Instead do this --
let
    heavyComputationResult =
        heavyComputation arg1 arg2
in
List.map (\_ -> heavyComputationResult) [ 1, 2, 3, 4 ]

-- This works too (but is less readable) --
List.map ((\value _ -> value) (heavyComputation arg1 arg2)) [ 1, 2, 3, 4 ]

Try it out

You can try the example configuration above out by running the following command:

elm-review --template sparksp/elm-review-always/example

elm-review-always's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar sparksp avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

elm-review-always's Issues

Simplify Fix

Since elm-review applies elm-format to any fixed files, it's not necessary to track the parens.

Include heavy computation warning in report

-- `always` is not allowed.
always something

-- You should replace this `always` with an anonymous function `\\_ ->`.
--
-- It's more concise, more recognizable as a function, and makes it easier to
-- change your mind later and name the argument.
--
-- Caution: If `something` does some heavy computation then you may not want that
-- within an anonymous function as the work will be done every time. Instead, do
-- the calculation in a nearby let..in block first.

Do not include the caution for literals...

always "literal"
always 'c'
always 3.14159265359
always Nothing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.