Writing Clean concise functional code in Python with abstraction & transducers

Muthukumaran Navaneethakrishnan
1 min readJul 7, 2021

I have been exploring the opportunities on writing effective functional programming in python. The existing functions map,filter reduce does not allow effective chaining, which makes the functional code looks really bad

Consider an example list of users

To get the name of male users , whose salary greater than 100,000 we would be writing something like this with lambda

It was hard to understand whenever the code has been written within. The intention of these functional programming kindda operators is specifically meant to chain, so that it would be easy for developers to adapt.

Unfortunately python did not support it, Java/Javascript has it. RxPython has it , But it was too heavy.

I have created this small library to support chaining something like this

It would be easy for the developers to reason. you can chain as many operators as you need.

Abstractions

It does support abstraction and moved across for example just add a stream and extend for abtractions

Abstractions with Functional Streams

Transducers

Transducers are composable algorithmic transformations. . Functional streams provide composable transformations aka transducers with Stream.transducer and connect with pipe over data

The library is simple ,lean & fast

Currently Supported operations are , Most of them are self explanatory

  • map
  • filter
  • reduce
  • length
  • flatmap
  • distinct
  • peek
  • take
  • skip
  • asList

The library is available in

Watch Demo on https://youtu.be/AcQcxh0VQv0

The source code is available on https://github.com/muthuishere/python-streams

--

--

Muthukumaran Navaneethakrishnan

Programmer works with Java, Javascript ,Clojure, Rust, Golang & Python.