From: (see below) on 8 Feb 2010 20:03 On 09/02/2010 00:36, in article hkqamr$8mq$1(a)munin.nbi.dk, "Randy Brukardt" <randy(a)rrsoftware.com> wrote: > > "(see below)" <yaldnif.w(a)blueyonder.co.uk> wrote in message > news:C7964E2E.135979%yaldnif.w(a)blueyonder.co.uk... >> On 08/02/2010 21:20, in article wccd40fpgpu.fsf(a)shell01.TheWorld.com, >> "Robert A Duff" <bobduff(a)shell01.TheWorld.com> wrote: >> >>> It's trivial if you only want slices as R-values. >>> And anyway, slices as L-values don't really work: >> >> I can't agree. I have this code: >> >> procedure FFT_to_HWT (FFTCs : in complex_array; ... >> HWT_tree : out complex_array; ...) is >> ... >> iFFTCs : complex_array (1..f(FFTCs'length)); >> begin >> ... >> iFFTCs(1..nr_bins) := FFTCs(first_bin..last_bin); >> iFFTCs(nr_bins+1..iFFT_size) := (others => (0.0,0.0)); >> inverse_FFT(iFFTCs(1..iFFT_size)); >> ... >> HWT_tree(next_HWTC..next_HWTC+iFFT_size-1) := iFFTCs(1..iFFT_size); >> ... >> end FFT_to_HWT; >> >> I think that using slices as L-values as well as R-values helps to make >> this >> a lot clearer than it otherwise would be, and probably faster as well. > > It's cases like this that make both the pro and con for slices. Expressions > like these are probably easier to read and probably faster than the > equivalent non-slice code. > > But on the flip side, it's very hard to get the bounds right (I think I get > these sorts of slices wrong 25% of the time). So you end up spending a lot > of time debugging (at least Ada detects this). Moreover, it's hard to figure > out from the code whether or not the bounds are right -- I'll often end > drawing a picture and plug in various values to see if it makes sense. Case But you rraly have to do that reasoning in any case. > in point is the first statement; it fails unless last_bin - first_bin + 1 = > nr_bins. So it's not clear how that is saving anything. The elided statements include: if ... then nr_bins := ...; last_bin := nr_bins + first_bin - 1; else last_bin := ...; nr_bins := last_bin - first_bin + 1; end if; I was hoping the compiler would be able to propagate the bounds down to the slices, and then note that: iFFTCs(1..nr_bins)'Length == FFTCs(first_bin..last_bin)'Length Perhaps that is asking too much. Recoding thus: iFFTCs(1..last_bin-first_bin+1) := FFTCs(first_bin..last_bin); iFFTCs(last_bin-first_bin+2..iFFT_size) := (others => (0.0,0.0)); might even be an improvement in clarity, and I'd be disappointed if the compiler did not then notice the identity of lengths. -- Bill Findlay <surname><forename> chez blueyonder.co.uk
From: tmoran on 9 Feb 2010 01:34 >>> In Ada, if you do "P(X(5..10));" procedure P can see that the >>> lower bound is 5. That's a break in the abstraction -- P shouldn't >>> know or care where the string came from. >> For many, but not all, P. > >Can you give an example where P should know that its String >parameter came from a slice, and should know the lower bound >of that slice? I can't think of any off the top of my head >-- it just seems like a fundamentally broken abstraction if >you care about the lower bound of a string. package Int_IO is new Ada.Text_IO.Integer_IO(Integer); ... Last := Line'first-1; for i in V'range loop Integer_IO.Get(Line(Last+1 .. Line'last), V(i), Last); end loop;
From: Pascal Obry on 9 Feb 2010 02:11 A pattern that I have used in some applications. I have an application that output structured strings, so I use something like that: type Index is range 1 .. 100; type Output is new String (Index); subtype Label is Index range 1 .. 10; subtype Code is Index range 12 .. 18; subtype Category is Index range 35 .. 50; ... You get the idea... Then the code is simply: Line : Output; Line (Code) := "..."; or Put_Line (Line (Category)); or if Line (Code) = "..." then An equivalent code using routines (a la C, using strcpy, strcmp) will never be as readable as the use of slices in Ada. I would even say that such code become obfuscated beyond anything acceptable to my taste. Pascal. -- --|------------------------------------------------------ --| Pascal Obry Team-Ada Member --| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE --|------------------------------------------------------ --| http://www.obry.net - http://v2p.fr.eu.org --| "The best way to travel is by means of imagination" --| --| gpg --keyserver keys.gnupg.net --recv-key F949BD3B
From: AdaMagica on 9 Feb 2010 03:14 > A pattern that I have used in some applications. I have an application > that output structured strings, so I use something like that: > > type Index is range 1 .. 100; > type Output is new String (Index); > > subtype Label is Index range 1 .. 10; > subtype Code is Index range 12 .. 18; > subtype Category is Index range 35 .. 50; > ... > > You get the idea... Then the code is simply: > > Put_Line (Line (Category)); This is a pattern I have used myself. So there's a strong opposition to "useless slices" among Ada's aficionados.
From: Jean-Pierre Rosen on 9 Feb 2010 03:56
Pascal Obry a �crit : > or > > S (A .. B) := V; > > I would say that slices are really what make life easier in Ada compared > to other languages in many ways. > And don't forget: S (A .. B) := (others => 0); Enough with loop and loops at the lowest level! Thanks to slices, you can really manipulate arrays as high level objects, without always going down inside! Now, I am sure someone will tell: "but that doesn't go as far as multidimensional arrays...". True. Look at the syntax for Fortran slices, and you'll see that the cure would be worth than the disease. -- --------------------------------------------------------- J-P. Rosen (rosen(a)adalog.fr) Visit Adalog's web site at http://www.adalog.fr |