comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <bmoore.ada@gmail.com>
Subject: Re: Tally
Date: Thu, 16 Jan 2020 07:35:28 -0800 (PST)
Date: 2020-01-16T07:35:28-08:00	[thread overview]
Message-ID: <4967a95b-3d79-4a5b-a30d-0f04f00ebbc4@googlegroups.com> (raw)
In-Reply-To: <lyftgfzlb4.fsf@pushface.org>

On Thursday, January 16, 2020 at 4:34:57 AM UTC-7, Simon Wright wrote:
> Jere writes:
> 
> > I might recommend using insert every iteration and seeing if
> > it fails or succeeds.  Since it gives a cursor regardless of
> > success, you can use that get the element and avoid doing
> > all the searches each time:
> >
> >     for Number of Input loop
> >     
> >         -- Try to insert a new element.  This will 
> >         -- fail if the element already exists
> >         Counts.Insert
> >             (Key => Number,
> >              New_Item => 1,
> >              Position => Position,
> >              Inserted => Inserted);
> >     
> >         -- Since the element already exists, simply
> >         -- increment the count
> >         if not inserted then
> >             Counts(Position) := Counts(Position) + 1;
> >         end if;
> >
> >     end loop;
> 
> Yes, I thought about that. More LoC/less clarity without the comments
> are the only objections I can see (though fairly compelling for toy
> examples!)

Thanks Simon for catching my failure to insert the initial value.

In Ada 2020, I think one could use a container aggregate to initialize the map
to contain the initial objects:

e.g.
   Counts : Count_Maps.Map 
     := [for I in Input'Range
           when (for all J in Input'First .. I - 1 => Input (I) /= Input (J))
              use I => 0];

The "when" clause should filter the input to just the unique values.
The "use" clause creates the mapping between key value and count value (Initially 0).

Then you could just write:

   for Number of Input loop
      Counts (Number) := Counts (Number) + 1;
   end loop;

To update the counts.

I leave it to the reader to decide whether this is clearer than what you had,
as I think many would prefer what you had.

For that matter, you could do it all in one shot and even make the map a constant.

   Counts : constant Count_Maps.Map
     := [for I in Input'Range
           when (for all J in Input'First .. I - 1 => Input (I) /= Input (J))
              use I =>
                 [for K in Input'Range when Input (K) = Input (I)) => 1]
                   'Reduce("+", 0)];

Using a reduction expression to count the values. 
But this is definitely quite a mouthful.

I think if one wanted a constant object, it would be clearer to write a
function that returns the map container object using a simpler form of expression to create the return object.

It would be nice however, if one could test membership of an array or container using "in" or "not in" to see if a particular element value can be found.

Then one could write;

Counts : Count_Maps.Map :=  
  [for I in Input'Range when (Input (I) not in Input (1 .. I - 1)) use I => 0];

To create the initial map objects, which is easier to read.

Similarly, it would be nice to apply 'Max or 'Min to an array or container object, which could be shorthand forms for reduction expressions using Ada 2020 syntax to return the largest or smallest element in the array or container.

e.g.
  Put_Line ("Biggest=>" & Natural'Image(Input'Max));

But if these ideas have any merit, you'd have to look past Ada 2020 to a future version of the language.

Brad

  reply	other threads:[~2020-01-16 15:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-14 15:27 Tally Gilbert Gosseyn
2020-01-14 16:22 ` Tally Niklas Holsti
2020-01-14 17:28   ` Tally Simon Wright
2020-01-15 11:52     ` Tally Simon Wright
2020-01-14 21:08 ` Tally Jeffrey R. Carter
2020-01-15  3:40   ` Tally Brad Moore
2020-01-15  9:03     ` Tally Simon Wright
2020-01-15 23:09       ` Tally Jere
2020-01-16 11:34         ` Tally Simon Wright
2020-01-16 15:35           ` Brad Moore [this message]
2020-01-16 20:20             ` Tally Randy Brukardt
2020-01-16 22:03               ` Tally Jeffrey R. Carter
2020-01-16 22:00             ` Tally Jeffrey R. Carter
2020-01-16 22:25               ` Tally Simon Wright
2020-01-17  2:51                 ` Tally Brad Moore
2020-01-17  3:08       ` Tally Brad Moore
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox