comp.lang.ada
 help / color / mirror / Atom feed
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: project euler 26
Date: Thu, 07 Sep 2023 00:32:21 +0100	[thread overview]
Message-ID: <87o7ieq3ne.fsf@bsb.me.uk> (raw)
In-Reply-To: uda7c7$2ids2$2@dont-email.me

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On 2023-09-06 17:16, Ben Bacarisse wrote:
>
>> I am curious to know how reusable this is.  Can the packages be
>> instantiated in such a way that the argument ranges over the elements
>> of, say, and Ordered_Map?
>
> Sure:
>
>    with Ada.Containers.Ordered_Maps;
>
>    package Integer_Maps is
>       new Ada.Containers.Ordered_Maps (Integer, Integer);
>    use Integer_Maps;
>    package Cursor_Arguments is new Generic_Arguments (Cursor);

Ah!  So the arguments correspond to the "with" functions in the order
listed, and, since Cursor already has Next, there no need to specify
anything.  One could (I've just tried) use => notation.  You could have
written

  package Arguments is new Generic_Arguments (Next => Positive'Succ,
                                             Argument_Type => Positive);

in your first example -- swapping the order just to make the point
obvious.  This tripped me up when I was playing around with a Cursors
solution.

There are a couple of details that prevent your Maximum_At function from
working properly in this case though.  First, we can't have an empty
map, because X.Last can't be compared with X.First when either is
No_Element, so the test for Right < Left fails before the desired error
can be raised.

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.

Anyway, I am still not sure how to write a generic test for an empty
range.

>    package Map_Values is new Generic_Values (Integer);
>    package Map_Functions is
>       new Generic_Discrete_Comparable_Valued
>           (Cursor_Arguments, Map_Values);
>
> Then given X is a map: X : Map;
>
>    Map_Functions.Maximum_At (X.First, X.Last, Element'Access)

It's possible I was not clear about what I was aiming for.  I was hoping
to be able to find the maximum of some arbitrary function, taking the
function's arguments from any sequential collection.  Either a simple
range of values, an array or vector of values, a list of values or even
an ordered map of values -- any ordered list of values.

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?

Another solution would be to write Maximum_At so that it knows it has a
cursor argument, but then I don't think it would work for native arrays,
would it?  And we'd loose plain ranges altogether.

>> Maybe a more generic a solution would involve passing something that can
>> be iterated over, rather than two values of an "enumerated" type?  I
>> mean enumerated in the mathematical sense -- it may be the wrong word in
>> Ada.
>
> Yes, but Ada does not have built-in range types. Therefore such design will
> not work out of the box with discrete types because 2..999 is not a proper
> object in Ada. However, talking about abstractions, you can create an
> interval type for the purpose or else use an ordered set of integers.

But then (I think) the only function one could pass would be something
like Element as in you example above.  Using an ordered set of integers
would not allow

  Map_Functions.Maximum_At (Set.First, Set.Last, Period'Access)

would it?

>> I am asking you but I am also the group.  I appreciate your help,
>> but don't want you to feel any obligation to keep helping!
>
> No problem.

You seem to be on your own as far as helping out is concerned!

-- 
Ben.

  reply	other threads:[~2023-09-06 23:32 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04  9:19 project euler 26 CSYH (QAQ)
2023-09-04 11:06 ` Niklas Holsti
2023-09-04 12:39   ` Dmitry A. Kazakov
2023-09-04 16:01     ` Ben Bacarisse
2023-09-04 19:20       ` Dmitry A. Kazakov
2023-09-04 20:18         ` Ben Bacarisse
2023-09-04 21:00           ` Dmitry A. Kazakov
2023-09-04 23:16             ` Ben Bacarisse
2023-09-05  7:23               ` Dmitry A. Kazakov
2023-09-05 15:18                 ` Ben Bacarisse
2023-09-05 17:08                   ` Dmitry A. Kazakov
2023-09-06  1:10                     ` Ben Bacarisse
2023-09-06  7:06                       ` Dmitry A. Kazakov
2023-09-06 15:16                         ` Ben Bacarisse
2023-09-06 15:54                           ` Dmitry A. Kazakov
2023-09-06 23:32                             ` Ben Bacarisse [this message]
2023-09-07  9:02                               ` Dmitry A. Kazakov
2023-09-08  1:32                                 ` Ben Bacarisse
2023-09-08  7:23                                   ` Dmitry A. Kazakov
2023-09-09  0:25                                     ` Ben Bacarisse
2023-09-09  9:32                                       ` Dmitry A. Kazakov
2023-09-10  1:20                                         ` Ben Bacarisse
2023-09-10  8:46                                           ` Dmitry A. Kazakov
2023-09-10 19:22                                             ` Ben Bacarisse
2023-09-11  6:53                                               ` Dmitry A. Kazakov
2023-09-11 16:13                                                 ` Ben Bacarisse
2023-09-12  7:17                                                   ` Dmitry A. Kazakov
2023-09-13 12:24                                                     ` Ben Bacarisse
2023-09-14  6:33                                                       ` Dmitry A. Kazakov
2023-09-14 14:30                                                         ` Ben Bacarisse
2023-09-08  6:09                               ` G.B.
2023-09-08 21:02                                 ` Ben Bacarisse
2023-09-09  8:13                                   ` G.B.
2023-09-09 21:04                                     ` Ben Bacarisse
2023-09-10  9:11                                     ` Dmitry A. Kazakov
2023-09-05 17:35                 ` moi
2023-09-04 14:23 ` Dmitry A. Kazakov
2023-09-07  7:31 ` Francesc Rocher
2023-09-15  9:07   ` CSYH (QAQ)
2023-09-19  7:59     ` comp.lang.ada
replies disabled

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