Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-10

New feature discussion: LAMBDA

12 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dariusz Mazur

Hi
Based on discussion about "for in" I want to suggest my proposition. When we investigate some code, sometimes we found pattern:

Pattern 1:
/  begin
    startfun;
    try
      ...
    finally
      stopfun;
    end;
/  end;
Pattern 2:
  for i:= startfun to stopfun do begin
     ....
  end;
Pattern 3:
  startfun;
  while stopfun do begin
    ...
  end;

Common for all mentioned patterns are two connected functions, which controlled running of body source. First make all initializations, second helping with iterations and finalizations.

My proposition is make possibility of join, those pair to one sentence. All patterns can be change to one common template:

   if startfun then repeat
   until stopfun;

I You see we need two function with boolean result. Of course possibility of parameters give wide appliance.

To preserve pascalish style I propose something like this:

 tMyObject = class(tObject)
   function startfun:boolean;
   function stopfun:boolean;
  property lambdafun; start startfun stop stopfun;
 end;

and invoke:

  var
   t : tMyObject;
  begin
   ..
   t.lambdafun do begin
      ....
   end;
  end;

because on declaration side is similar do properties (also two function) we can use the same syntax. Only keyword start and stop exchange read and write.

To using i thought about WITH, but it may confusing bit, thus I propose omit, and only use keyword DO.

Why lambda. Because is very similar to constructing no name procedures and use them as normal. We have few mechanism controlling runnig: IF, REPEAT, WHILE, FOR. They work well, but sometimes they are week. For example we don't have enough primitives do build own implementation FORIN method, on iterators also. But in Lisp any mechanism, any sequence of computing collection can be done and source is very readable lisp syntax: (lambda (<formal parameters>) <body>) This is not unusual, but we can send this declaration as argument of procedure.
Of course syntax like this
tlist.foreach( procedure (x : tObject);begin writeln(x) end)

is ugly (thought I sow something similar in C++/boost) but when we change it a bit:

  tlist.foreach(x) do  begin
    writeln(x);
  end;

its not so strange

Because my english is very poor I only show some application

EXP. mutex (and patterns as RIAA)

    tMutex = class
      property makeatomic; start lock stop unlock;
    end;
  .....
  begin
    tMutex.makeatomic do begin
      ...
    end;
  end;

EXP: foreach

tMyList = class

     function tMyList.start:boolean;
     begin
       current:=first;
       result:=currentnil;
     end;
     function tMyList.stop:boolean;
     begin
       current:=next(current);
        resutl:=currentnil;
     end;

When attributes are added, or exception caching this will be more flexible.

Darek

Reply by email

Dariusz Mazur
to Michael Schnell

Michael Schnell pisze:

