From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a37:2751:: with SMTP id n78mr34349652qkn.177.1579188929918; Thu, 16 Jan 2020 07:35:29 -0800 (PST) X-Received: by 2002:a9d:74c4:: with SMTP id a4mr2468566otl.119.1579188929191; Thu, 16 Jan 2020 07:35:29 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!g89no3077125qtd.0!news-out.google.com!w29ni1375qtc.0!nntp.google.com!g89no3077115qtd.0!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 16 Jan 2020 07:35:28 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=50.66.161.135; posting-account=lzqe5AoAAADHhp_gregSufVhvwu22fBS NNTP-Posting-Host: 50.66.161.135 References: <3c6f1486-293d-4eb5-a379-279b108248d8@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <4967a95b-3d79-4a5b-a30d-0f04f00ebbc4@googlegroups.com> Subject: Re: Tally From: Brad Moore Injection-Date: Thu, 16 Jan 2020 15:35:29 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:57851 Date: 2020-01-16T07:35:28-08:00 List-Id: 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