<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Lambda Expressions - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Lambda Expressions - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 16 May 2026 08:37:47 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/lambda-expressions/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Python RegEx Tutorial – How to use RegEx inside Lambda Expression ]]>
                </title>
                <description>
                    <![CDATA[ It’s possible to use RegEx inside a lambda function in Python. You can apply this to any Python method or function that takes a function as a parameter. Such functions and methods include filter(), map(), any(), sort(), and more. Keep reading as I sh... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-regex-tutorial-how-to-use-regex-inside-lambda-expression/</link>
                <guid isPermaLink="false">66adf1e588723f64bc4313a0</guid>
                
                    <category>
                        <![CDATA[ Lambda Expressions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python 3 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Regex ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Fri, 17 Mar 2023 09:31:41 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/regexinlambda.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>It’s possible to use RegEx inside a lambda function in Python. You can apply this to any Python method or function that takes a function as a parameter. Such functions and methods include <code>filter()</code>, <code>map()</code>, <code>any()</code>, <code>sort()</code>, and more.</p>
<p>Keep reading as I show you how to use regular expressions inside a lambda function.</p>
<h2 id="heading-what-well-cover">What We'll Cover</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-use-regex-inside-the-expression-of-a-lambda-function">How to use RegEx inside the Expression of a Lambda Function</a><ul>
<li><a class="post-section-overview" href="#heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-filter-function">How to use RegEx inside the Expression of a Lambda Function with the <code>filter()</code> Function</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-map-function">How to use RegEx inside the Expression of a Lambda Function with the <code>map()</code> Function</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-sort-method">How to use RegEx inside the Expression of a Lambda Function with the <code>sort()</code> Method</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h2 id="heading-how-to-use-regex-inside-the-expression-of-a-lambda-function">How to use RegEx inside the Expression of a Lambda Function</h2>
<p>The syntax with which a lambda function can take a RegEx as its expression looks like this:</p>
<pre><code class="lang-py"><span class="hljs-keyword">lambda</span> x: re.method(pattern, x)
</code></pre>
<p>Be aware that you have to use the lambda function on something. And that’s where the likes of <code>map()</code>, <code>sort()</code>, <code>filter()</code>, and others come in.</p>
<h3 id="heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-filter-function">How to use RegEx inside the Expression of a Lambda Function with the <code>filter()</code> Function</h3>
<p>The first example I will show you use the <code>filter()</code> function:</p>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> re

fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'mango'</span>, <span class="hljs-string">'banana'</span>, <span class="hljs-string">'cherry'</span>, <span class="hljs-string">'apricot'</span>, <span class="hljs-string">'raspberry'</span>, <span class="hljs-string">'avocado'</span>]
filtered_fruits = filter(<span class="hljs-keyword">lambda</span> fruit: re.match(<span class="hljs-string">'^a'</span>, fruit), fruits)

<span class="hljs-comment"># convert the new fruits to another list and print it</span>
print(list(filtered_fruits)) <span class="hljs-comment"># ['apple', 'apricot', 'avocado']</span>
</code></pre>
<p>In the code above:</p>
<ul>
<li>the <code>filter()</code> takes the lambda function as the function to execute and the <code>fruits</code> list as the iterable</li>
<li>for the expression of the lambda function, it uses the <code>re.match()</code> method of Python RegEx and uses the pattern <code>^a</code> on the argument <code>fruit</code></li>
<li>the last thing I did was convert all items on the list that matches the pattern into a list</li>
</ul>
<h3 id="heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-map-function">How to use RegEx inside the Expression of a Lambda Function with the <code>map()</code> Function</h3>
<p>To use RegEx inside a lambda function with another function like <code>map()</code>, the syntax is similar:</p>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> re

fruits2 = [<span class="hljs-string">'opple'</span>, <span class="hljs-string">'bonono'</span>, <span class="hljs-string">'cherry'</span>, <span class="hljs-string">'dote'</span>, <span class="hljs-string">'berry'</span>]
modified_fruits = map(<span class="hljs-keyword">lambda</span> fruit: re.sub(<span class="hljs-string">'o'</span>, <span class="hljs-string">'a'</span>, fruit), fruits2)

<span class="hljs-comment"># convert the new fruits to another list and print it</span>
print(list(modified_fruits)) <span class="hljs-comment"># ['apple', 'banana', 'cherry', 'date', 'berry']</span>
</code></pre>
<p>In the code above:</p>
<ul>
<li>the <code>modified_fruits</code> is looping through the <code>fruits2</code> list with a <code>map()</code> function</li>
<li>uses the <code>re.sub()</code> method of Python RegEx as the expression of the lambda function. </li>
</ul>
<p>The <code>re.sub</code> method lets you replace the first value with the second one. In the example, it switched all occurrences of <code>o</code> to <code>a</code>.</p>
<h3 id="heading-how-to-use-regex-inside-the-expression-of-a-lambda-function-with-the-sort-method">How to use RegEx inside the Expression of a Lambda Function with the <code>sort()</code> Method</h3>
<p>The last example I will show you uses the <code>sort()</code> method of lists:</p>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> re

fruits = [ <span class="hljs-string">'banana'</span>, <span class="hljs-string">'fig'</span>, <span class="hljs-string">'grapefruit'</span>]

<span class="hljs-comment"># sort fruits based on the number of vowels</span>
fruits.sort(key=<span class="hljs-keyword">lambda</span> x: len(re.findall(<span class="hljs-string">'[aeiou]'</span>, x)))

print(fruits) <span class="hljs-comment">#['fig', 'banana', 'grapefruit']</span>
</code></pre>
<p>In the code, the lambda function sorts the list based on the number of vowels. It does it with the combination of the <code>len()</code> method, the <code>findall()</code> method of Python RegEx, and the pattern <code>[aeiou]</code>.</p>
<p>The word fruit with the lowest number of vowels comes first. If you use <code>reverse=True</code>, it arranges the fruits based on those with the highest number of vowels – descending order:</p>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> re

fruits = [ <span class="hljs-string">'banana'</span>, <span class="hljs-string">'fig'</span>, <span class="hljs-string">'grapefruit'</span>]

<span class="hljs-comment"># sort fruits based on the number of vowels</span>
fruits.sort(key=<span class="hljs-keyword">lambda</span> x: len(re.findall(<span class="hljs-string">'[aeiou]'</span>, x)), reverse=<span class="hljs-literal">True</span>)

print(fruits) <span class="hljs-comment"># ['grapefruit', 'banana', 'fig']</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we looked at how you can pass in RegEx to a lambda function by showing you examples using the <code>filter()</code>, <code>map()</code> functions, and the <code>sort()</code> method.</p>
<p>I hope this article gives you the knowledge you need to use RegEx inside a lambda function.</p>
<p>Keep coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Lambda Function in Python – Example Syntax ]]>
                </title>
                <description>
                    <![CDATA[ Lambda functions are anonymous functions that can contain only one expression. You may think that lambda functions are an intermediate or advanced feature, but here you will learn how you can easily start using them in your code. In Python, functions... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/lambda-function-in-python-example-syntax/</link>
                <guid isPermaLink="false">66b0c39f4d2b90ec4a447a56</guid>
                
                    <category>
                        <![CDATA[ beginner ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Lambda Expressions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ilenia Magoni ]]>
                </dc:creator>
                <pubDate>Mon, 27 Sep 2021 15:23:43 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/pexels-aleksandar-pasaric-4344759.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Lambda functions are anonymous functions that can contain only one expression.</p>
<p>You may think that lambda functions are an intermediate or advanced feature, but here you will learn how you can easily start using them in your code.</p>
<p>In Python, functions are usually created like this:</p>
<pre><code class="lang-pithon">def my_func(a):
  # function body
</code></pre>
<p>You declare them with the <code>def</code> keyword, give them a name, and then add the list of arguments surrounded by round parenthesis. There could be many lines of code, with as many statements and expressions as you need inside.</p>
<p>But sometimes you might need a function with only one expression inside, for example a function that doubles its argument:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">double</span>(<span class="hljs-params">x</span>):</span>
  <span class="hljs-keyword">return</span> x*<span class="hljs-number">2</span>
</code></pre>
<p>This is a function that you can use, for example, with the <code>map</code> method.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">double</span>(<span class="hljs-params">x</span>):</span>
  <span class="hljs-keyword">return</span> x*<span class="hljs-number">2</span>

my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
new_list = list(map(double, my_list))
print(new_list) <span class="hljs-comment"># [2, 4, 6, 8, 10, 12]</span>
</code></pre>
<p>This would be a good place to use a lambda function, as it can be created exactly where you need to use it. This means using fewer lines of code and and you can avoid creating a named function that is only used once (and then has to be stored in memory).</p>
<h2 id="heading-how-to-use-lambda-functions-in-python">How to use lambda functions in Python</h2>
<p>You use lambda functions when you need a small function for a short time – for example as an argument of a higher order function like <code>[map](https://www.freecodecamp.org/news/python-map-function-how-to-map-a-list-in-python-3-0-with-example-code/)</code> or <code>filter</code>. </p>
<p>The syntax of a lambda function is <code>lambda args: expression</code>. You first write the word <code>lambda</code>, then a single space, then a comma separated list of all the arguments, followed by a colon, and then the expression that is the body of the function.</p>
<p>Note that you can't give a name to lambda functions, as they are anonymous (without a name) by definition.</p>
<p>A lambda function can have as many arguments as you need to use, but the body must be one single expression.</p>
<h3 id="heading-example-1">Example 1</h3>
<p>For example, you could write a lambda function that doubles its argument: <code>lambda x: x*2</code>, and use it with the <code>map</code> function to double all elements in a list:</p>
<pre><code class="lang-python">my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
new_list = list(map(<span class="hljs-keyword">lambda</span> x: x*<span class="hljs-number">2</span>, my_list))
print(new_list) <span class="hljs-comment"># [2, 4, 6, 8, 10, 12]</span>
</code></pre>
<p>Notice the difference between this one and the function we wrote above with the <code>double</code> function. This one is more compact, and there is not an extra function occupying space in memory.</p>
<h3 id="heading-example-2">Example 2</h3>
<p>Or you could write a lambda function that checks if a number is positive, like <code>lambda x: x &gt; 0</code>, and use it with <code>filter</code> to create a list of only positive numbers.</p>
<pre><code class="lang-python">my_list = [<span class="hljs-number">18</span>, <span class="hljs-number">-3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">0</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">12</span>]
new_list = list(filter(<span class="hljs-keyword">lambda</span> x: x &gt; <span class="hljs-number">0</span>, my_list))
print(new_list) <span class="hljs-comment"># [18, 5, 12]</span>
</code></pre>
<p>The lambda function is defined where it is used, in this way there is not a named function in memory. If a function is used i only one place it makes sense to use a lambda function to avoid cluttering.</p>
<h3 id="heading-example-3">Example 3</h3>
<p>You can also return a lambda function from a function.</p>
<p>If you ever need to create multiple functions that multiply numbers, for example doubling or tripling and so on, lambda can help.</p>
<p>Instead of creating multiple functions, you could create a function <code>multiplyBy</code> as below, and then call this function multiple times with different arguments to create the functions that double, triple, and so on.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">muliplyBy</span> (<span class="hljs-params">n</span>):</span>
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span> x: x*n

double = multiplyBy(<span class="hljs-number">2</span>)
triple = muliplyBy(<span class="hljs-number">3</span>)
times10 = multiplyBy(<span class="hljs-number">10</span>)
</code></pre>
<p>The lambda function takes the value <code>n</code> from the parent function, so that in <code>double</code> the value of <code>n</code> is <code>2</code>, in <code>triple</code> it is <code>3</code> and in <code>times10</code> it is <code>10</code>. Now calling these functions with an argument will multiply that number.</p>
<pre><code class="lang-python">double(<span class="hljs-number">6</span>)
&gt; <span class="hljs-number">12</span>
triple(<span class="hljs-number">5</span>)
&gt; <span class="hljs-number">15</span>
times10(<span class="hljs-number">12</span>)
&gt; <span class="hljs-number">120</span>
</code></pre>
<p>If you weren't using a lambda function here, you would need to define a different function inside <code>multiplyBy</code>, something like the below:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">muliplyBy</span> (<span class="hljs-params">x</span>):</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">temp</span> (<span class="hljs-params">n</span>):</span>
    <span class="hljs-keyword">return</span> x*n
  <span class="hljs-keyword">return</span> temp
</code></pre>
<p>Using a lambda function uses half the lines and makes it more readable.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Lambda functions are a compact way to write functions if your function includes only one small expression. They are not usually something that beginner coders use, but here you have seen how you can easily use them at any level.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Lambda Expressions in Python ]]>
                </title>
                <description>
                    <![CDATA[ Lambda Expressions Lambda Expressions are ideally used when we need to do something simple and are more interested in getting the job done quickly rather than formally naming the function. Lambda expressions are also known as anonymous functions. Lam... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/lambda-expressions-in-python/</link>
                <guid isPermaLink="false">66c3597bd372f14b49bdcbd8</guid>
                
                    <category>
                        <![CDATA[ Lambda Expressions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 04 Jan 2020 23:41:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9e32740569d1a4ca3bd6.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-lambda-expressions"><strong>Lambda Expressions</strong></h2>
<p>Lambda Expressions are ideally used when we need to do something simple and are more interested in getting the job done quickly rather than formally naming the function. Lambda expressions are also known as anonymous functions.</p>
<p>Lambda Expressions in Python are a short way to declare small and anonymous functions (it is not necessary to provide a name for lambda functions). </p>
<p>Lambda functions behave just like regular functions declared with the <code>def</code> keyword. They come in handy when you want to define a small function in a concise way. They can contain only one expression, so they are not best suited for functions with control-flow statements. </p>
<h3 id="heading-syntax-of-a-lambda-function">Syntax of a Lambda Function</h3>
<p><code>lambda arguments: expression</code></p>
<p>Lambda functions can have any number of arguments but only one expression.</p>
<h3 id="heading-example-code">Example code</h3>
<pre><code class="lang-py"><span class="hljs-comment"># Lambda function to calculate square of a number</span>
square = <span class="hljs-keyword">lambda</span> x: x ** <span class="hljs-number">2</span>
print(square(<span class="hljs-number">3</span>)) <span class="hljs-comment"># Output: 9</span>

<span class="hljs-comment"># Traditional function to calculate square of a number</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">square1</span>(<span class="hljs-params">num</span>):</span>
  <span class="hljs-keyword">return</span> num ** <span class="hljs-number">2</span>
print(square(<span class="hljs-number">5</span>)) <span class="hljs-comment"># Output: 25</span>
</code></pre>
<p>In the above lambda example, <code>lambda x: x ** 2</code> yields an anonymous function object which can be associated with any name. So, we associated the function object with <code>square</code>. So from now on we can call the <code>square</code> object like any traditional function, for example <code>square(10)</code></p>
<h2 id="heading-examples-of-lambda-functions"><strong>Examples of lambda functions</strong></h2>
<h3 id="heading-beginner"><strong>Beginner</strong></h3>
<pre><code class="lang-py">lambda_func = <span class="hljs-keyword">lambda</span> x: x**<span class="hljs-number">2</span> <span class="hljs-comment"># Function that takes an integer and returns its square</span>
lambda_func(<span class="hljs-number">3</span>) <span class="hljs-comment"># Returns 9</span>
</code></pre>
<h3 id="heading-intermediate"><strong>Intermediate</strong></h3>
<pre><code class="lang-py">lambda_func = <span class="hljs-keyword">lambda</span> x: <span class="hljs-literal">True</span> <span class="hljs-keyword">if</span> x**<span class="hljs-number">2</span> &gt;= <span class="hljs-number">10</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">False</span>
lambda_func(<span class="hljs-number">3</span>) <span class="hljs-comment"># Returns False</span>
lambda_func(<span class="hljs-number">4</span>) <span class="hljs-comment"># Returns True</span>
</code></pre>
<h3 id="heading-complex"><strong>Complex</strong></h3>
<pre><code class="lang-py">my_dict = {<span class="hljs-string">"A"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"B"</span>: <span class="hljs-number">2</span>, <span class="hljs-string">"C"</span>: <span class="hljs-number">3</span>}
sorted(my_dict, key=<span class="hljs-keyword">lambda</span> x: my_dict[x]%<span class="hljs-number">3</span>) <span class="hljs-comment"># Returns ['C', 'A', 'B']</span>
</code></pre>
<h2 id="heading-use-case">Use-case</h2>
<p>Let’s say you want to filter out odd numbers from a <code>list</code>. You could use a <code>for</code> loop:</p>
<pre><code class="lang-python">my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>]
filtered = []

<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> my_list:
     <span class="hljs-keyword">if</span> num % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>:
         filtered.append(num)

print(filtered)      <span class="hljs-comment"># Python 2: print filtered</span>
<span class="hljs-comment"># [1, 3, 5, 7, 9]</span>
</code></pre>
<p>Or you could write this as a one liner with list-comprehensions:</p>
<pre><code class="lang-python">filtered = [x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>] <span class="hljs-keyword">if</span> x % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>]
</code></pre>
<p>But you might be tempted to use the built-in <code>filter</code> function. Why? The first example is a bit too verbose and the one-liner can be harder to understand. But <code>filter</code> offers the best of both words. What is more, the built-in functions are usually faster.</p>
<pre><code class="lang-python">my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>]

filtered = filter(<span class="hljs-keyword">lambda</span> x: x % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>, my_list)

list(filtered)
<span class="hljs-comment"># [1, 3, 5, 7, 9]</span>
</code></pre>
<p>NOTE: in Python 3 built in functions return generator objects, so you have to call <code>list</code>. In Python 2, on the other hand, they return a <code>list</code>, <code>tuple</code>or <code>string</code>.</p>
<p>So what happened? You told <code>filter</code> to take each element in <code>my_list</code> and apply the lambda expressions. The values that return <code>False</code> are filtered out.</p>
<h4 id="heading-more-information"><strong>More Information:</strong></h4>
<ul>
<li><a target="_blank" href="https://docs.python.org/3/reference/expressions.html#lambda">Official Docs</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to start working with Lambda Expressions in Java ]]>
                </title>
                <description>
                    <![CDATA[ By Luis Santiago Before Lambda expressions support was added by JDK 8, I’d only used examples of them in languages like C# and C++. Once this feature was added to Java, I started looking into them a bit closer. The addition of lambda expressions adds... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-these-4-things-and-working-with-lambda-expressions-b0ab36e0fffc/</link>
                <guid isPermaLink="false">66c359d8f83dfae169b2c012</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Lambda Expressions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 24 Dec 2017 04:53:23 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*_Ascfro4BseNIKWOJ06MwQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luis Santiago</p>
<p>Before Lambda expressions support was added by JDK 8, I’d only used examples of them in languages like C# and C++.</p>
<p>Once this feature was added to Java, I started looking into them a bit closer.</p>
<p>The addition of lambda expressions adds syntax elements that increase the expressive power of Java. In this article, I want to focus on foundational concepts you need to get familiar with so you can start adding lambda expressions to your code today.</p>
<h4 id="heading-quick-introduction">Quick Introduction</h4>
<p>Lambda expressions take advantage of parallel process capabilities of multi-core environments as seen with the support of pipeline operations on data in the Stream API.</p>
<p>They are anonymous methods (methods without names) used to implement a method defined by a functional interface. It’s important to know what a functional interface is before getting your hands dirty with lambda expressions.</p>
<h4 id="heading-functional-interface">Functional interface</h4>
<p>A functional interface is an interface that contains one and only one abstract method.</p>
<p>If you take a look at the definition of the Java standard <a target="_blank" href="https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html">Runnable interface</a>, you will notice how it falls into the definition of functional interface because it only defines one method: <code>run()</code>.</p>
<p>In the code sample below, the method <code>computeName</code> is implicitly abstract and is the only method defined, making MyName a functional interface.</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyName</span></span>{
  <span class="hljs-function">String <span class="hljs-title">computeName</span><span class="hljs-params">(String str)</span></span>;
}
</code></pre>
<h4 id="heading-the-arrow-operator">The Arrow Operator</h4>
<p>Lambda expressions introduce the new arrow operator <code>-&gt;</code> into Java. It divides the lambda expressions in two parts:</p>
<pre><code>(n) -&gt; n*n
</code></pre><p>The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.</p>
<p>The right side is the lambda body which specifies the actions of the lambda expression. It might be helpful to think about this operator as “becomes”. For example, “n becomes n*n”, or “n becomes n squared”.</p>
<p>With functional interface and arrow operator concepts in mind, you can put together a simple lambda expression:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">NumericTest</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">computeTest</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span></span>; 
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
    NumericTest isEven = (n) -&gt; (n % <span class="hljs-number">2</span>) == <span class="hljs-number">0</span>;
    NumericTest isNegative = (n) -&gt; (n &lt; <span class="hljs-number">0</span>);

    <span class="hljs-comment">// Output: false</span>
    System.out.println(isEven.computeTest(<span class="hljs-number">5</span>));

    <span class="hljs-comment">// Output: true</span>
    System.out.println(isNegative.computeTest(-<span class="hljs-number">5</span>));
}
</code></pre>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyGreeting</span> </span>{
    <span class="hljs-function">String <span class="hljs-title">processName</span><span class="hljs-params">(String str)</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
    MyGreeting morningGreeting = (str) -&gt; <span class="hljs-string">"Good Morning "</span> + str + <span class="hljs-string">"!"</span>;
    MyGreeting eveningGreeting = (str) -&gt; <span class="hljs-string">"Good Evening "</span> + str + <span class="hljs-string">"!"</span>;

      <span class="hljs-comment">// Output: Good Morning Luis! </span>
    System.out.println(morningGreeting.processName(<span class="hljs-string">"Luis"</span>));

    <span class="hljs-comment">// Output: Good Evening Jessica!</span>
    System.out.println(eveningGreeting.processName(<span class="hljs-string">"Jessica"</span>));    
}
</code></pre>
<p>The variables <code>morningGreeting</code> and <code>eveningGreeting</code>, lines 6 and 7 in the sample above, make a reference to <code>MyGreeting</code> interface and define different greeting expressions.</p>
<p>When writing a lambda expression, it is also possible to explicitly specify the type of the parameter in the expression like this:</p>
<pre><code class="lang-java">MyGreeting morningGreeting = (String str) -&gt; <span class="hljs-string">"Good Morning "</span> + str + <span class="hljs-string">"!"</span>;
MyGreeting eveningGreeting = (String str) -&gt; <span class="hljs-string">"Good Evening "</span> + str + <span class="hljs-string">"!"</span>;
</code></pre>
<h4 id="heading-block-lambda-expressions">Block Lambda Expressions</h4>
<p>So far, I have covered samples of single expression lambdas. There is another type of expression used when the code on the right side of the arrow operator contains more than one statement known as <strong>block lambdas</strong>:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyString</span> </span>{
    <span class="hljs-function">String <span class="hljs-title">myStringFunction</span><span class="hljs-params">(String str)</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span> <span class="hljs-params">(String args[])</span> </span>{
    <span class="hljs-comment">// Block lambda to reverse string</span>
    MyString reverseStr = (str) -&gt; {
        String result = <span class="hljs-string">""</span>;

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = str.length()-<span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)
            result += str.charAt(i);

        <span class="hljs-keyword">return</span> result;
    };

    <span class="hljs-comment">// Output: omeD adbmaL</span>
    System.out.println(reverseStr.myStringFunction(<span class="hljs-string">"Lambda Demo"</span>)); 
}
</code></pre>
<h4 id="heading-generic-functional-interfaces">Generic Functional Interfaces</h4>
<p>A lambda expression cannot be generic. But the functional interface associated with a lambda expression can. It is possible to write one generic interface and handle different return types like this:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyGeneric</span>&lt;<span class="hljs-title">T</span>&gt; </span>{
    <span class="hljs-function">T <span class="hljs-title">compute</span><span class="hljs-params">(T t)</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{

    <span class="hljs-comment">// String version of MyGenericInteface</span>
    MyGeneric&lt;String&gt; reverse = (str) -&gt; {
        String result = <span class="hljs-string">""</span>;

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = str.length()-<span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)
            result += str.charAt(i);

        <span class="hljs-keyword">return</span> result;
    };

    <span class="hljs-comment">// Integer version of MyGeneric</span>
    MyGeneric&lt;Integer&gt; factorial = (Integer n) -&gt; {
        <span class="hljs-keyword">int</span> result = <span class="hljs-number">1</span>;

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>; i &lt;= n; i++)
            result = i * result;

        <span class="hljs-keyword">return</span> result;
    };

    <span class="hljs-comment">// Output: omeD adbmaL</span>
    System.out.println(reverse.compute(<span class="hljs-string">"Lambda Demo"</span>)); 

    <span class="hljs-comment">// Output: 120</span>
    System.out.println(factorial.compute(<span class="hljs-number">5</span>)); 

}
</code></pre>
<h4 id="heading-lambda-expressions-as-arguments">Lambda Expressions as arguments</h4>
<p>One common use of lambdas is to pass them as arguments.</p>
<p>They can be used in any piece of code that provides a target type. I find this exciting, as it lets me pass executable code as arguments to methods.</p>
<p>To pass lambda expressions as parameters, just make sure the functional interface type is compatible with the required parameter.</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyString</span> </span>{
    <span class="hljs-function">String <span class="hljs-title">myStringFunction</span><span class="hljs-params">(String str)</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">reverseStr</span><span class="hljs-params">(MyString reverse, String str)</span></span>{
  <span class="hljs-keyword">return</span> reverse.myStringFunction(str);
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span> <span class="hljs-params">(String args[])</span> </span>{
    <span class="hljs-comment">// Block lambda to reverse string</span>
    MyString reverse = (str) -&gt; {
        String result = <span class="hljs-string">""</span>;

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = str.length()-<span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)
            result += str.charAt(i);

        <span class="hljs-keyword">return</span> result;
    };

    <span class="hljs-comment">// Output: omeD adbmaL</span>
    System.out.println(reverseStr(reverse, <span class="hljs-string">"Lambda Demo"</span>)); 
}
</code></pre>
<p>These concepts will give you a good foundation to start working with lambda expressions. Take a look at your code and see where you can increase the expressive power of Java.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
