comp.lang.ada
 help / color / mirror / Atom feed
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: project euler 26
Date: Sun, 10 Sep 2023 02:20:20 +0100	[thread overview]
Message-ID: <87tts2kenf.fsf@bsb.me.uk> (raw)
In-Reply-To: udhe3n$u2l$1@dont-email.me

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

> On 2023-09-09 02:25, Ben Bacarisse wrote:
>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

>>> [rant on]
>>> An Ada programmer would just write a loop.
>> Yes, that's fine.  Different languages have different objectives.  Just
>> write the empty range test and the loop you need for each kind of
>> collection.
>
> You can loop in Ada over empty ranges, no problem.

Yes, but the problem in hand (maximum of F over X) should raise an error
on an empty X.  I know there are other options, but you chose to
raise an error so that's the design I was talking about.

>> That was definitely the way things were done in the 80s.
>
> Yes, before the Dark Ages of Computing...

Eh?  There have been repeated updates to the Ada language.  Are they
taking Ada into the dark ages?  If so, what was the golden age of Ada
when its design was perfect for numerical algorithms?

>>> 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.
>> Generalising to an arbitrary base is local to the function that finds
>> the answer for one element.  It's an entirely separate axis of
>> generalisation to that of where the elements come from.
>> It's interesting to me that you consider one simply wrong and the other
>> natural.
>
> Because one is a software design artifact and another is the result of
> problem space analysis.

Which one the wrong one?

>> In some languages the "wrong" one does not even merit
>> consideration as it's just there for free.
>
> Being a part of design it has all possible merits to consider and then
> reject it. That is in the position of a puzzle solver. Now in the position
> of a library designer, the Ada standard library has an [informal] interface
> that supports what you wanted:

Well, there has been some confusion over that, but from what I
understand of the code below, adding in a function to maximise would be
simple.

> 1. Cursor
> 2. Key at the cursor
> 3. Element at the cursor
> 4. Iterate procedure
>
> So, for the Ada standard library it might look like this:
>
> generic
>    type Container_Type (<>) is limited private;
>    type Element_Type is private;
>    type Key_Type is private;
>    type Cursor_Type is private;
>    with function "<" (Left, Right : Element_Type) return Boolean is <>;
>    with function Key (Position : Cursor_Type) return Key_Type is <>;
>    with function Element
>                  (  Position  : Cursor_Type
>                  )  return Element_Type is <>;
>    with procedure Iterate
>                   (  Container : Container_Type;
>                      Process   : not null access procedure
>                                  (Position : Cursor_Type)
>                   )  is <>;
> function Generic_Container_Maximum_At (Container : Container_Type)
>    return Key_Type;
>
> function Generic_Container_Maximum_At (Container : Container_Type)
>    return Key_Type is
>    Found  : Boolean := False;
>    Max    : Element_Type;
>    Result : Key_Type;
>    procedure Walker (Position : Cursor_Type) is
>    begin
>       if Found then
>          if Max < Element (Position) then
>             Result := Key (Position);
>             Max    := Element (Position);
>          end if;
>       else
>          Result := Key (Position);
>          Max    := Element (Position);
>          Found  := True;
>       end if;
>    end Walker;
> begin
>    Iterate (Container, Walker'Access);
>    if Found then
>       return Result;
>    else
>       raise Constraint_Error with "Empty container";
>    end if;
> end Generic_Container_Maximum_At;
>
> Instantiation:
>
>    package Integer_Maps is
>       new Ada.Containers.Ordered_Maps (Integer, Integer);
>    use Integer_Maps;
>    function Integer_Map_Max is
>       new Generic_Container_Maximum_At (Map, Integer, Integer, Cursor);

This is probably the closest we can get to a universal solution.
Vectors don't have a Key function but I am sure I could find out what
should be provided there.

Thanks.

I agree that is does not feel worth it.  Just write the loop our each time.

-- 
Ben.

  reply	other threads:[~2023-09-10  1:20 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
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 [this message]
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