Top-rated recipes tagged "functional"http://code.activestate.com/recipes/tags/functional/top/2012-10-06T22:15:17-07:00ActiveState Code RecipesDirt simple map/reduce (Python)
2011-05-15T16:46:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577676-dirt-simple-mapreduce/
<p style="color: grey">
Python
recipe 577676
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/crosstab/">crosstab</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map_reduce/">map_reduce</a>, <a href="/recipes/tags/pivot_table/">pivot_table</a>, <a href="/recipes/tags/statistics/">statistics</a>).
Revision 9.
</p>
<p>Simple tool for analyzing datasets.</p>
Recursive Functional State Machine (Python)
2011-05-20T13:46:48-07:00Stefan Tunschhttp://code.activestate.com/recipes/users/4178042/http://code.activestate.com/recipes/577709-recursive-functional-state-machine/
<p style="color: grey">
Python
recipe 577709
by <a href="/recipes/users/4178042/">Stefan Tunsch</a>
(<a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/state_machine/">state_machine</a>).
</p>
<p>This is a simple state machine that takes a functional approach.
It requires trampoline from pysistence.func to avoid the recursion limit.</p>
<p>Namedtuples are used to define the different states.
globals() is used to reference the states. (This could also be done putting states into a separate module and getting them through getattr.)</p>
<p>In this recipe the functions called in the different states need to return a boolean, which defines the success or failure event.</p>
fmap(): a kind of inverse of the built-in Python map() function (Python)
2012-10-06T22:15:17-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578281-fmap-a-kind-of-inverse-of-the-built-in-python-map-/
<p style="color: grey">
Python
recipe 578281
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/composition/">composition</a>, <a href="/recipes/tags/fmap/">fmap</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map/">map</a>).
</p>
<p>The Python map() function returns a list of the results of applying the function to the items of the argument sequence(s).</p>
<p>The fmap() function does the inverse, in a sense. It returns the result of applying a list of functions to a given argument.</p>
Recursively defined, Haskell-style infinite lists (Python)
2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/
<p style="color: grey">
Python
recipe 578119
by <a href="/recipes/users/4181975/">John Crichton</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>).
Revision 2.
</p>
<p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p>
<p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p>
indefinite currying decorator with greedy call and up front args checking (Python)
2011-10-28T22:05:55-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/577928-indefinite-currying-decorator-with-greedy-call-and/
<p style="color: grey">
Python
recipe 577928
by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a>
(<a href="/recipes/tags/curry/">curry</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>).
Revision 2.
</p>
<p>Decorator makes function currying as long as there are more correct arguments to take and fires it as soon as there is enough to call, also checks arguments up front for errors.</p>
Python Infinite Rotations and Tails (Python)
2011-02-13T15:27:42-08:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/577574-python-infinite-rotations-and-tails/
<p style="color: grey">
Python
recipe 577574
by <a href="/recipes/users/4060075/">Thomas Ahle</a>
(<a href="/recipes/tags/cycles/">cycles</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/rotations/">rotations</a>, <a href="/recipes/tags/tails/">tails</a>).
</p>
<p>This is an example of how functional ideas of infinite lists can be used in python to optimize memory usage of certain problems.</p>
Binary search function. (Python)
2011-02-07T06:37:52-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577565-binary-search-function/
<p style="color: grey">
Python
recipe 577565
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/binary_search/">binary_search</a>, <a href="/recipes/tags/bsearch/">bsearch</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lower_bound/">lower_bound</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/upper_bound/">upper_bound</a>).
Revision 3.
</p>
<p>For a number of years Python has provided developers with the special parameters 'cmp' and 'key' on list.sort and __builtin__.sorted. However Python does not provide a built-in mechanism for doing binary searches on such sorted lists. This recipe provides a simple function that allows you to perform binary searches on your sorted sequences.</p>
Sudoku solver in functional-programming style (Python)
2010-04-23T12:35:13-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577188-sudoku-solver-in-functional-programming-style/
<p style="color: grey">
Python
recipe 577188
by <a href="/recipes/users/4173270/">Arnau Sanchez</a>
(<a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/sudoku/">sudoku</a>).
Revision 10.
</p>
<p>A simple brute-force Sudoku solver written in functional-programming style. This code is not aimed for speed, the goal is to write a clear, compact and (hopefully) pedagogical functional solution.</p>
Functional selection sort (Python)
2009-09-29T13:34:35-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/576917-functional-selection-sort/
<p style="color: grey">
Python
recipe 576917
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/selection/">selection</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>).
Revision 3.
</p>
<p>This is a variant of selection sort without using for-statements. Do you like it? :-)</p>
compare(), making filter() fun again (Python)
2008-10-04T12:40:42-07:00Andreas Nilssonhttp://code.activestate.com/recipes/users/4167183/http://code.activestate.com/recipes/576526-compare-making-filter-fun-again/
<p style="color: grey">
Python
recipe 576526
by <a href="/recipes/users/4167183/">Andreas Nilsson</a>
(<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>).
Revision 2.
</p>
<p>compare() takes a function parameter and returns a callable comparator when compared to a value. When called, the comparator returns result of comparing the result of calling the function and the value it was created with.</p>
<p>Basic usage:</p>
<pre class="prettyprint"><code>items = [1, 2, 3, 4, 5]
def double(x):
return x * 2
for i in filter(compare(double) > 5, items):
print i #Prints 3, 4 and 5
</code></pre>