Pipes

Inspired by the following Tweet from Eric Elliot in which he uses a reducer to make a pipe function, I thought I'd do something similar in Python and F#.

Functional programming in Python seems to be "un-Pythonic" in general from what I can tell, but it can be a fun language to do FP in. The most annoying thing is that the lambda keyword is pretty verbose. Nonetheless, the same thing can be achieved pretty neatly - about as neatly as JavaScript.

pipe = lambda *fns: lambda x: reduce(lambda y, f: f(y), list(fns), x)
g = lambda n: n + 1
f = lambda n: n * 2
h = pipe(g, f)

h(20) # 42

Then, of course, in F# it's a native operator in the language so the same pipeline is beautifully brief.

let g n = n + 1
let f n = n * 2
let h a =
    a
    |> g
    |> f
    
h 20 // 42