I work in Python. Recently, I discovered a wonderful little package called fn. I've been using it for function composition.
For example, instead of:
baz(bar(foo(x))))
with fn, you can write:
(F() >> foo >> bar >> baz)(x) .
When I saw this, I immediately thought of Clojure:
(-> x foo bar baz) .
But notice how, in Clojure, the input is on the left. I wonder if this possible in python/fn.
Function composition is the way of combining two or more functions in such a way that the output of one function becomes the input of the second function and so on.
You could just write a function that returns Max(lst), Min(lst) . It will be inefficient since it will recurse twice, but it's the simplest way to combine the two existing functions. Use objects! Your object can have 2 properties min and max.
A composite function can be evaluated by evaluating the inner function using the given input value and then evaluating the outer function taking as its input the output of the inner function.
It is possible to compose a function with itself. Suppose f is a function, then the composition of function f with itself will be. (f∘f)(x) = f(f(x)) Let us understand this with an example: Example: If f(x) = 3x2, then find (f∘f)(x).
You can't replicate the exact syntax, but you can make something similar:
def f(*args):
result = args[0]
for func in args[1:]:
result = func(result)
return result
Seems to work:
>>> f('a test', reversed, sorted, ''.join)
' aestt'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With