Mandatory language incorporating functional elements

Asked

Viewed 294 times

12

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 and what are their main differences?

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

I have zero experience in functional languages, so the question has arisen:
That aspects of an imperative language, such as C#, are functional?

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

1 answer

6


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 another.

When we talk about functional C# it’s just an allusion to the paradigm and not that C# actually becomes functional (It may sound like academicism, which I neither like, but it is important to conceptualize right, neither C#, nor Javascript, or others who cite how functional are in fact, nor am I talking about the purely functional ones because to be pure is very complicated and makes language almost impossible in practice.

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

I’m not even gonna talk about recursiveness that has existed since the beginning of the language.

Anonymous functions

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

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

Generators

In C# 2 the generators were created (Yield) which, together with the Amble and the type inference (var), among other things, the creation of LINQ which is very similar to the Pipes and continuation functional languages (no functional language currying native in the language, but it is possible to do so). Generators are fundamental in functional languages for many scenarios because it is one of the mechanisms that allows the call Lazy Evaluation.

And it allowed greater ease to work with asymchronism with corroding. See about the mechanism.

Declarative form

Functional languages tend to be more declarative (as demonstrated in the original response). C# can do this with LINQ. Although you can always do a little this limited by coding convention (it has always been done for example Fluent Interfaces), with the right technologies become easier and more powerful. Extension methods helped and moved a little further away from the idea of OOP.

Explicit flow control by these and other forms is increasingly avoided.

Purity

Functional languages preach much to purity of functions and the absence of side effects. C# always allowed right immutability (plus) that helps in this (readonly that can now be used in new contexts previously prohibited). But this has been improved in each version.

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

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

Pattern Matching

Available and improving in each version since the 7.0, has enabled an anti OOP :) way of selecting what to do, simplifying code and giving more robustness eliminating much cast. It encourages focusing on the algorithm according to type. Example.

Expressions

C# has transformed some statements for expressions. Case of throw and switch. And there’s more to come.

Typing

C# does not have Algebric Data Types or Discriminated Unions, but with Generics it is possible to do something close. By the way, genericity and metaprogramming can help a lot to achieve the functional objectives. Has proposed for Adts to be implemented in the same language.

Tuple It’s not enough to refer to functional language, but almost all of them have this form of data composition, also to the detriment of object orientation. At least it’s a different way of structuring data in a more informal way. Example.

Simplified syntax

This is neither a specific mechanism nor something fundamental for a language to be considered functional, but it is common for them to have simplified syntax, which allows writing code well expressive and not very verbose. This is in contrast to Java, which does not allow you to express all kinds of code well. The philosophy of C# is to write short and semantic code, which can be better abstracted some operations, including having operator overload just to set an example. But in the latest versions one can notice a bigger concern to stay with a code more "mathematical" and lean (search mainly from the 6 forward, but had improvements in the 2, 3 and 4 too), without losing its characteristics.

Ranges entered C# 8. It is a more expression type and it helps to be more declarative. Just like foreach which has always been more declarative by hiding the raw scanning mechanism from the collection.

It is not entering the language, but you see people using, Microsoft writing simpler types, often only with state, without hiding the detail of the implementation, something that is more typical of functional languages.

There is also today a more discrete use of exceptions. Exception is side effect by definition.

C# has adopted some characteristics of Duck Typing at some points.

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

Completion

Far from the list being complete in everything that C# approaches more functional, there are things that I’ve forgotten or don’t even have much information for me link, or it is even too subtle to be listed as an explicit item.

There are 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# puts new mechanisms that are opposed to OO is already indicative.

In my original answer I wanted to strongly move C# away from the world created by the centralization of objects in the code, which is a mistake in most scenarios. Making the object less important by facilitating other forms balances a little more. Encourage the composition has much more functional face than OO that encourages group and couple things (functional induces much more to cohesion and low coupling).

I didn’t talk about reflection and code generation that isn’t exactly functional, but that helps move towards it. I also did not mention what is expected from C# 9 forward, 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 more not to stay too long, and also because it fits specific questions since not even have good material in Sopt yet.

Additional information

  • It’s just an addendum. By the (little) that I read about, those who study the trade-offs of languages know that there is no perfect language, it depends on the use that will be made of it, which varies from case to case. It’s about trade-offs. And some think that mixing paradigms weakens language, because it mixes contradictory elements. And even languages that care about the purity of the paradigm have flaws. So it’s a snooker.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.