Imperative language incorporating functional elements

Yesterday I was reading an answer about the differences between the paradigms functional and imperative, and I came across this statement:

Functional Programming and Object-Oriented Programming. What are they and what are their main differences?

today, new languages and those that want to be updated are increasingly adhering to the functional paradigm. Of course, not purely, just in a way pragmatics. I put Scala and F# in the functional table because they sell like that, but they are very imperative too, and implement everything important from object orientation. So even imperative and functional can be used together, although there is one paradigm that is strong and the other only auxiliary. C # is an example of an imperative language that is more functional every day, but will never cease to be predominantly imperative, and will maintain its orientation to the object (but that has role less and less important, since 2.0 this has been falling).

I have zero experience in functional languages, so the doubt arose:
What aspects of an imperative language, such as C#, are functional?

I wanted to see examples of C # features that are incorporating features of the functional paradigm, to try to understand the idea behind the functional model a little better.

Author: Pedro Gaspar, 2018-11-18

1 answers

There in the answer is showing that imperative and functional paradigms are practically antagonistic, it is not possible to have both integrally in the same language. Let that be clear. It is different from object orientation, for example, which is a secondary paradigm, or neither is it, which can be added to a language of one of the main paradigms. It is possible to take some elements commonly present in one paradigm and associate in a limited way to the other.

When we speak of C# functional, it's just an allusion to the paradigm and not that C# becomes really functional (it may seem academicism, which I do not like, but it is important to conceptualize right, neither C#, nor JavaScript, or others that cite how functional they are in fact, and I'm not talking about purely functional because to be pure it is very complicated and makes the language

Come on. C# has always been seen as an object-oriented language. She is too, but she has always been essentially imperative, as well as Java, Ruby and even Smalltalk (this to a slightly lesser degree), contrary to what people imagine. Almost all languages have something very functional superficially. We cannot say that a language is functional just because it has a function. Of course, this does not cease to be a functional point of the language, but the function in the normal form of imperative languages are more like procedures with input communication, processing and data output, it is just a form of modularize code, one of the pillars of productivity in programming (the others are high-level syntax and automatic memory management).

I will not even talk about recursion that has existed since the beginning of the language.

Anonymous functions

Functional languages have certain requirements for functions, among other features. One of these is the function being of First Class and of high order. This means that the function it can be used anywhere, so you can assign the function itself (not call and assign the result) to a local, class or instance variable, or pass it as a parameter or return it, anyway anywhere that makes sense.

C # could always do this with delegates with closures, but in versions 2 and 3 improved syntax (lambda), delegates Ready and where can use.

Generators

In C# 2 are created in the world (dividend yield) that made it possible, along with the in the {[the 29th]}lambda and {[a 52]}article type (var), among other things, the creation of the IT is which is very similar to the {[the 29th]}pipe and {[a 29]}continuation languages and functional doesn't have currying the native in the language, but it is possible to do so). Generators are fundamental in languages functional for many scenarios because it is one of the mechanisms that allows the so-called lazy evaluation.

And it allowed greater ease to work with asynchronism with corrotines . see about the engine .

Declarative Form

Functional languages tend to be more declarative (as demonstrated in the original answer). C # can do this with LINQ . Although you can always do a little bit of this limited form by coding convention (always gave do for example Fluent Interfaces), with the right technologies get easier and more powerful. extension methods helped and moved a little further away from the idea of OOP.

More and more explicit flow control is avoided by these and other forms.

Purity

Functional languages preach a lot topurity of functions and the absence ofside effects . C # always allowed certain immutability (more ) that helps in this (readonly which can now be used in new contexts previously forbidden). But this was being improved in each version.

.NET has the attribute [Pure], but it's not something that language forces.

Today it is possible to have immutability enhanced in struct (example), in reference , in addition to what already existed by default to maintain transparency referential .

Pattern Matching

Available and improving in each version since 7.0, it has been allowing an anti OOP:) way to select what to do, simplifying code and giving more robustness eliminating a lot cast. It encourages putting focus on the algorithm according to type. example .

Expressions

C # has transforming some statements by expressions . Case of throw and switch. And there's more to come.

Typing

C # does not have algebraic Data Types or Discriminated Unions, but with generics it is possible to do something close. By the way, genericity and metaprogramming can greatly help achieve functional goals. There is a proposal for ADTs to be implemented in the language itself.

Tuple is not enough to be something that refers to the functional language, but almost all have this form of data composition, also at the expense of object orientation. At least it's a different way of structuring data in a more informal way. example .

Simplified syntax

This is not a specific mechanism and not something fundamental for a language to be considered functional, but it is common for them to have simplified syntaxes, which allow to write well expressive codes and little verbose. This contrasts especially with Java which does not allow to express well all kinds of code. The philosophy of C# is to write short and very semantic code, being able to better abstract some operations, including having operator overload just to stay in an example. But in the last versions we notice a greater concern to stay with a more "mathematical" and lean code (search mainly from 6 forward, but had improvements in 2, 3 and 4 also), without losing its characteristics.

ranges entered in C # 8. It is a more expression Type and helps to be more declarative. Just like foreach which has always been more declarative hiding the raw collection scanning mechanism.

Is not getting into the language, but you see people using, Microsoft writing simpler types, often stateful only, without hiding the detail of the implementation, something that is more typical of functional languages.

There is also today a more discreet use of exceptions. Exception is side effect by setting.

C # has adopted some features of duck typing at some points.

None of this is essential in functional, but it has a certain face in some flavors.

Conclusion

Far from the list being complete in everything that C# comes closest to the functional, has things that I forgot or does not have much information for me to be able link, or that it is even too subtle to appear as an explicit item.

Has some things that are not easy to see as functional, only that philosophy is adopted more in functional, for example nullity that is usually avoided in functional. The fact that C# put new mechanisms that oppose OO is already an indicative.

In my original answer I wanted to make strong C# 's remoteness from the world created by centering objects in code, which is an error in most scenarios. Leaving the object less important, facilitating other forms balances a little more. Encouraging composition has a lot more of the functional face Than Oo that encourages grouping and coupling things (functional induces a lot more of cohesion and low coupling).

I did not talk about reflection and code generation that is not exactly functional, but that helps to walk towards this. I also did not talk about what is planned from C # 9 ahead, which may or may not enter, for example records.

The Gabriel Schade has good articles and lectures on the subject and even a library that helps C # be even more functional .

Some things I did not explain anymore so as not to get too long, and also because it fits specific questions since neither has good material in the SOpt yet.

Additional information

 6
Author: Maniero, 2020-08-17 13:39:32