Jump to content
John Kouraklis

Difference between Pred and -1

Recommended Posts

I was bitten several times by errors in "for 0..N-1" loops when container was empty and iterator was unsigned. So for arrays "for Low..High", for other types "for in" and "for 0..Count-1" only when modification of an element is required

Edited by Fr0sT.Brutal

Share this post


Link to post

Pred() and Succ() are simple iterators defined for enumerable simple data types. If only they could be overloaded for other data types (lists, collections, whatever... maybe even for yield support) their usefulness would be much higher.

Share this post


Link to post
58 minutes ago, Alexander Elagin said:

Pred() and Succ() are simple iterators defined for enumerable simple data types. If only they could be overloaded for other data types (lists, collections, whatever... maybe even for yield support) their usefulness would be much higher.

That doesn't make a lot of sense. What is the successor of a collection. Perhaps you mean the successor of a member of a collection. But that only makes sense in the context of the collection. 

Share this post


Link to post

Scoured our complete source code and found only one occurence each of pred and succ.

One was legit.

for e := Low(TElementType) to Pred(High(TElementType)) do  // i.e. do not include the last enum value in the loop.


The other one was a bullshit one - and I was probably the author of both.

function AfterFirst(const Match:char; const st:string):String;
var
  index : Word;
begin
  index := pos(Match,st);
  if index <> 0 
   then Result := Copy(st, index + 1, succ(Length(st) - index))
    else Result := st;
end;

It's not even consequent. :classic_rolleyes:

  • Like 1
  • Haha 1

Share this post


Link to post

Finally my 2cts: Using methods like Predecessor (or Successor) to access an index based on the amount of items in a container would just hurt my brain to much. I also would always think that -1 or +1 is faster than calling some method, but then I'm the kind of developer that doe not inspect Delphis libs, I just use them and rely on documentation...a dying breed I know. But know this, all you Delphi RTL and VCL know-it-alls give Embarcadero an excuse to not maintain the online help.:classic_ninja:

 

 

 

 

Please consider this comment tongue-in-cheek.

  • Like 1

Share this post


Link to post

I did a quick look at some of the big component sources and you can actually find Pred() usage quite often. You can see some pas files use both options, Pred() and Count - 1. which I assume is due to different developers being involved.

But this one is interesting:

if Idx > pred(FItems.Count - 1) then

and no comments. Since the same method already uses Pred(), I assume someone didn't like Count-1 and they changed it half way. 🙂

Share this post


Link to post
1 minute ago, Mike Torrettinni said:

I did a quick look at some of the big component sources and you can actually find Pred() usage quite often. You can see some pas files use both options, Pred() and Count - 1. which I assume is due to different developers being involved.

But this one is interesting:


if Idx > pred(FItems.Count - 1) then

and no comments. Since the same method already uses Pred(), I assume someone didn't like Count-1 and they changed it half way. 🙂

Total obfuscation here. Just because it exists doesn't mean you should use it. 

Share this post


Link to post
37 minutes ago, David Heffernan said:

Total obfuscation here. Just because it exists doesn't mean you should use it. 

True, interesting it wasn't caught in code review before release.

 

Share this post


Link to post
On 4/15/2020 at 10:30 AM, Lars Fosdal said:

The other one was a bullshit one - and I was probably the author of both.


function AfterFirst(const Match:char; const st:string):String;
var
  index : Word;
begin
  index := pos(Match,st);
  if index <> 0 
   then Result := Copy(st, index + 1, succ(Length(st) - index))
    else Result := st;
end;

It's not even consequent. :classic_rolleyes:

Fun fact: you can omit the 3rd argument of Copy if you just want to cut from the beginning.

  • Like 2
  • Thanks 1

Share this post


Link to post
Just now, Stefan Glienke said:

Fun fact: you can omit the 3rd argument of Copy if you just want to cut from the beginning.

 

Wow! That fact had eluded me!

Share this post


Link to post
3 hours ago, Stefan Glienke said:

Fun fact: you can omit the 3rd argument of Copy if you just want to cut from the beginning.

I always used MaxInt to copy til the very end but wasn't aware I can omit Count 🙂

Share this post


Link to post
4 hours ago, Lars Fosdal said:

Wow! That fact had eluded me!

Yeah, it's in the fine print. 😉 I learned that one only in the last few years.

  • Like 1

Share this post


Link to post
On 4/21/2020 at 12:49 AM, Bill Meyer said:

Yeah, it's in the fine print. 😉 I learned that one only in the last few years.

It seems to exist for a long time, may be from delphi2005, when I omission the 3rd argument, it should be a compile error, but it works 🙂 .

Share this post


Link to post

I also found many funs in math, for example:

function Mean(const Data: array of Single): Single;
begin
  Result := SUM(Data) / (High(Data) - Low(Data) + 1);
end;

delphi usually uses "(High(Data) - Low(Data) + 1)" to get the length, and FPC uses “High(Data)+1”(fortunately,I had never seen "Succ(High(L)"),so why it just not use the  concise and readable  "Length(Data)"?

Share this post


Link to post

OK, I must say that I started using Pred() more and more and I must say I like it, more and more.

I re-read the comments above, but I have to say it really looks like High for arrays. I don't see it exotic anymore.

 

This makes sense:

for i := 0 to High(aArray) do
for i := Low(aArray) to High(aArray) do
for i := 0 to Pred(aList.Count) do

Reading some comments again was interesting, so if using Pred() means I will never go beyond Delphi beginner level, so be it 🙂 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×