Sign in Join

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

Mailing lists / fpc-pascal / 2010-05

a few trivial questions

19 messages · 11 people · –

Reply by email Subscribe to this list Your subscription

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

spir ☣

Hello,

* TFPList
Is there another way to traverse a list than

    for i :=0 to (list.count - 1) do ...

What about list.high?
Also, is it possible to set a list's base index to 1 (so that last_index=count)? There is a super handy forEachCall method (calling a proc for each item), but I cannot find a matching map method (calling a func and storing results).

* string <-> number
I could not find _functions_ to operate such conversions, only _procedures_ str and val. So that I always end up with superfluous statements and temps variables:

    str(n,text);
    writeln(text);

vs

    writeln(NumbetToString(n));

Ok, I can write these funcs (I did ;-). But how comes there are no builtin functions for that?

* string section
Is there another way to slice a string than using copy. In particuliar, the count argument is not always practical to indicate the end_of_section position. (count = last-first+1)

* array value notation
I can set a static array's value on declaration line, but could not find a way to do it later (even less to change it). And I cannot find a way to set the value of a dynamic array --when it's not fed from a loop. End up with series of arr[i]:=v statements (code from the http://en.wikipedia.org/wiki/Middle_ages ;-). Same for lists, indeed.
    var
        arr : array [1..2] of Integer;
    ...
    i:=1 ; j:=2;
    arr := (i,j);
    ==> Syntax error, ")" expected, but "," found

Finally: is there a wiki version of the official docs, to collectively enhance them (with notes, examples, explanations...). (I'm dreaming, but after all fpc is free software, and it's the #1 reason why I chose it.) (*)

Denis

(*) This gives me an idea: a converse doc system that takes properly formatted doc and inserts proper parts of it as comment into the code at their proper place -- not the contrary ;-)

[class TFoo]
...
[method Do]
... )
... > inserted just before/after TFoo.Do's header in code ... )
[/method]
...
[/class]

________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Michael Van Canneyt
to spir ☣
spir ☣ wrote 5 lines
Hello, * TFPList Is there another way to traverse a list than for i :=0 to (list.count - 1) do ...

You can use an enumerator with the latest SVN:

foreach P in List do

What about list.high?

Count=High.

Also, is it possible to set a list's base index to 1 (so that last_index=count)?

No. That would break all other existing code.

There is a super handy forEachCall method (calling a proc for each item), but I cannot find a matching map method (calling a func and storing results).

What do you mean with 'Map' ?

Show quoted text 5 lines
* string <-> number I could not find _functions_ to operate such conversions, only _procedures_ str and val. So that I always end up with superfluous statements and temps variables: str(n,text); writeln(text);

In SysUtils: StrToInt() and IntToStr()

vs writeln(NumbetToString(n)); Ok, I can write these funcs (I did ;-). But how comes there are no builtin functions for that?

They exist; you simply don't know them.

* string section Is there another way to slice a string than using copy. In particuliar, the count argument is not always practical to indicate the end_of_section position. (count = last-first+1)

I think that LeftStr and RightStr exist in strutils.

