COFFEE

A lot of times I meet friends and colleagues in coffee shops, to me, it was a remedy to keep one awake at all time, but it’s much more than that. Not only can your daily cup of joe help you feel more…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Lambda Calculus in Object Oriented Programming

Functional Programming is a technique of programming that stresses on evaluating functions, especially functions in which the data will always remain fixed. It focuses more on evaluating functional expressions, rather than evaluating statements or commands. Functions are first-class citizens in functional programming.

Some examples of Functional Programming languages:

In Object-Oriented Languages, objects can easily be reused in another part of the application. New objects can be created in the same class and languages have varying memory management for these features.

The fundamental difference between the two is how the two approach data and behaviors. In all applications, there are two main components: the data (the things your program knows) and the behaviors (the stuff your program can do to (or with) that data). Object Oriented Programming revolves around bringing data together and associating different behaviors in a single location (an “object”). When your data needs to be mutated, this makes it easier to understand how your program works. Functional Programming says that data and behavior are distinctively different things and should be kept separate.

Diving in on those fundamental differences, one of the core aspects that separate OOP from FP is how much their functions strictly adhere to the Lambda Calculus theory. Wikipedia defines lambda calculus as —

This theory is the backbone of functional programming, and it’s not extremely complex, regardless of the language you are most experienced with. In fact, we have used binding and substitution every day in our labs. To put it simply, we are speaking of a theory where you can define functions with named arguments, and call such functions substituting the named argument in the function body with your own values.

Here is a mathematical example of a lambda equation solving for partial derivatives :

Here is a basic Langrangian function for Cost Minimization. The analogy being given the constraints of 1000 units, and 10 employees, you want to know what is the minimum cost of producing 1 additional unit. Once you take the partial derivative for each variable(calling the function), Lambda can act as that constraint because it is essentially an arbitrary argument we passed into the function(setting each partial derivative equal to the lambda we passed in).

In that example, we wrote the function and a lambda constraint. Once we start our first step/call the function, we first return functions consisting of partial derivatives and our lambda constraint. It is at this point, the lambda constraint is evaluated in conjunction with each variable to optimize for both.

Ultimately, all we are doing is defining a function with arguments, of which when we call the function we substitute certain arguments that adhere to a function within the body of your function.

Javascript is a very apt language when it comes to functional programming concepts, mostly because of its dynamic abilities.

The power of this is at its highest utility when dealing with a complex set of fixed data (i.e. a multivariate regression of data), but a simple example can be easily shown below in JavaScript.

In more complex problems, an additional currying step would be involved, which can also be done in JavaScript. We can create a partial application of the function by setting a variable equal to

A way of creating a partial application in Ruby is to create a lambda that takes a parameter and returns a lambda that does something with it.

In this example, the lambda will take num1 as a parameter, and return a lambda. When you call this lambda with num2, it will return the product of the two.

In conclusion, there is no ‘better’ approach to programming, and one great thing about JavaScript is that it is so flexible of a language. While its OOP downfall is its lack of arrays/objects as first-class citizens of the language,
you can work around this by applying functional concepts like a “lambda semantic” — and gain the advantages brought by the lambda calculus, such as partial application. It can support object-oriented programming because it also supports inheritance through prototyping as well as properties and methods.

Add a comment

Related posts:

Time Series Forecasting

Notwithstanding the time series analysis is widely implemented for the business and sociology, it is still considered as one of the areas where most of the data scientists and machine learning…

Virtual Kidnapping using ChatGPT

Virtual kidnapping using #ChatGPT refers to a hypothetical situation where an individual may try to manipulate or deceive others by utilizing an AI language model like ChatGPT to simulate a…

Megara

No final de 2017 eu decidi largar o curso de arquitetura que eu fazia na FAAP, pois não conseguia me ver trabalhando com aquilo, assistia às aulas sem interesse e não tinha motivação para continuar e…