Is there an equivalent of partial application for return values?
-
25-05-2021 - |
题
If papply returns a function of less arity than an input function, is there a similar FP operation with returns a function which returns a value regardless of the value of the input function? If so, is there a C# equivalent?
Consider a C# function that returns void
which you want to convert into an expression, and you've done this many many times by writing an anonymous function wrapper like (args) => f(args); return null;
.
In C#,
public Func<T1, T2, ..., T8, TResult> WhatIsMyName<T1, T2, ..., T8, TResult> (Action<T1, T2, ..., T8> action, TResult value = default(TResult))
{
return (t) => { action(t); return value; }
}
which you would ideally call like FP.WhatIsMyName(voidfunc)
and so avoid having to cast.
In Clojure,
(defn whatismyname? [f x]
(f)
x)
解决方案
You could write a method that creates the anonymous function for you.
public Action<T> Ignore<T,TResult>(Func<T,TResult> func)
{
return (args) => { func(args); return; }
}
Although your code technically returns args, which would be the following format.
public Func<T,T> Ignore<T,TResult>(Func<T,TResult> func)
{
return (args) => { func(args); return args; }
}
In either case you can pass it into another method like this Method(Ignore(DoSomething));
or you can call it yourself like this Ignore(DoSomething)(1);
.
其他提示
In functional programming, you'd probably use function composition to augment or modify a result. For your example, you simply compose the existing function with a constant one, e.g. given f
:
val f' = const 0 o f
where o
is function composition (like in math) and const 0
creates a constant function always returning 0 (for any argument). Those are standard combinators, which you can define as follows:
fun (g o f) x = g (f x)
fun const x y = x