Comparison of programming languages (list comprehension)

Comparison of programming languages (list comprehension)
Programming language comparisons
General comparison
Basic syntax
Basic instructions
Arrays
Associative arrays
String operations
String functions
List comprehension
Object-oriented programming
Object-oriented constructors
Database access
Database RDBMS

Evaluation strategy
List of "hello world" programs

ALGOL 58's influence on ALGOL 60
ALGOL 60: Comparisons with other languages
Comparison of ALGOL 68 and C++
ALGOL 68: Comparisons with other languages
Compatibility of C and C++
Comparison of Pascal and Borland Delphi
Comparison of Object Pascal and C
Comparison of Pascal and C
Comparison of Java and C++
Comparison of C# and Java
Comparison of C# and Visual Basic .NET
This box: view · talk · edit


Contents

List Comprehensions

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 (set comprehension.) as distinct from the use of map and filter functions.

Boo

List with all the doubles from 0 to 10 (exclusive)

doubles = [i*2 for i in range(10)]

List with the names of the customers based on Rio de Janeiro

rjCustomers = [customer.Name for customer in customers if customer.State == "RJ"]

C#

var ns = from x in Enumerable.Range(0,100)
         where x*x > 3
         select x*2;

The previous code is syntactic sugar for the following code written using lambda expressions:

var ns = Enumerable.Range(0, 100)
        .Where(x => x*x > 3)
        .Select(x => x*2);

Clojure

An infinite lazy sequence:

(for [x (iterate inc 0) :when (> (* x x) 3)] (* 2 x))

A list comprehension using multiple generators:

(for [x (range 20), y (range 20), z (range 20) :when (== (+ (* x x) (* y y)) (* z z))] [x y z])

Common Lisp

List comprehensions can be expressed with the loop macro's collect keyword. Conditionals are expressed with if, as follows:

(loop for x from 0 to 100 if (> (* x x) 3) collect (* 2 x))

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 [where CONDITION] 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.

Erlang

L = lists:seq(0,100).
S = [2*X || X <- L, X*X > 3].

F#

seq { for x in 0 .. 100 do if x*x > 3 then yield 2*x }

Or, more correctly for floating point values

seq { for x in 0.0 .. 100.0 do if x**2.0 > 3.0 then yield 2.0*x }

Groovy

(0..100).findAll{ x -> x * x > 3 }.collect { x -> 2 * x }

Haskell

[x * 2 | x <- [0 .. 99], x * x > 3]

An example of a list comprehension using multiple generators:

pyth = [(x,y,z) | x <- [1..20], y <- [x..20], z <- [y..20], x^2 + y^2 == z^2]

JavaScript

Borrowing from Python, JavaScript 1.7 and later have array comprehensions.[1] Although this feature has been proposed for inclusion in the fourth edition ECMAScript standard, Mozilla is the only implementation that currently supports it.

/* There is no "range" function in JavaScript's standard
   library, so the application must provide it. */
function range(n) {
  for (var i = 0; i < n; i++)
    yield i;
}
 
[2 * x for (x in range(100)) if (x * x > 3)]

JavaScript 1.8 adds Python-like generator expressions.

Mythryl

 s = [ 2*i for i in 1..100 where i*i > 3 ];

Multiple generators:

 pyth = [ (x,y,z) for x in 1..20 for y in x..20 for z in y..20 where x*x + y*y == z*z ];

Nemerle

$[x*2 | x in [0 .. 100], x*x > 3]

Objective Caml

OCaml supports List comprehension through OCaml Batteries. [2]

Perl 6

my @s = ($_ * 2 if $_ ** 2 > 3 for 0 .. 99);

Python

Python uses the following syntax to express list comprehensions over finite lists:

S = [2*x for x in range(100) if x**2 > 3]

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:

from itertools import count
S = (2*x for x in count() if x**2 > 3)

(Subsequent use of the generator expression will determine when to stop generating values).

R

x = (0:100)
S = 2 * x[x ^ 2 > 3]

Racket

(for/list ([x (in-range 100)] #:when (> (* x x) 3)) (* x 2))

An example with multiple generators:

(for*/list ([x (in-range 1 21)] [y (in-range 1 21)] [z (in-range 1 21)]
            #:when (= (+ (* x x) (* y y)) (* z z)))
  (list x y z))

Scala

Using the for-comprehension:

val s = for (x <- Stream.from(0); if x*x > 3) yield 2*x

Scheme

List comprehensions are supported in Scheme through the use of the SRFI-42 library.[3]

(list-ec (: x 100) (if (> (* x x) 3)) (* x 2))

An example of a list comprehension using multiple generators:

(list-ec (: x 1 21) (: y x 21) (: z y 21) (if (= (+ (* x x) (* y y)) (* z z))) (list x y z))

Smalltalk

((1 to: 100) select: [ x | x squared > 3 ]) collect: [ x | x * 2 ]


Visual Prolog

S = [ 2*X || X = list::getMember_nd(L), X*X > 3 ]

Windows PowerShell

$s = ( 0..100 | ? {$_*$_ -gt 3} | % {2*$_} )

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Comparison of programming languages — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (basic instructions) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (mapping) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (array) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (syntax) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Comparison of programming languages (strings) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • List comprehension — A 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 (set comprehension) as distinct from the use of map… …   Wikipedia

  • Comparison of programming languages (string functions) — String functions redirects here. For string functions in formal language theory, see String operations. Programming language comparisons General comparison Basic syntax Basic instructions Arrays …   Wikipedia

  • Python (programming language) — infobox programming language name = Python paradigm = multi paradigm: object oriented, imperative, functional year = 1991 designer = Guido van Rossum developer = Python Software Foundation latest release version = 2.6 latest release date =… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”