Tuesday 10 November 2020

lambda functions for "dumb" people...

Introduction

First of all, I have to admit I used to be really dumb regarding lambda expressions. Partly because of my age (I was about 40 back then) and the fact that I started coding at 35. If you're in a similar situation and have some math background, read on.

The funny thing was, I used to understand lambdas quite well, having learned a bit of math in the past; only the way people used to describe them in the context of programming made me immune to both knowledge and understanding. Let alone using lambda functions effectively. After a while of thinking, experimenting, reading documentation and looking for links with my previous knowledge, I finally got something that is sufficient for my everyday's coding practice, mostly in C#.

If you know what "function" in math means and what to do with "ordinary" functions in your code, it might seem strange to hear or read about lambda things being "small anonymous functions", or "functions without separate declaration" etc. From my point of view, these are just names that people who are familiar with the topic make up. My way of feeling comfortably dumb and being against such careless "explanations" made me take another path: just go "one level beyond functions", and suddenly it becomes very natural. To me—and I hope it will be natural to you.

Back to Math

I'm not a mathematician—please don't treat the following section as a professional introduction; I'm not educated enough to pretend I know much about the field of functional analysis. I tried to learn it once, but failed. The main idea behind "one level beyond" is: you can define functions whose domain is a set of other functions.

Example: Let f map [0,1] to R. You can define a functional F to be 's value at, say, x = 1/2. The value of F will obviously depend on a particular choice of f.

You can do anything that is "doable" with such fs, as long as the function space they live in permits that "anything". If you choose f to be integrable over R, you can define your functional as an integral, say, of (3x + 2) from –3 to 5. Note that his functional is linear. It it were an L2(R) space, the integral of | |2 would be an example of a non-linear functional.

Summing up: you can define "functions over functions", and their name is "functionals". In other words: functions may be arguments for other functions. Whether they are "higher level" or "more abstract", is irrelevant, although there is some reason why we learn about "ordinary" functions first and then we try to take a course on functional analysis. For me it was way too difficult and I approached the subject much too late.

If you know C/C++ function pointers, you're almost there. I was almost familiar with these, but not enough. Perhaps this lack of practice added to my resistance to understanding lambda functions.

A way to understand lambdas

In my view lambda "functions" are, in a natural way, function- (or delegate or function-pointer)-like arguments for other functions. In the picture above—they are fs that can be eaten by some F, and give some result. Whether it's void or does return a value, is irrelevant here; the same of course applies to f, depending on what your code is intended to do. 

One great advantage due to "anonymous" nature of lambdas is that you see the "declaration" in place, just as if someone declared a functional like

F() = [–1,1] [x ↦ 3x + 2] dx .

Does this definition contain a label/name for f under the integral? No. Just bare (andhopefully, understandable) definition. Lambda expressions are analogous. Hence the "small, anonymous function". The smaller the function, the more readable and clear its definition "in place" is.

If you want to operate on strings, you may want to include some filter that would e.g. make the string argument be processed in lowercase. Or in UPPERCASE. If you want to operate on some class' fields, and cannot use direct pointers or references, include a lambda that chooses the apropriate field. There's plenty of examples on the net.

I hope that helps a bit. Please excuse my non-native English and typesetting, I have no time to dig in MathJax & friends to make formulas look as good as they should. If you find a bug/error here, please let me know.

No comments:

Post a Comment