question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid map/filter with lambda

See original GitHub issue

Rule request

Thesis

Forbid using lambda inside of map and filter.

# bad
map(lambda x: x*2, lst)
# good
(x*2 for x in lst)

# bad
filter(lambda x: x%2, lst)
# good
(x for x in lst if x%2)

# using functions instead of lambdas is ok
map(str, lst)
str(str.islower, lst)

Reasoning

Generator expressions are good, easier to read, faster, and even shorter.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Jrryycommented, Oct 5, 2020

I don’t say that lambda is good, use it everywhere. I say, that sometimes they are more suitable than generators or functions.

I totally agree with this statement. There are cases in which using lambdas makes your code cleaner, more readable, and even faster. There are also cases in which using them transforms you code into a mess of complicated functional nonsense. Finding a balance between lambdas, generators, and functions, is a must to write good code, but simply forbidding their usage will not necessarily improve someone’s code.

In my humble opinion, the usage of lambdas depends on the context and complexity of the code and thus forbidding it might be too strict for a big amount of developers.

1reaction
sobolevncommented, Oct 2, 2020

@AlwxSin consider using real monads instead! (just joking 🙂 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda Functions in Python — Filter, Map & Reduce
A lambda function is an anonymous function with syntax lambda args: expression . The expression is executed on the arguments and the result...
Read more >
Lambda event filtering - AWS Documentation
Use event filtering with a Lambda event source mapping to filter out stream and queue events to your Lambda function based on certain...
Read more >
Functions in Python along with Map, Filter, and Lambda ...
A filter is a function that can be applied to a list that will return only the values that pass a certain condition....
Read more >
4. Lambda Operator, filter, reduce and map - Python Courses eu
The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw- ......
Read more >
List comprehension vs. lambda + filter - python - Stack Overflow
The first is the function call overhead: as soon as you use a Python function (whether created by def or lambda ) it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found