Why would one use a functional language in an otherwise Imperative project?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your project is truly imperative, you probably don’t want a purely functional language. But you probably still want a language with functional features; functional style addresses low-level code structure in the same way that object-oriented style addresses high-level structure. Both allow you to package certain common patterns in a language-supported way.
In a primarily imperative project, functional style is useful at the expression and statement level, allowing you to abstract common loops and sequences:
For example, take this common pattern:
That’s
map:Or this:
Becomes
fold(also known asreduce):Imperative style does not address this low-level duplication all that well–hence the “I wish I had a nickel for every time I typed
for(int i = 0; ...)“. Even in OO languages without functional features, code inside methods doesn’t differ much from similar non-OO languages.Some IDEs for address this by providing code snippets. This addresses the lack of abstraction power in the wrong way. The way to handle a repeated pattern is not to encourage cut-and-paste with little holes for variable names, but to abstract the pattern into a reusable unit.
Note: I addressed embedding functional code in an imperative project. A top-to-bottom project in functional style will look different. Here are some links taken from similar Stack Overflow questions: