# Elementwise 0.120116 -//- beta release -//- Lazily compute functions,method calls and operations on

Discussion in 'Python' started by Nathan Rice, Jan 16, 2012.

1. ### Nathan RiceGuest

Elementwise provides helpful proxy objects which let you perform a
series of computations on every element of an iterable or graph, in a
lazy manner.

Docs: http://packages.python.org/elementwise/
GitHub: https://github.com/nathan-rice/Elementwise
Examples:

The standard ElementwiseProxy:

>>> nums = ElementwiseProxy([1, 2, 3, 4)
>>> print nums.bit_length()

1, 2, 2, 3
>>> nums + 1

2, 3, 4, 5
>>> print nums * 2

2, 4, 6, 8
>>> print nums == 2

False, True, False, False
>>> print ((nums + 1) * 2 + 3).apply(float)

7.0, 9.0, 11.0, 13.0
>>> print (nums.apply(float) + 0.0001).apply(round, 2)

1.0, 2.0, 3.0, 4.0
>>> print abs(nums - 3)

2, 1, 0, 1
>>> print (nums * 2 + 3) / 4
>>> print efoo2.undo(3)

1, 2, 3, 4
>>> print ((nums * 2 + 3) / 4).replicate([2, 2, 3, 3])

1, 1, 2, 2
>>> words = ElementwiseProxy(["one", "two", "three", "four"])
>>> print (words + " little indians").upper().split("

").apply(reversed).apply("_".join) * 2
'INDIANS_LITTLE_ONEINDIANS_LITTLE_ONE',
'INDIANS_LITTLE_TWOINDIANS_LITTLE_TWO',
'INDIANS_LITTLE_THREEINDIANS_LITTLE_THREE',
'INDIANS_LITTLE_FOURINDIANS_LITTLE_FOUR'

The PairwiseProxy:

>>> nums = PairwiseProxy([1, 2, 3, 4])
>>> nums + [1, 2, 3, 4]

2, 4, 6, 8
>>> nums * [2, 2, 3, 3]

2, 4, 9, 12
>>> nums == [2, 2, 3, 5]

False, True, True, False
>>> (nums.apply(float) / itertools.count(2) +

itertools.count(1)).apply(round, args=itertools.repeat([2]))
1.5, 2.67, 3.75, 4.8
>>> abs(nums - [3, 2, 1, 1])

2, 0, 2, 3
>>> (nums * [2, 2, 1, 5] + [3, 5, 9, 0]) / [4, 1, 2, 3]

1, 9, 6, 6
>>> ((nums * itertools.repeat(2) + itertools.repeat(3)) /

itertools.repeat(4)).replicate([2, 2, 3, 3])
1, 0, 0, 0
>>> ((nums * [2, 3, 4, 5]) > [5, 6, 7, 8]) != [True, True, False, True]

True, True, True, False

The RecursiveElementwiseProxy:

>>> treenums = RecursiveElementwiseProxy([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> treenums + 1

((2, 3, 4), (5, 6, 7), (8, 9, 10))
>>> treenums * 2

((2, 4, 6), (8, 10, 12), (14, 16, 18))
>>> (treenums * 2 + 1).apply(float)

((3.0, 5.0, 7.0), (9.0, 11.0, 13.0), (15.0, 17.0, 19.0))

Feedback is welcome.

Nathan Rice, Jan 16, 2012