Functional lang – common list operations – python, STL, LINQ …

http://www.ibm.com/developerworks/java/library/j-ft2/index.html shows a fundamental Functional Programming technique — A) apply a pure function (possibly a closure[1]) on successive elements of a sequence, B) and then perform accumulation, filter/grep, reduce etc across the entire sequence. Here I described it as a 2-stepper but you do not have to feel this way.

Python offers various elements of this strategy — list comprehension, apply(), filter(), map(), reduce(),  … but are they used with pure functions?

FP favors immutable objects. Basically,

      tuples + list operations in (B) = a major component of FP

Sorry about the loose language… Mine is very different from the academic/textbook presentation of FP, but in terms of techniques, mine is not too far.

STL offers many similar operations to (B), esp. with the *_copy and *_if algorithms in http://bigblog.tanbin.com/2012/04/stl-algos-and-their-if-and-copy.html
– find if
– count if
– remove_copy
– replace_copy

[1] I feel a closure qualifies as a pure function even though the output depends on some data in the “environment”.

I’ll stick my neck out and say that STL iterator concept is a defining feature(?) of a sequence.

Advertisements

One thought on “Functional lang – common list operations – python, STL, LINQ …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s