From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: project euler 26 Date: Fri, 8 Sep 2023 09:23:13 +0200 Organization: A noiseless patient Spider Message-ID: References: <878r9mudvj.fsf@bsb.me.uk> <87a5u1u1yv.fsf@bsb.me.uk> <8734ztttpc.fsf@bsb.me.uk> <87fs3ssl6v.fsf@bsb.me.uk> <87a5u0rts0.fsf@bsb.me.uk> <87jzt3qqlb.fsf@bsb.me.uk> <87o7ieq3ne.fsf@bsb.me.uk> <8734zpo3fz.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 8 Sep 2023 07:23:13 -0000 (UTC) Injection-Info: dont-email.me; posting-host="58fa476a7c15b05a127e12ddc7037961"; logging-data="3554729"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RfISsg3mXd/FeDkw2E0GSWC5Me8b/30g=" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Cancel-Lock: sha1:97s7SpA+iCoQQcgz0OYioVe/SDU= In-Reply-To: <8734zpo3fz.fsf@bsb.me.uk> Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:65620 List-Id: On 2023-09-08 03:32, Ben Bacarisse wrote: > "Dmitry A. Kazakov" writes: > >> On 2023-09-07 01:32, Ben Bacarisse wrote: >>> "Dmitry A. Kazakov" writes: >>> >>>> On 2023-09-06 17:16, Ben Bacarisse wrote: >>>> >>> Second, if I try to use a Vector rather than an Ordered_Map, I am told >>> that: >>> test2.adb:97:05: error: instantiation error at line 12 >>> test2.adb:97:05: error: no visible subprogram matches the specification for "<" >>> It would seem that vector cursors can't be compared using < (at least by >>> default). Maybe the installation needs more arguments. >> >> Vector has a proper index type. All you have to do is. Given >> >> package Integer_Vectors is >> new Ada.Containers.Vectors (Integer, Integer); >> >> Wrap Element into a function: >> >> V : Integer_Vectors.Vector; >> function Element (Index : Integer) return Integer is >> begin >> return V.Element (Index); >> end Element; >> ... >> >> and use the wrapper. > > Sure, but the hope was to write something that does not need new > code for new situations. That's what makes it reusable. Why should it be? You wanted to find maximum of a function. Vector is not a function. It is in mathematical terms, but not in the language terms. The abstraction for finding maximum in a container is just a different abstraction. >> Then you would pass Has_Element for it. For integers you would use wrapped >> X'Valid (there is no Integer'Valid, unfortunately. Only X'Valid where X is >> an object). > > It's definitely getting what I call cumbersome. Yes, because you try too hard to make it work where it probably should not. > I don't think this is incoherent. The Haskell libraries ensure that any > collection that is logically foldable is indeed foldable. Ada arrays and library containers do not share interfaces. [It is a long discussion how they could be]. But similarly, there is no shared interface between digits of a number in octal base and a container and a string in UTF-16 encoding. Should there be? No. Should the language allow adding ad-hoc interfaces to existing types. Yes, and this is possible in Ada in some very uncomfortable AKA cumbersome way, which is why "finding maximum" is not a worthy abstraction in Ada. >>> The bottom line is the last argument should be something very general >>> like the Period function. >>> A fix (though it's not really ideal) would be to use function >>> composition here (inventing . as the composition operator): >>> Map_Functions.Maximum_At (X.First, X.Last, Period'Access >>> . Element'Access) >>> but I don't think Ada has a function composition operator, does it? >> >> No as it would require closures. > > What closure is required for a function composition? There is no > environment to "close over". In Ada a function can use anything visible at its declaration point and at the location of its body. You can even declare a function inside a recursively called function and let it see local variables of each recursive call, in effect having an infinite set of functions. > That's a lot just to use something that is supposed to be reusable. [rant on] An Ada programmer would just write a loop. Abstractions are meant to abstract the problem domain. If you starting abstract elementary programming activities, then there might be something wrong with the language or with you. Then there is a point about readability. When I see a loop I say, aha this is what the guy is going to do. When I see a pile of calls of a dozen generic instances with arbitrary names, I begin to worry. In my view it is a road to nowhere, for an imperative language at least. The end of this road can be seen in modern C++. 20 years ago C++ was severely crippled broken but sometimes enjoyable language. You could *read* a C++ program. In these days with all libraries it became Forth on steroids. There is no such thing as a C++ program anymore, just calls upon calls. [rant off] > It only occurred to me after writing the non-generic solution. I > remember Ada as being something of a pioneer in it's attempt to provide > generic solutions, so I wondered how far things had come. I don't think > something really widely reusable is possible in this case. As I said you think in a wrong direction of abstracting the language "finding maximum" rather than the problem space, e.g. generalization to other bases, other mathematical structures etc. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de