Recent

Author Topic: Functional programming in Pascal  (Read 5508 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1421
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Functional programming in Pascal
« Reply #15 on: June 23, 2019, 02:46:02 pm »
PascalDragon, I do understand that you can make your code less readable by fitting everything on a single line. But is there any new functionality?

Yes, there is.
You obviously missed the point from PascalDragon's examples.

Read his examples more carefully, see in the last example, that you can for instance generate a functions with one parameter, depending on some other parameter (local var from other function, or whatever).

Code: Pascal  [Select]
  1. type
  2.   TMyFunc = function(aArg: LongInt): LongInt is reference;
  3.  
  4. function GetFunc(aArg: LongInt): TMyFunc;
  5. var
  6.   tmp: LongInt;
  7. begin
  8.   tmp := aArg * 2;
  9.   Result := function(aArg: LongInt): LongInt begin Result := aArg * tmp; end;
  10. end;
  11.  

Actually, I very much agree with you about lesser readability, but yes, there is actually new functionality.
The bad thing is that people will use it all over the place, when not actually needed, which will do nothing else but make the code less readeable.
It should be used sparingly.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #16 on: June 23, 2019, 05:40:29 pm »
In your example, what is the actual result? A number, a pointer to a function or something else?

And because I don't understand, I read the Wikipedia article, which states:

Quote from: Wikipedia
Use of anonymous functions is a matter of style. Using them is never the only way to solve a problem; each anonymous function could instead be defined as a named function and called by name.

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 76
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: Functional programming in Pascal
« Reply #17 on: June 23, 2019, 05:45:15 pm »
Please don't let C programmers work on FPC because it can be denify
(Say NO to "modern Dinamics").Pascal til used because is native and typed.
CSV Comp DB Developer {Pascal Lover}

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #18 on: June 23, 2019, 08:19:39 pm »
Ok. Let's take an example: spawning threads in Java.

A thread in Java is essentially an anonymous (void) function without parameters. It is a procedure, that will run unhindered, do its thing and terminate. Cool. That's the best way to use threads.

But, there is also no immediate interaction. Then again, that's not so bad in Java, as it runs in a VM and all threads and instances are managed (actually, they're all globally available through the JNI). Like in C/C++, you have to give the tread a pointer to an object if you want it to communicate with it. And yes, it does have its own internal state (local vars).

On the other hand, it is of type Runnable (an interface), so you can also make a void function of that type and use that. That's how you can pass that pointer as parameter. If you want more from Java threads, things become complicated.

But you can do all those things already in Object Pascal for a few decades. And much better.
« Last Edit: June 23, 2019, 08:25:19 pm by SymbolicFrank »

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #19 on: June 23, 2019, 11:00:06 pm »
Btw, I think it's interesting that I have big problems with finding a job, because HR managers think I cannot do anything because I haven't got the right degree, and my co-workers become really irritated because they have no idea what I'm talking about.

PascalDragon

  • Sr. Member
  • ****
  • Posts: 426
  • Compiler Developer
Re: Functional programming in Pascal
« Reply #20 on: June 24, 2019, 09:17:31 am »
In your example, what is the actual result? A number, a pointer to a function or something else?
The type of Result in the example is a function reference (see the declaration of TMyFunc). And that function reference can then be called somewhere else. You can think of it like a function or method variable, but with a bit more magic behind the scenes involved.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #21 on: June 24, 2019, 09:22:55 am »
Is a function reference different from a function pointer? And is the state that the parameters are already filled?

valdir.marcos

  • Hero Member
  • *****
  • Posts: 735
Re: Functional programming in Pascal
« Reply #22 on: June 24, 2019, 11:05:29 am »
PascalDragon, I do understand that you can make your code less readable by fitting everything on a single line. But is there any new functionality?

Didn't you read what I wrote? It allows for functions together with state to be passed around outside of the scope they were declared in. Somewhat like nested functions, but not restricted to their point of declaration.

PascalDragon, I do understand that you can make your code less readable by fitting everything on a single line. But is there any new functionality?
Yes, there is.
You obviously missed the point from PascalDragon's examples.

Read his examples more carefully, see in the last example, that you can for instance generate a functions with one parameter, depending on some other parameter (local var from other function, or whatever).

Code: Pascal  [Select]
  1. type
  2.   TMyFunc = function(aArg: LongInt): LongInt is reference;
  3.  
  4. function GetFunc(aArg: LongInt): TMyFunc;
  5. var
  6.   tmp: LongInt;
  7. begin
  8.   tmp := aArg * 2;
  9.   Result := function(aArg: LongInt): LongInt begin Result := aArg * tmp; end;
  10. end;

