MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1＆1

# Python, Ruby, Perl: Apply a Function to a List

Xah Lee, , …,

## Python

### Removing Elements in a List

To remove elements in a list that satisfies some criterion, use the function `filter( ‹testFunction›, ‹list›)`. The “testFunction” will be applied to each element in the list. If `testFunction(‹element›)` returns False, then that element will not be in the resulting list.

```# -*- coding: utf-8 -*-
# python

aa = range(11)                  # list 0 to 10

def ff(n): return n % 2 == 0    # return True if divisible by 2

bb = list(filter( ff, aa))  # python 2, “filter” returns a list
# in python 3, “filter” returns iterator object. Use list() to convertt to list.

print(bb2)                      # [0, 2, 4, 6, 8, 10]
```

### Applying a Function to a List

The “map” function applies a function to all elements of a list. Example:

```# -*- coding: utf-8 -*-
# python

def ff(n): return n*n

print (map(ff, range(5)))         # [0, 1, 4, 9, 16]
```

http://docs.python.org/lib/built-in-funcs.html

## Ruby

### Removing Elements in a List

To remove elements, use “select” method and pass it a block.

In Ruby, many methods can take a “block” argument. The “block” is a kind of lambda expression. The syntax has the form: `‹object_name›.‹method_name› { |‹dummy_var›| ‹expression›}`. Example:

```# -*- coding: utf-8 -*-
# ruby

aa = 0..10                      # a array 0 to 10

p aa.select { |xx| xx % 2 == 0 }   # ⇒ [0, 2, 4, 6, 8, 10]
```

### Applying a Function to a List

Use “map” method on array object to create a new array. Example:

```# -*- coding: utf-8 -*-
# ruby

def ff(n)
n*n
end

aa = 0..4                       # array 0 to 4

p aa.map {|xx| ff xx}           # ⇒ [0, 1, 4, 9, 16]
```

Use method “map!” to have the original array modified.

## Perl

### Removing Elements in a List

Use “grep” to remove elements in a list. The form is one of:

• `grep {‹true/false function name› \$_} ‹myList›`
• `grep {‹expression on \$_› ;} ‹myList›`

Example:

```# -*- coding: utf-8 -*-
# perl

use Data::Dumper;

sub ff {return \$_[0] % 2 == 0}; # return true if divisible by 2

print Dumper[ grep {ff \$_} (0..10)]; # ⇒  [ 0, 2, 4, 6, 8, 10 ]
```

The `\$_` is a builtin variable that represent a argument given to a subroutine. `\$_[0]` means the first argument. `@_` is the entire arguments as array. The `\$_` is also the default input for regex to match, and in general represents a default argument.

The `(0..10)` generate a list from 0 to 10.

The `%` above is the operator for computing remainder of a division.

The `Data::Dumper` module is to import the “Dumper” function for printing list.

### Applying a Function to a List

Use “map” to apply a function to a list. The basic form is `map {myFunction(\$_)} myList`. It returns a list.

```# -*- coding: utf-8 -*-
# perl

use Data::Dumper; \$Data::Dumper::Indent=0;

sub ff {return (\$_[0])**2;};    # square a number

print Dumper [ map { ff(\$_)} (0..10)];
# ⇒ \$VAR1 = ['0','1','4',9,'16',25,36,49,'64',81,100];
```

The `**` is the exponential operator.