Forum > General

Functional programming in Pascal

(1/16) > >>

rnfpc:
Are there functional programming methods in Pascal such as map, filter, apply, reduce, fold, foldl and foldr function? I could not find them on searching the internet. Possibly Pascal is a purely imperative language.

If not, is it possible to create a map function so that one can sent a list/array to it with a function and it return a new list/array with sent function applied to each element of sent list:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---outlist := map(sentfunction, sentlist);
where sentfunction is a function which takes only one item and returns a modified item.

marcov:
Pascal is mixed imperative-OOP. Conceptually the same level as C++.

There are no such functions. If they rely on functional lazy evaluation there also would be no point.

rsz:
I was also wondering about this. Does FreePascal support anonymous functions or alike? something like this?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---x := MyFunction(procedure begin    // do stuff  end)
One could easily implement map, fold, etc. if functions are to become first class in FreePascal. This would also open the door for other interesting concepts, such as easy "async" execution of functions. e.g.:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---context := ExecuteAsync(procedure begin    // do stuff in another thread  end);// do stuffcontext.Await;
I'm still pretty new to FreePascal, so maybe something as easy as this is already possible?

marcov:

--- Quote from: rsz on June 22, 2019, 11:58:06 am ---I was also wondering about this. Does FreePascal support anonymous functions or alike? something like this?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---x := MyFunction(procedure begin    // do stuff  end)
--- End quote ---

Being worked on by an external contributor.  Slow progress.


--- Quote ---One could easily implement map, fold, etc. if functions are to become first class in FreePascal. This would also open the door for other interesting concepts, such as easy "async" execution of functions. e.g.:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---context := ExecuteAsync(procedure begin    // do stuff in another thread  end);// do stuffcontext.Await;
I'm still pretty new to FreePascal, so maybe something as easy as this is already possible?

--- End quote ---

Not yet. Delphi has some such features, though there are caveats (capturing the state of the anonymous function doesn't always goes right afaik). I use it in Delphi, but try to avoid complex parametrisation.

But it remains a bit of a kludge since it is bolted on functionality, rather than being in the core of the language like with functional languages. Also compiletime typing might restrict you from emulating functional language interpreters.

Lot of work, low gains. Rather than thinking in copying over language functionality, better restate your problem as cleanly as possible, and find a better way.

julkas:

--- Quote from: rnfpc on June 22, 2019, 10:38:53 am ---Are there functional programming methods in Pascal such as map, filter, apply, fold, foldl and foldr function? I could not find them on searching the internet. Possibly Pascal is a purely imperative language.

--- End quote ---
I know great map, filter, reduce,.. from Python!
My opinion - you should first think about how to describe, implement your data structure, algorithm in Pascal language and not about language features. It's not easy. Each programming language has it's own spirit, philosophy, strength and weakness.
Regards.

P.S. You can write your own map, filter, reduce,..

Navigation

[0] Message Index

[#] Next page

Go to full version