Actually, I very much agree with you about lesser readability, but yes, there is actually new functionality.
The bad thing is that people will use it all over the place, when not actually needed, which will do nothing else but make the code less readeable.
It should be used sparingly.
This feature superficially remembers me of Computer Associate Clipper's Eval(), AEval() and DBEval():
https://www.itlnet.net/programming/program/Reference/c53g01c/ng418f5.html
https://vivaclipper.wordpress.com/2014/01/09/eval/
https://vivaclipper.wordpress.com/2014/01/07/aeval/
https://vivaclipper.wordpress.com/2014/01/17/dbeval/

Three or two decades ago, that feature eased a lot to get the job done for a very few of us, but became a real nightmare in maintenance for many programmers not that high skilled.
The same happened as we over used pre-proccessed syntax instead of CA Clipper's standard one.
https://vivaclipper.files.wordpress.com/2012/11/preprocessor-primer.pdf
Time taught us that the most important and expensive parts of a project life-circle are before (the beginning studies and decisions period) and after (the maintenance period) and not the intensive programming middle activity; today best know as KISS, an acronym for "keep it simple, stupid":
https://en.wikipedia.org/wiki/KISS_principle

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #23 on: June 24, 2019, 01:23:58 pm »
Microsoft made .NET 15 years ago, and decided there wasn't enough lock-in. So they came up with the Foundations: Windows Presentation Foundation, Windows Communication Foundation and one more for scheduling that was dropped.

Of those, Communication Foundation decided that a new communication paradigm was in order: instead of only sending data, it would send the whole object over the line. This mostly worked as expected for integers, but strings were different and objects weird. To send an object to another computer, it was decompiled and the source code with the fields was send over the line. It was recompiled and executed at the other side.

This is perfect to lock in people to the Microsoft eco-system, as it will be quite hard to run this on another platform. And it generated lots of hair-raising problems when you had a distributed project or just wanted to communicate with, say, a Unix server. (My boss demanded it.) And debugging it was really hard.

It's the same idea.

440bx

  • Hero Member
  • *****
  • Posts: 935
Re: Functional programming in Pascal
« Reply #24 on: June 24, 2019, 02:12:56 pm »
This feature superficially remembers me of Computer Associate Clipper's Eval(), AEval() and DBEval():
What that "feature" reminds me of is the old COBOL "ALTER" statement which allowed a programmer to alter (pun fully intended) the target of a goto.  Someone figured that a simple "GOTO" wasn't bad enough, with "ALTER" not only the programmer could "GOTO" just about anywhere in the program but the target of the "GOTO" could only be known at run time.   goto-s "al-dente" with lots of marinara sauce (nose bleed) and a thick layer of parmesan (aspirin gewk).   Great stuff :) 

Three or two decades ago, that feature eased a lot to get the job done for a very few of us, but became a real nightmare in maintenance for many programmers not that high skilled.
I always thought that the mark of a highly skilled programmer is the ability to produce code that is easy to maintain for the average programmer. 



using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #25 on: June 24, 2019, 03:16:49 pm »
I always thought that the mark of a highly skilled programmer is the ability to produce code that is easy to maintain for the average programmer.
And to make sure all the interfaces are defined and used. Bonus points if that is easier than using the quick-and-dirty way.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #26 on: June 24, 2019, 04:25:49 pm »
Btw, about the readability:

A few years ago, I was working on a C# ASP.NET webapp. And the lead designer spend his time with Resharping all the source code. And he turned on all the options. So, when I made some beautiful, readable and maintainable code, he turned it in an unreadable and unmaintainable mess of nested LINQ queries (Lambda functions, or anonymous functions).

PascalDragon

  • Sr. Member
  • ****
  • Posts: 426
  • Compiler Developer
Re: Functional programming in Pascal
« Reply #27 on: June 25, 2019, 09:51:25 am »
Is a function reference different from a function pointer? And is the state that the parameters are already filled?
Yes, it's different, because a function pointer is only a single CodePointer. Compare that to a method pointer which is in fact a TMethod containing both a function pointer and the Self pointer. A function reference is again different, because it also contains space for all the captured variables from the outer scope that is kept alive until the last function reference goes out of scope.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 618
Re: Functional programming in Pascal
« Reply #28 on: June 25, 2019, 01:33:53 pm »
A pointer to an object instance?

PascalDragon

  • Sr. Member
  • ****
  • Posts: 426
  • Compiler Developer
Re: Functional programming in Pascal
« Reply #29 on: June 26, 2019, 03:24:54 pm »
Behind the scenes it's an interface with only an Invoke() method that's implemented by a class instance. This class instance is shared by all anonymous functions of a single scope. Though this is all an implementation detail.