Use map/filter with built-in functions for performance#1237
Merged
Conversation
…dules Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Replace map, reduce, and lambda with list comprehension
Replace map/filter with list comprehensions
Jan 30, 2026
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…ormance Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot
AI
changed the title
Replace map/filter with list comprehensions
Use map/filter with built-in functions for performance optimization
Jan 30, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR converts simple list comprehensions to list(map()) calls with built-in functions in four files. However, the PR title "Replace map/filter with list comprehensions" contradicts the actual direction of changes (which replace comprehensions WITH map).
Changes:
- Converted
[str(i) for i in range(...)]tolist(map(str, range(...)))in date utilities - Converted
[i.strip() for i in ...]patterns tolist(map(str.strip, ...))in spell correction and sentence segmentation - Converted
[tok.lower() for tok in toks]tolist(map(str.lower, toks))in text preprocessing
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| pythainlp/util/date.py | Changed date range generation from list comprehension to map() for converting numbers to strings, creating inconsistency with the adjacent list comprehension on line 92 |
| pythainlp/ulmfit/preprocess.py | Converted lowercase transformation from list comprehension to map() in the lowercase_all function |
| pythainlp/tokenize/thaisumcut.py | Changed sentence stripping operation from list comprehension to map() in sentence segmentation logic |
| pythainlp/spell/words_spelling_correction.py | Converted word list reading from list comprehension to map() when loading spelling correction data |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot
AI
changed the title
Use map/filter with built-in functions for performance optimization
Optimize performance: use map/filter with built-in functions
Jan 30, 2026
…rings Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot
AI
changed the title
Optimize performance: use map/filter with built-in functions
Revert incorrect filter(None) optimization in ulmfit preprocessing
Jan 30, 2026
Co-authored-by: bact <128572+bact@users.noreply.github.com>
|
Copilot
AI
changed the title
Revert incorrect filter(None) optimization in ulmfit preprocessing
Use map/filter with built-in functions for performance
Jan 30, 2026
bact
approved these changes
Jan 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What does this changes
Replaces list comprehensions with
map()andfilter()when calling built-in functions or methods. Removes redundant falsy value checks.Optimized patterns:
Files modified:
What was wrong
List comprehensions with built-in functions have Python bytecode overhead. Built-in callables (
str,int,str.strip,str.lower) are C implementations—passing them directly tomap()avoids loop machinery. Similarly,filter(None, ...)is faster than comprehensions for removing falsy values.In
tokenize/thaisumcut.py, manually checking for empty strings beforefilter(None, ...)was redundant sincefilter(None)already removes them.How this fixes it
Direct function references to
map()andfilter()execute in C with lazy evaluation. No lambda needed—performance gain without semantic change. List comprehensions retained only where lambda would be required or complex logic involved.Your checklist for this pull request
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.