Show quoted text 5 lines
Again something inspired by Delphi-Prism ? ( http://prismwiki.codegear.com/en/Lambda_Expressions )

No at all, I don't even see this before. I thing long about this. But lately I see this in C++ (strange) and Lisp (beautiful) In Prism have You:

c -> c.Name = 'Smith'

which is very strange, special syntax, and need to be translate to "method", and declaration longer than one line is awful,

my proposition semantically is between properties (they consist also two function), aspect programming and lambda

is very easy to translate to ordinal pascal( lambda invoke change to repeat-while iteration, similarly as in generics)

has wider application:

from FPC WIKI:
procedure StringLoop(S: String);
var
  C: Char;
begin
  for C in S do
    DoSomething(C);
end;

my prop:

procedure stringloop(s :tMyString);
begin
   {with} s.eachword do begin
      if s.currentword'begin' then
        writeln(s.currentword);
   end;
end;

of course I can write

procedure stringloop(s :tMyString);
begin
   s.firstWord;
   try
   while  s.nexword do begin
      if s.currentword'begin' then
        writeln(s.currentword);
   end;
   except
     s.exceptWord;
   end;
end;

which can be equivalent

from compiler side invoke of lambda is translate to proper iteration before main compilation from developer side: its easy to use as properties, fewer bugs caused forget of initialization or finalization, or use incompatible init and step function in iterators

Darek

Reply by email

Jeff Wormsley
to Michael Schnell: Again something inspired by Delphi-Prism ? ( http://prismwiki.codegear.com/en/Lambda_Expre…
Michael Schnell wrote
Again something inspired by Delphi-Prism ? ( http://prismwiki.codegear.com/en/Lambda_Expressions )

Wow, talk about unreadable code...

I'm all for saving typing, but not at the expense of readability. This reminds me of C's oddball ? operator. One of the reasons I like Pascal is that you do usually have to spell out what you are doing. It makes reading the code months or years later, or reading someone else's code much easier. So it might not be the most efficient in terms of typing, or even compilation or execution, but the savings in maintenance more than make up for it. The examples given at that site would drive me crazy. I don't believe I'll be using Prism any time soon.

Jeff.

I haven't smoked for 3 years, 2 months and 3 days, saving $5,222.69 and not smoking 34,817.99 cigarettes.

Reply by email

ik
to Dariusz Mazur: Michael Schnell pisze: No at all, I don't even see this before. I thing long about this.…

Pascal have something close to lambda, and it's nested functions/procedures

The original idea of lambda is to have sub process that takes parameters and simple tasks.
For example (in Ruby):

def action(base)
expo = lambda { |by_num| base ** by_num}
x = something
a = expo(x)
....
end

I can do the same with the following:

procedure action(base : integer)
  function expo(by_num)
  begin
    expo := base ** by_num
  end;
var x,a : integer;
begin
   x := something;
  a := expo(x);
 ...
end;

Of curse it's one usage for it, but most of the real usage of lambda can be made in pascal today. And please do not take ideas from C++, it's a language that wrote on it's agenda to implement any possible technology out there regardless of it's need or usage, and it's impossible to use that language without some framework or ignoring this technologies.

Ido
http://ik.homelinux.org/

2009/10/20 Dariusz Mazur

Show quoted text 79 lines
Michael Schnell pisze: > Again something inspired by Delphi-Prism ? > > ( http://prismwiki.codegear.com/en/Lambda_Expressions ) > > > No at all, I don't even see this before. I thing long about this. But lately I see this in C++ (strange) and Lisp (beautiful) In Prism have You: c -> c.Name = 'Smith' which is very strange, special syntax, and need to be translate to "method", and declaration longer than one line is awful, my proposition semantically is between properties (they consist also two function), aspect programming and lambda is very easy to translate to ordinal pascal( lambda invoke change to repeat-while iteration, similarly as in generics) has wider application: from FPC WIKI: procedure StringLoop(S: String); var C: Char; begin for C in S do DoSomething(C); end; my prop: procedure stringloop(s :tMyString); begin {with} s.eachword do begin if s.currentword'begin' then writeln(s.currentword); end; end; of course I can write procedure stringloop(s :tMyString); begin s.firstWord; try while s.nexword do begin if s.currentword'begin' then writeln(s.currentword); end; except s.exceptWord; end; end; which can be equivalent from compiler side invoke of lambda is translate to proper iteration before main compilation from developer side: its easy to use as properties, fewer bugs caused forget of initialization or finalization, or use incompatible init and step function in iterators -- Darek _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Marco van de Voort
to Jeff Wormsley: Wow, talk about unreadable code... I'm all for saving typing, but not at the expense of…

In our previous episode, Jeff Wormsley said:

Show quoted text 5 lines
> ( http://prismwiki.codegear.com/en/Lambda_Expressions ) Wow, talk about unreadable code... I'm all for saving typing, but not at the expense of readability. This reminds me of C's oddball ? operator.

(hmm, if I would name one feature I hate about C, that would be that if (x=y) goes unnoticed while legal. At least the ? operator doesn't bite you if you don't use it)

Reply by email

Dariusz Mazur
to ik: Pascal have something close to lambda, and it's nested functions/procedures The original…

ik pisze:

Show quoted text 5 lines
Pascal have something close to lambda, and it's nested functions/procedures The original idea of lambda is to have sub process that takes parameters and simple tasks.

You don't understand me. I know sub process, this is nice feature of pascal, but i talk about something different.

Sometimes we have sub process and want to do with them rather complicated task.

Show quoted text 22 lines
For example (in Ruby): def action(base) expo = lambda { |by_num| base ** by_num} x = something a = expo(x) .... end I can do the same with the following: procedure action(base : integer) function expo(by_num) begin expo := base ** by_num end; var x,a : integer; begin x := something; a := expo(x); ... end;

My example was different. Try to implement my example with the same readability, error prove,

Show quoted text 7 lines
Of curse it's one usage for it, but most of the real usage of lambda can be made in pascal today. And please do not take ideas from C++, it's a language that wrote on it's agenda to implement any possible technology out there regardless of it's need or usage, and it's impossible to use that language without some framework or ignoring this technologies.

I don't draw from C++, which is imperative language , rather try to use some of the best of functional language.
I don't even know, that it should be name lambda. Its similar to iterators, but also to aspect. It has the same syntax as properties. thats all.

Show quoted text 70 lines
2009/10/20 Dariusz Mazur < > Michael Schnell pisze: Again something inspired by Delphi-Prism ? ( http://prismwiki.codegear.com/en/Lambda_Expressions ) No at all, I don't even see this before. I thing long about this. But lately I see this in C++ (strange) and Lisp (beautiful) In Prism have You: c -> c.Name = 'Smith' which is very strange, special syntax, and need to be translate to "method", and declaration longer than one line is awful, my proposition semantically is between properties (they consist also two function), aspect programming and lambda is very easy to translate to ordinal pascal( lambda invoke change to repeat-while iteration, similarly as in generics) has wider application: from FPC WIKI: procedure StringLoop(S: String); var C: Char; begin for C in S do DoSomething(C); end; my prop: procedure stringloop(s :tMyString); begin {with} s.eachword do begin if s.currentword'begin' then writeln(s.currentword); end; end; of course I can write procedure stringloop(s :tMyString); begin s.firstWord; try while s.nexword do begin if s.currentword'begin' then writeln(s.currentword); end; except s.exceptWord; end; end; which can be equivalent from compiler side invoke of lambda is translate to proper iteration before main compilation from developer side: its easy to use as properties, fewer bugs caused forget of initialization or finalization, or use incompatible init and step function in iterators
Darek

Reply by email

Michael Schnell
to Jeff Wormsley: Wow, talk about unreadable code... I'm all for saving typing, but not at the expense of…
Jeff Wormsley wrote
Wow, talk about unreadable code...

In fact I did not ever try to understand this. I just happened to know about Lambda in Prism and wanted to let the forum share that.

I'm all for saving typing, but not at the expense of readability.

Agreed.

This reminds me of C's oddball ? operator.

In fact I think that the ? operator in C is easily readable (But of course in C you easily can create unreadable code by e.g. doing assignments in if-clauses.

  if (a=b) a=1;
which happens to translate in Pascal as:
  a:=b;
  if a0 then a:=1;

-Michael

Reply by email