public static Func<string, Task<T>> MyMethod<T>(
        UserCredentials credentials,
        Func<string, string, string, Task<T>> func
    ) =>
        async (value) => await func(credentials.user, credentials.pwd, value);

I'm having difficulty discerning the purity of the function, because obviously T(int) results in a different output as T(string). Or does the purity depend on T(int) = 5 always resulting in 10 and T(string) = "Foo" always resulting in "Bar"?


Or does the purity depend on T(int) = 5 always resulting in 10 and T(string) = "Foo" always resulting in "Bar"?


A Type Parameter is still one of the function's input parameters, even though it doesn't reside within the function's parentheses. The definition of a pure function is that, given the same values for its input parameters, it will still produce the same result (without any side-effects).

| improve this answer | |
  • That's where I was leaning too, but I'm new to this way of thinking. Thank you. – Yatrix Feb 23 '18 at 18:18
  • 2
    Note: purity is not the same as idempotency. idempotency says that f(f(x) == f(x) FORALL x, or in other words: the result of executing a subroutine multiple times is the same as executing it once. sqrt is pure, but not idempotent: sqrt(sqrt(16)) is not the same as sqrt(16). Conversely, a subroutine that deletes a particular file is idempotent (if you execute it once, the result is that the file is gone, if you execute it multiple times, the file is still gone) but clearly not pure. – Jörg W Mittag Feb 23 '18 at 18:34
  • 4
    Your sqrt example doesn't make any sense. Nobody disagrees that those two calls return a different result, but you're not passing in the same value, are you? – Robert Harvey Feb 23 '18 at 18:54
  • @RobertHarvey - Jörg is using the mathematical definition of Idempotence (see the first bullet point in the lead section), not the common software engineering one, which can be seen as equivalent if the state of the system is an additional implicit parameter to the function. – Jules Feb 23 '18 at 19:36
  • 2
    @JörgWMittag I'm not sure I buy that GET is pure. Yes, it has no side effects but it can definitely depend on hidden information i.e. the result is not purely (see what I did there) dependent on the input. – JimmyJames Feb 23 '18 at 20:01

Using the definition of Pure Function from wikipedia, a function is pure if:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).

There are two ways I think you can look at this: 1. the type parameter T is part of the function definition or it's part of the input to the function. Either way, I don't think it matters in determining whether the function is pure.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.