Lambda Function vs. UDF

been_29·2024년 7월 29일
post-thumbnail

💡 The difference between Lambda Function and UDF


🎃 Lambda Funtion

  • Definition
    • A small anonymous function defined with the ‘lambda’ keyword.
    • Unlike regular functions defined using ‘def’, a ‘lambda’ function can have any number of arguments but only one expression.
  • Syntax
    lambda arguments: expression
  • Characteristics
    • Anonymous : Lambda functions do not have a name.
    • Single Expression : Lambda functions are limited to a single expression. They cannot contain multiple statements.
    • Inline : They are often used inline, making them convenient for short-term operations.
    • Functional : Commonly used with functions like ‘map()’, ‘filter()’, and ‘sorted()’.
  • Usage
    • Commonly used when a simple function is needed for a short period of time, such as when working with higher-order functions that take other functions as arguments. They are often used in functional programming constructs.
  • Example
    • Ex1) Lambda Function for Addition
      add = lambda x, y: x + y
      print(add(5, 3))  # Output: 8
    • Ex2) Lambda Function with ‘map()’
      numbers = [1, 2, 3, 4, 5]
      doubled = list(map(lambda x: x * 2, numbers))
      print(doubled)  # Output: [2, 4, 6, 8, 10]
    • Ex3) Lambda Function with ‘filter()’
      numbers = [1, 2, 3, 4, 5]
      evens = list(filter(lambda x: x % 2 == 0, numbers))
      print(evens)  # Output: [2, 4]
    • Ex4) Lambda Function with ‘sorted()’
      words = ["banana", "apple", "cherry"]
      sorted_words = sorted(words, key=lambda x: len(x))
      print(sorted_words)  # Output: ['apple', 'banana', 'cherry']
    • Ex5) Lambda Function with ‘apply()’
      #Categorized people with their age
      df['Age Group'] = df['Age'].apply(lambda x:'Young' if x<30 else 'old')
      df
  • Benefits of Lambda Functions
    • Conciseness : Lambda functions provide a compact way of defining small functions.
    • Readability : In some cases, lambda functions can make the code more readable by removing the need to define a separate named function.
    • Functional Programming : They fit well with functional programming styles and tools, such as ‘map’, ‘filter’, and ‘reduce’.
  • Limitations
    • Single Expression : Lambda functions are limited to a single expression. They cannot contain multiple statements or annotations.
    • Readability : Overuse of lambda functions can sometimes lead to code that is difficult to read and understand.
    • No Assignment : You cannot assign statements or docstrings to lambda functions, limiting their use compared to regular functions.



🎃 UDF

  • Definition
    • User Defined Function(UDF) is a function provided by the user of a program or environment, as opposed to being built into the environment or language.
    • Used to extend the functionality of a programming language or database system by allowing users to create their own custom functions to their specific needs.
  • Characteristics
    • Customizable : UDFs allow users to define their own logic, enabling customization that is not possible with built-in functions.
    • Reusable : Once defined, UDFs can be reused across different parts of a program or across multiple programs.
    • Encapsulation : UDFs encapsulate code, making it modular, easier to read, and maintain.
    • Integration : UDFs can be integrated into existing programming languages or database systems, allowing for seamless extension of functionality.
  • Example
    • Ex1) In Programming Languages
      # Define a UDF to calculate the square of a number
      def square(x):
          return x * x
      
      # Use the UDF
      print(square(5))  # Output: 25
    • Ex2) In Database Systems
      -- Define a UDF to concatenate first and last names
      CREATE FUNCTION full_name(first_name VARCHAR, last_name VARCHAR)
      RETURNS VARCHAR AS $$
      BEGIN
          RETURN first_name || ' ' || last_name;
      END;
      $$ LANGUAGE plpgsql;
      
      -- Use the UDF
      SELECT full_name('John', 'Doe');  -- Output: 'John Doe'
  • Benefits of UDFs
    • Extend Functionality : UDFs allow users to add new capabilities to programming languages and database systems.
    • Code Reusability : UDFs promote code reusability, reducing redundancy and enhancing maintainability.
    • Modularization : By encapsulating specific functionalities into functions, UDFs help in organizing code better.
    • Improved Readability : UDFs make the code more readable by abstracting complex logic into named functions.
  • Drawbacks of UDFs
    • Performance Overhead : UDFs can introduce performance overhead, especially if not optimized properly.
    • Complexity : Writing and maintaining UDFs can add complexity to the codebase.
    • Security Risks : Poorly written UDFs can introduce security vulnerabilities, especially in database systems.
profile
Data Analysis

0개의 댓글