Most viewed recipes tagged "lambda"http://code.activestate.com/recipes/tags/lambda/views/2017-04-27T21:26:00-07:00ActiveState Code RecipesClassifying characters using nested conditional expressions (Python)
2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/
<p style="color: grey">
Python
recipe 580792
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>).
</p>
<p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p>
<p>print n, 'is odd' if n % 2 == 1 else 'is even'</p>
<p>Here, the conditional expression is this part of the print statement above:</p>
<p>'is odd' if n % 2 == 1 else 'is even'</p>
<p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p>
<p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p>
<p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</p>
List comparison, difference and more using set & frozenset (Python)
2012-11-01T10:30:58-07:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578310-list-comparison-difference-and-more-using-set-froz/
<p style="color: grey">
Python
recipe 578310
by <a href="/recipes/users/4181178/">Scott S-Allen</a>
(<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/contain/">contain</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/union/">union</a>).
</p>
<p>Python has a powerful suite of tools for comparing lists by way of sets and frozensets. Here are a few examples and conveniences that many newcomers, even a few seasoned developers, are unaware.</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>
Password Generator (Short Code) (Python)
2011-11-18T03:02:13-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577949-password-generator-short-code/
<p style="color: grey">
Python
recipe 577949
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/alphabet/">alphabet</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>, <a href="/recipes/tags/password_generator/">password_generator</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/short_code/">short_code</a>).
Revision 2.
</p>
<p>This recipe generates a pseudo-random password of a prescribed length. It also lets you specify what characters are not permitted in the password or specify what characters are.</p>
lambda program (Python)
2009-07-03T22:03:28-07:00nxwhttp://code.activestate.com/recipes/users/4170980/http://code.activestate.com/recipes/576829-lambda-program/
<p style="color: grey">
Python
recipe 576829
by <a href="/recipes/users/4170980/">nxw</a>
(<a href="/recipes/tags/lambda/">lambda</a>).
</p>
<p>An attempt to write a whole program inside a python lambda.</p>
Name a lambda (Python)
2014-07-02T20:54:01-07:00David Weilhttp://code.activestate.com/recipes/users/1296670/http://code.activestate.com/recipes/578902-name-a-lambda/
<p style="color: grey">
Python
recipe 578902
by <a href="/recipes/users/1296670/">David Weil</a>
(<a href="/recipes/tags/func_name/">func_name</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/naming/">naming</a>).
</p>
<p>A very simple recipe to allow you <em>easily</em> name lambda-objects (or other kind of objects, callable, for example, partial objects) you create, with little overhead and friendly syntax.</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>
Blocks for python (Python)
2011-10-24T22:26:07-07:00yoav glaznerhttp://code.activestate.com/recipes/users/4178625/http://code.activestate.com/recipes/577920-blocks-for-python/
<p style="color: grey">
Python
recipe 577920
by <a href="/recipes/users/4178625/">yoav glazner</a>
(<a href="/recipes/tags/block/">block</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/style/">style</a>).
</p>
<p>A simple <strong>block</strong> function that lets one send multi line blocks of code to a function
it doesn't really act like a <em>normal</em> def/lambda but offers cool style</p>
Another use for lambdas and function references (Python)
2009-02-27T06:20:33-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576672-another-use-for-lambdas-and-function-references/
<p style="color: grey">
Python
recipe 576672
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/function_reference/">function_reference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/loop/">loop</a>).
</p>
<p>I was writing code to do bootstrapping on a set of data. I wanted a test case where if I asked for one bootstrap I would be returned the original data. lambdas and function references saved me from inefficient code.</p>