Show quoted text 9 lines
* array value notation I can set a static array's value on declaration line, but could not find a way to do it later (even less to change it). And I cannot find a way to set the value of a dynamic array --when it's not fed from a loop. End up with series of arr[i]:=v statements (code from the http://en.wikipedia.org/wiki/Middle_ages ;-). Same for lists, indeed. var arr : array [1..2] of Integer; ... i:=1 ; j:=2; arr := (i,j); ==> Syntax error, ")" expected, but "," found

This is only possible with dynamic arrays (no declared length), and there you should use setlength.

Finally: is there a wiki version of the official docs, to collectively enhance them (with notes, examples, explanations...). (I'm dreaming, but after all fpc is free software, and it's the #1 reason why I chose it.) (*)

No, there is no wiki version, and there never will be.

Michael.

Reply by email

Graeme Geldenhuys
to spir ☣: * TFPList Is there another way to traverse a list than for i :=0 to (list.count - 1) do…

2010/5/12 spir ☣:

Show quoted text 5 lines
* TFPList Is there another way to traverse a list than    for i :=0 to (list.count - 1) do ... What about list.high?

Yes, I use the Iterator design pattern. This allows me to write code as follows:

---------------
var
  itr: ITBStringIterator;
begin
  ...
  itr := gIteratorFactory.StringIterator(sl);
  while itr.HasNext do
     writeln(itr.Next);
   ...
end;
---------------

I have created Iterators for all existing list types and the code (and accompanied article explaining Iterators) are freely available at: http://opensoft.homeip.net/articles/

Iterators have the added flexibility that I can even combine them with
regular expressions to creating a filtered list.
-----------
fitr := gIteratorFactory.FilteredStringIterator(MyStringsCollection, 'foob.*r');
while fitr.HasNext do
begin
  DoSomethingWithItem(fitr.Next);
  ...
end;
-----------

I can also move backwards, forwards, peak back or forward, reverse the iterating, reset the iteration etc. Iterators are very flexible.

Ok, I can write these funcs (I did ;-). But how comes there are no builtin functions for that?

IntToStr(), Format(), FormatFloat() etc...?

Is there another way to slice a string than using copy. In particuliar, the count argument is not always practical to indicate the end_of_section position. (count = last-first+1)

Again here you can use iterators to iterator character by character if you want. No need to know about Count or Count-1 etc. For all types of lists you now have the same interface....
iterator.HasNext

...Michael answered the rest of the questions.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

spir ☣
to Graeme Geldenhuys: 2010/5/12 spir ☣: Yes, I use the Iterator design pattern. This allows me to write code…
Show quoted text 38 lines
2010/5/12 spir ☣: > > * TFPList > Is there another way to traverse a list than >    for i :=0 to (list.count - 1) do ... > What about list.high? Yes, I use the Iterator design pattern. This allows me to write code as follows: --------------- var itr: ITBStringIterator; begin ... itr := gIteratorFactory.StringIterator(sl); while itr.HasNext do writeln(itr.Next); ... end; --------------- I have created Iterators for all existing list types and the code (and accompanied article explaining Iterators) are freely available at: http://opensoft.homeip.net/articles/ Iterators have the added flexibility that I can even combine them with regular expressions to creating a filtered list. ----------- fitr := gIteratorFactory.FilteredStringIterator(MyStringsCollection, 'foob.*r'); while fitr.HasNext do begin DoSomethingWithItem(fitr.Next); ... end; ----------- I can also move backwards, forwards, peak back or forward, reverse the iterating, reset the iteration etc. Iterators are very flexible.

Oh, thank you for this pattern. This is a really nice feature, esp. in that it integrates so well with while loops? I'll have a look at your code as soon as I can.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

spir ☣
to Michael Van Canneyt: You can use an enumerator with the latest SVN: foreach P in List do Count=High. No. That…
Show quoted text 13 lines
On Wed, 12 May 2010, spir ☣ wrote: > Hello, > > * TFPList > Is there another way to traverse a list than > for i :=0 to (list.count - 1) do ... You can use an enumerator with the latest SVN: foreach P in List do

Great!

> What about list.high? Count=High.

? In my code count=high+1. I mean list[list.count] does not exist.

> Also, is it possible to set a list's base index to 1 (so that last_index=count)? No. That would break all other existing code.
I don't understand. If I set myList.baseIndex := 1, how can it affect other people's code? It's a new feature that does not touch existing codebase.
> There is a super handy forEachCall method (calling a proc for each item), but I cannot find a matching map method (calling a func and storing results). What do you mean with 'Map' ?

It's the conventional name in most PLs of a higher-order function that creates a new sequence from an original one and a mapping func:

   cubes := map(numbers, cube)

or in OO style:

   cubes := numbers.map(cube)

But you certainly know that better than me...

Show quoted text 6 lines
> * string <-> number > I could not find _functions_ to operate such conversions, only _procedures_ str and val. So that I always end up with superfluous statements and temps variables: > str(n,text); > writeln(text); In SysUtils: StrToInt() and IntToStr()

Thank you. Hadn't found them.

[...]

Show quoted text 10 lines
> * array value notation > I can set a static array's value on declaration line, but could not find a way to do it later (even less to change it). And I cannot find a way to set the value of a dynamic array --when it's not fed from a loop. End up with series of arr[i]:=v statements (code from the http://en.wikipedia.org/wiki/Middle_ages ;-). Same for lists, indeed. > var > arr : array [1..2] of Integer; > ... > i:=1 ; j:=2; > arr := (i,j); > ==> Syntax error, ")" expected, but "," found This is only possible with dynamic arrays (no declared length), and there you should use setlength.

I haven't managed to do that with dynamic arrays, even after setting length.

var
	a : array of Integer;
begin
	setLength(a, 3);
	a := (1,2,3);
	writeln(a[1]);
end.
==> same error message as above
> Finally: is there a wiki version of the official docs, to collectively enhance them (with notes, examples, explanations...). > (I'm dreaming, but after all fpc is free software, and it's the #1 reason why I chose it.) (*) No, there is no wiki version, and there never will be.

.......

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Graeme Geldenhuys
to spir ☣

spir ☣ het geskryf:

Show quoted text 7 lines
>> Also, is it possible to set a list's base index to 1 (so that >> last_index=count)? > No. That would break all other existing code. I don't understand. If I set myList.baseIndex := 1, how can it affect other people's code? It's a new feature that does not touch existing codebase.

At one point fpGUI switched all list type components to be 1-based. This caused such a lot of issues that I very quickly switched it back to 0-based. Why? Because even though my components were 1-based, the FPC lists where 0-based. So I had to very carefully keep track of which one uses what and often made mistakes. I personally hate 0-based counting because no human counts 0, 1, 2 etc.. so why must computers? But because 0-based is so standardized in programming languages, it's just easier to follow the crowd than fight them. :-)

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Jürgen Hestermann
to Graeme Geldenhuys
I personally hate 0-based counting because no human counts 0, 1, 2 etc.. so why must computers?

Yes. That's my feeling too. Zero-based arrays/lists always generate trouble because of substracting or adding one in many places. It makes code much less readable.

But because 0-based is so standardized in programming languages,

Well, it is mainly C that has this "standardization". In Pascal this was never a standard.

it's just easier to follow the crowd than fight them. :-)

In the first place it is. But if it would be available, the crowd may change its mind.

Reply by email

spir ☣
to Graeme Geldenhuys: spir ☣ het geskryf: At one point fpGUI switched all list type components to be 1-based.…
Show quoted text 18 lines
spir ☣ het geskryf: >>> Also, is it possible to set a list's base index to 1 (so that >>> last_index=count)? >> No. That would break all other existing code. > > I don't understand. If I set myList.baseIndex := 1, how can it affect > other people's code? It's a new feature that does not touch existing > codebase. At one point fpGUI switched all list type components to be 1-based. This caused such a lot of issues that I very quickly switched it back to 0-based. Why? Because even though my components were 1-based, the FPC lists where 0-based. So I had to very carefully keep track of which one uses what and often made mistakes. I personally hate 0-based counting because no human counts 0, 1, 2 etc.. so why must computers? But because 0-based is so standardized in programming languages, it's just easier to follow the crowd than fight them. :-)

Right, I'm aware of that (and also share your pov (*)); I just wanted to point that such flexibility does not affect external code. Not more, in fact, that array [i..j] of AType

Denis

(*) For a custom toy language, every kind of seq uses 1-based index and closed-interval section. This is very annoying when the implementation lang is 0-based and uses half-open intervals.
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Marco van de Voort
to Graeme Geldenhuys: spir ☣ het geskryf: At one point fpGUI switched all list type components to be 1-based.…

In our previous episode, Graeme Geldenhuys said:

Show quoted text 6 lines
At one point fpGUI switched all list type components to be 1-based. This caused such a lot of issues that I very quickly switched it back to 0-based. Why? Because even though my components were 1-based, the FPC lists where 0-based. So I had to very carefully keep track of which one uses what and often made mistakes. I personally hate 0-based counting because no human counts 0, 1, 2 etc..

Hmm. The fact they don't name a state out loud, doesn't really mean it doesn't exist in their "model". "nothing" is a perfectly human term. It is just considered default and thus not stated.

Reply by email

Vinzent Höfler
to Jürgen Hestermann: Yes. That's my feeling too. Zero-based arrays/lists always generate trouble because of…

Von: "Jürgen Hestermann"

Show quoted text 5 lines
> it's just easier to follow the crowd > than fight them. :-) In the first place it is. But if it would be available, the crowd may change its mind.

No, she won't. She never did.

Vinzent.

GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Reply by email

Werner Van Belle
to Marco van de Voort: In our previous episode, Graeme Geldenhuys said: Hmm. The fact they don't name a state…

In our previous episode, Graeme Geldenhuys said:

> I personally hate 0-based counting because no > human counts 0, 1, 2 etc.. >

Well, we once had a prof who counted from 0. After he had drewn a large diagram on the board he wanted to make sure that it was indeed 12 states as stated in the caption. So he went: 0, 1, 2, ... , 11. Hmmm... I've seem to have missed one. Let's count again: 0, 1, 2 , .... 11

He really couldn't figure out what was wrong until somebody pointed him out that he should start was really hilarious,

Reply by email

Marc Weustink
to Jürgen Hestermann: Yes. That's my feeling too. Zero-based arrays/lists always generate trouble because of…
Jürgen Hestermann wrote 6 lines
> I personally hate 0-based counting because no > human counts 0, 1, 2 etc.. so why must computers? Yes. That's my feeling too. Zero-based arrays/lists always generate trouble because of substracting or adding one in many places. It makes code much less readable.

Read Index as offset form start. Then the index 0 makse some sense for the first element :)

Show quoted text 11 lines
> But because 0-based is so > standardized in programming languages, Well, it is mainly C that has this "standardization". In Pascal this was never a standard. > it's just easier to follow the crowd > than fight them. :-) In the first place it is. But if it would be available, the crowd may change its mind.

IIRC indexes in VB were 1 based, didn't change much

Marc

Reply by email

Juha Manninen
to Werner Van Belle: In our previous episode, Graeme Geldenhuys said: Well, we once had a prof who counted…

Hi!

Show quoted text 7 lines
Well, we once had a prof who counted from 0. After he had drewn a large diagram on the board he wanted to make sure that it was indeed 12 states as stated in the caption. So he went: 0, 1, 2, ... , 11. Hmmm... I've seem to have missed one. Let's count again: 0, 1, 2 , .... 11 He really couldn't figure out what was wrong until somebody pointed him out that he should start was really hilarious,

I was thinking he must be a geek programmer but no, a programmer would understand the high bound is 12 - 1 = 11.

Programmer count from 0 by nature. That's what separates programmers from normal people.

:-) <-- (Could not resist writing this comment, sorry)

Juha

Reply by email

Jonas Maebe
to Jürgen Hestermann
Jürgen Hestermann wrote 5 lines
>> In the first place it is. But if it would be available, the crowd may >> change its mind. > No, she won't. She never did. Of course she did! Just look at trends in programming languages (and styles) of the past. How can you say that nothing will change ever.

Please take this sort of discussions to the fpc-other list.

Jonas
FPC mailing lists admin

Reply by email

Jürgen Hestermann
to Jonas Maebe

Jonas Maebe schrieb:
...

> Of course she did! Just look at trends in programming languages (and styles) of the past. How can you say that nothing will change ever. Please take this sort of discussions to the fpc-other list.

What "sort" of discussion? Is it only me or do you censore other mails too? What an arrogant attitude! Why did you allow Vincent to start this "sort of discussion" while sensoring mine? I once was a enthusiast Free Pascal / Lazarus fan but I am going to loose this now. This is no longer the open source spirit it once was.

Reply by email

Jonas Maebe
to Jürgen Hestermann
Jürgen Hestermann wrote 6 lines
Jonas Maebe schrieb: ... >> Of course she did! Just look at trends in programming languages (and styles) of the past. How can you say that nothing will change ever. > Please take this sort of discussions to the fpc-other list. What "sort" of discussion?

Discussions about disagreements on philosophical issues.

Is it only me or do you censore other mails too?

I stop threads on fpc-pascal from time to time when they become off-topic for that list. I am not targeting you in particular.

What an arrogant attitude! Why did you allow Vincent to start this "sort of discussion" while sensoring mine?

This mailing list is generally not moderated. That means that all mails are immediately sent to the mailing list. Unless the system is changed so that all mails require prior moderator approval, some mails will always get to the mailing list. Additionally, a) I do not read the mailing list every minute, so it can take a while before I react b) to avoid posting too many of these end-of-thread notices, I sometimes wait a while so that a thread will hopefully die out by itself (but I did not see that happening here) c) if I posted myself in such a discussion (in error or not -- I'm also only human), I feel I have to leave it open for a while longer to avoid getting the last word and then closing tings down for everyone else (but that was not the case here, and you got the last word in this discussion *on this list*; keep in mind that you all are free to continue it on the fpc-other list)

Jonas
FPC mailing lists admin

Reply by email