pythonfunctional-programmingtoolz

understanding toolz use cases


I am learning a little functional programming and looking at toolz. The differences between compose, pipe, thread_first, and thread_last seem very subtle or non-existent to me. What is the intended different use cases for these functions?


Solution

  • In practice (ignoring some formalism) these functions are typically interchangeable, especially when combined with functools.partial / toolz.curry and some lambda expressions, but depending on the context, it is just more convenient to use one over another.

    For example with built-in higher-order functions, like map or functools.reduce, thread_last is a natural choice. If you want to reuse a piece of code in multiple place, it is better to use compose(h, g, f) than adding function wrapper def fgh(x) pipe(x, f, g, h). And so on.