Functional programming in Python? by David Vujic

May 25, 2022

In the Functional programming in Python? presentation by David Vujic, he shares his experience of applying functional programming concepts to Python after working with functional languages like Clojure and ClojureScript. He divides the talk into three parts, covering the basics of functional programming, the use of comprehensions, and functional approaches to handling data and errors. David demonstrates the flexibility of Python by showing multiple ways to solve the same problem using functional programming techniques, such as list comprehensions and filter functions. He also discusses the benefits of using a functional mindset, including avoiding mutations and separating data and calculations. Additionally, David explores railway-oriented programming (ROP) as a way to handle errors and emphasizes the importance of error handling and refactoring in functional programming. Throughout the video, he encourages experimentation with functional programming in Python and shares his personal experience of improved code readability and understanding.

Functional programming in Python?: A comprehensive overview

Rediscovering Python with Functional Programming

David Vujic began by discussing his transition back to Python, aiming to bring the benefits of functional programming into the Python ecosystem. He structured his talk into three parts: an introduction to functional programming basics, the application of these principles in Python, and the developer experience and tools that support functional programming in Python.

 

The Zen of Python meets Functional Programming

David highlighted the flexibility of Python by comparing different ways to solve the same problem, aligning with the Zen of Python’s guideline to find one obvious way to solve a problem. He demonstrated how functional programming can coexist with Pythonic practices by using examples like finding a word in a list through for loops, the filter function, and list comprehensions.

 

Comprehensions and functional constructs

He delved into comprehensions, explaining their significance in functional programming within Python. David compared various functions and emphasized that generator comprehensions are particularly Pythonic. He then addressed common practices like mutating data and proposed safer alternatives, such as merging dictionaries with the pipe operator introduced in Python 3.9, to avoid unintended side effects.

 

Practical Functional Programming: Real-world examples

David shared a real-world scenario where he applied functional programming principles to improve a Python script for searching files in directories. By refactoring the code to use recursion and reduce mutations, he showcased how functional approaches can simplify and enhance code readability and maintainability.

In another example, David tackled a hypothetical problem of tracking available desks in an office. He demonstrated how a functional mindset, focusing on functions and methods rather than objects and classes, can provide cleaner and more efficient solutions.

 

REPL-Driven development and error handling

David emphasized the benefits of REPL-driven development, where code is tested and run directly in the editor, facilitating smoother experimentation. He shared an example of validating CSV files, illustrating how initial error handling with try-catch statements made the code messy. By refactoring the code into separate functions with localized error handling, he improved its readability and robustness.

 

Railway-Oriented Programming (ROP)

David explored Railway-Oriented Programming (ROP), a technique allowing functions to have successful or failure paths. By using a ROP decorator to handle errors and return a fail object, subsequent functions in the pipeline can check for failure and skip processing if necessary. This approach promotes a “happy path” style of coding, making functional code in Python more manageable and less error-prone.

 

Benefits and team adoption

David shared his personal experience, noting that functional programming has made his code easier to understand and debug by isolating problems into simple functions with clear inputs and outputs. Although he hasn’t observed a significant reduction in bugs, the overall code quality and maintenance have improved. His team has responded positively to these changes, and he plans to introduce the railway approach for better error handling in future projects.

 

Conclusion

David Vujic’s presentation on Functional Programming in Python? provided a comprehensive look at how functional programming principles can enhance Python development. By integrating functional constructs, embracing REPL-driven development, and applying techniques like Railway-Oriented Programming, developers can write cleaner, more maintainable, and robust Python code. Functional programming in Python is not only feasible but can significantly improve your coding experience and outcomes.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.