Comparison of programming languages (list comprehension)
List comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation as distinct from the use of map and filter functions.
Examples of list comprehension
Boo
List with all the doubles from 0 to 10doubles =
List with the names of the customers based in Rio de Janeiro
rjCustomers =
C++
C++ can use thestd::views namespace, introduced in C++20.using std::vector;
using std::ranges::to;
using std::views::filter;
using std::views::transform;
vector
| filter
| transform
| to
C#
IEnumerable
where x * x > 3
select x * 2;
The previous code is syntactic sugar for the following code written using lambda expressions:
IEnumerable
.Where
.Select;
Ceylon
Filtering numbers divisible by 3:value divisibleBy3 = ;
// type of divisibleBy3 is Iterable
Multiple "generators":
value triples = ;
// type of triples is Iterable
Clojure
An infinite lazy sequence::when ]
)
A list comprehension using multiple generators:
y
z
:when ) )]
)
CoffeeScript
largeNumbers =
Common Lisp
List comprehensions can be expressed with theloop macro's collect keyword. Conditionals are expressed with if, as follows:)
Cobra
List the names of customers:names = for cust in customers get cust.name
List the customers with balances:
names = for cust in customers where cust.balance > 0
List the names of customers with balances:
names = for cust in customers where cust.balance > 0 get cust.name
The general forms:
for VAR in ENUMERABLE get EXPR
for VAR in ENUMERABLE where CONDITION
Note that by putting the condition and expression after the variable name and enumerable object, editors and IDEs can provide autocompletion on the members of the variable.
Dart
var pyth =
];
Iterable
List.generate;
Elixir
for x <- 0..100, x * x > 3, do: x * 2
Erlang
L = lists:seq.
S = .
F#
Lazily-evaluated sequences:seq
Or, for floating point values
seq
Lists and arrays:
List comprehensions are the part of a greater family of language constructs called computation expressions.
Haskell
, x * x > 3]
An example of a list comprehension using multiple generators:
pyth =, y <-, z <-, x^2 + y^2 z^2]
Io
By using Range object, Io language can create list as easy as in other languages:Range 0 to asList select map
ISLISP
List comprehensions can be expressed with thefor special form. Conditionals are expressed with if, as follows:))
)
))
Julia
Julia supports comprehensions using the syntax:y =
and multidimensional comprehensions like:
z =
It is also possible to add a condition:
v =
And just changing square brackets to the round one, we get a generator:
g =
Mythryl
s = ;Multiple generators:
pyth = ;
Nemerle
$, x*x > 3]
Nim
Nim has built-in seq, set, table and object comprehensions on the sugar standard library module:import sugar
let variable = collect:
for item in @: item + 1
assert variable @
The comprehension is implemented as a macro that is expanded at compile time,
you can see the expanded code using the expandMacro compiler option:
var collectResult = newSeq
for item in items:
add
collectResult
The comprehensions can be nested and multi-line:
import sugar
let values = collect:
for val in :
collect:
for val2 in :
if != :
assert values @, @,
OCaml
OCaml supports [List comprehension through OCaml Batteries.Perl
my @s = map grep 0..99;
Array with all the doubles from 1 to 9 inclusive:
my @doubles = map 1..9;
Array with the names of the customers based in Rio de Janeiro :
my @rjCustomers = map @customers;
Filtering numbers divisible by 3:
my @divisibleBy3 = grep 0..100;
PowerShell
$s =
$s = 0..100 | where-object | foreach-object
Python
Python uses the following syntax to express list comprehensions over finite lists:s: list =
A generator expression may be used in Python versions >= 2.4 which gives lazy evaluation over its input, and can be used with generators to iterate over 'infinite' input such as the count generator function which returns successive integers:
import itertools
from typing import Iterator
s: Iterator =
.
R
x <- 0:100
S <- 2 * x
Racket
)
An example with multiple generators:
]
#:when ) ))
)
Raku
my @s = ;
Scala
Using the for-comprehension:val s = for yield 2*x
Scheme
List comprehensions are supported in Scheme through the use of the SRFI-42 library.)
An example of a list comprehension using multiple generators:
) )) )
SETL
s := ;
Smalltalk
collect: