1

I have been mostly writing Object-oriented code in my day to day work and have little experience with functional programming.

In JavaScript/typescript code, we can definitely pass objects as parameters to functions. My question is whether passing objects to functions violates the functional programming paradigm as I am mixing object oriented code with functional style programming. (Even though The object passed into the function does not get mutated).

For example, if I have a function f(x,y) => z where x is an object that defines some interface. Object x makes a network call to access some resource which is used to compute z. We can say that such a function still adheres to the functional programming concept of it having no side effects and it being pure even though there is mixing of OO code in the function parameter ?

Hope my question was clear.

2

1 Answer 1

2

if I have a function f(x,y) => z where x is an object that defines some interface. Object x makes a network call to access some resource which is used to compute z.

If x makes a network call and uses the result to compute z, then it's (by most definitions) non-deterministic (since it performs I/O) and thereby not a pure function. This further means that f is impure, too. I don't consider that FP. Rather, it's an attempt at Dependency Injection.

This doesn't mean, however, that you can't pass 'data structures' as parameters - they just have to be immutable or other pure functions.

If, instead, object x implemented an interface that defines a pure function (an algorithm, say), you can pass it as a parameter, use the pure function, and the function that uses the 'object' might still be pure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.