1718120960

Practical examples of using lambda expressions in languages such as Python or Java.


Lambda expressions, also known as anonymous functions, are widely used in programming languages like Python and Java to create small, single-use functions without having to formally define them. Here are practical examples of how lambda expressions can be used in both Python and Java: # <br>Python 1. Sorting with Custom Key ```py data = [('Alice', 25), ('Bob', 30), ('Charlie', 20)] # Sort by age sorted_data = sorted(data, key=lambda x: x[1]) print(sorted_data) ``` In this example, the lambda function **lambda x: x[1]** is used to sort a list of tuples by the second element (age). 2. Filtering Data ```py numbers = [1, 2, 3, 4, 5, 6] # Filter out even numbers even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) ``` Here, the lambda function **lambda x: x % 2 == 0** filters the list to keep only even numbers. 3. Mapping Data ```py numbers = [1, 2, 3, 4, 5] # Square each number squared_numbers = list(map(lambda x: x ** 2, numbers)) print(squared_numbers) ``` The lambda function lambda `x: x ** 2` is used to square each element in the list. 4. Reducing Data ```py from functools import reduce numbers = [1, 2, 3, 4, 5] # Sum all numbers sum_numbers = reduce(lambda x, y: x + y, numbers) print(sum_numbers) ``` The lambda function lambda **x, y: x + y** is used with reduce to sum all elements in the list. # <br>Java 1. Using Lambdas with Functional Interfaces ```java import java.util.function.Function; public class LambdaExample { public static void main(String[] args) { Function<Integer, Integer> square = x -> x * x; System.out.println(square.apply(5)); // Output: 25 } } ``` The lambda expression **x -> x * x** implements the **Function** interface's **apply** method to square a number. 2. Sorting with Lambdas ```java import java.util.Arrays; import java.util.List; public class LambdaSortExample { public static void main(String[] args) { List<String> names = Arrays.asList("Charlie", "Alice", "Bob"); names.sort((a, b) -> a.compareTo(b)); System.out.println(names); // Output: [Alice, Bob, Charlie] } } ``` The lambda expression **(a, b) -> a.compareTo(b)** is used to sort a list of strings. 3. Using Lambdas with Streams ```py import java.util.Arrays; import java.util.List; public class LambdaStreamExample { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); // Filter even numbers and print them numbers.stream() .filter(x -> x % 2 == 0) .forEach(System.out::println); // Output: 2 4 } } ``` The lambda expression **x -> x % 2 == 0** is used to filter even numbers from a stream. 4. Custom Comparator with Lambda ```java import java.util.Arrays; import java.util.Comparator; public class LambdaComparatorExample { public static void main(String[] args) { String[] words = {"banana", "apple", "cherry"}; // Sort words by length Arrays.sort(words, (a, b) -> Integer.compare(a.length(), b.length())); System.out.println(Arrays.toString(words)); // Output: [apple, banana, cherry] } } ``` The lambda expression **(a, b) -> Integer.compare(a.length(), b.length())** is used to sort strings by their length. These examples demonstrate the power and simplicity of lambda expressions in both Python and Java, making code more concise and readable.

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]