E01081: Python list comprehensions do not exist in EK9 - use stream pipelines
What went wrong
Python list comprehensions do not exist in EK9.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 1066 (Does EK9 have list comprehensions like Python?)
Rationale
EK9 uses stream pipelines instead of list comprehensions:
- More composable and reusable
- Clearer separation of operations
- Supports lazy evaluation
- Works consistently with all collection types
How to fix it
Use 'cat collection | operations | collect as List of Type' pattern. The 'cat' command starts a stream, pipeline operations transform it, and 'collect' gathers results into a new collection.
The [EXAMPLE] section below shows the comprehensions rejected, and the same squares and evens computed by 'map with' and 'filter by' over named functions.
See also
- E01077: 'def' not supported (EK9 uses function name directly)
- E01078: 'elif' not supported (use 'else if')
- E01079: 'None' not supported (use tri-state semantics)
- E01080: 'self' not supported (use 'this')
- E01081: list comprehensions not supported (use stream pipelines)
https://ek9.io/streamsAndPipelines.html https://ek9.io/forAI.json#$.excludedFeatures.features[?(@.keyword=="listComprehension")]
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E01081: Python list comprehensions do not exist in EK9 - use stream pipelines
#!ek9 defines module error.e01081.bad defines function //Simple list comprehension trySimpleListComprehension() numbers <- [1, 2, 3, 4, 5] squares <- [x * x for x in numbers] //List comprehension with filter tryListComprehensionWithFilter() numbers <- [1, 2, 3, 4, 5] evens <- [x for x in numbers if x mod 2 == 0] //List comprehension identity tryListComprehensionIdentity() items <- ["a", "b", "c"] copy <- [item for item in items]
Fixed: Use a stream pipeline: cat, then map or filter, collected into a List
#!ek9 defines module error.e01081.good defines function square() -> number as Integer <- rtn as Integer: number * number isEven() -> number as Integer <- rtn as Boolean: number mod 2 == 0 squares() -> numbers as List of Integer <- rtn as List of Integer: cat numbers | map with square | collect as List of Integer evens() -> numbers as List of Integer <- rtn as List of Integer: cat numbers | filter by isEven | collect as List of Integer
Questions and answers
These Q&A entries show E01081 and its fix: