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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,51bff7cd4c35a15d X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.196.232 with SMTP id ip8mr4644190pbc.6.1338365423631; Wed, 30 May 2012 01:10:23 -0700 (PDT) Path: pr3ni65252pbb.0!nntp.google.com!news1.google.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Martin Newsgroups: comp.lang.ada Subject: Re: Ada2012 : In praise of 'for ... of ... loop'... Date: Wed, 30 May 2012 01:10:22 -0700 (PDT) Organization: http://groups.google.com Message-ID: <371a4b67-1969-4cd5-90f4-d58a9b276f29@googlegroups.com> References: <74e4e6b5-20bd-4388-b4a0-dfbecc8070be@googlegroups.com> <4fc4e51d$0$6566$9b4e6d93@newsspool4.arcor-online.net> NNTP-Posting-Host: 20.133.0.8 Mime-Version: 1.0 X-Trace: posting.google.com 1338365423 24400 127.0.0.1 (30 May 2012 08:10:23 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 30 May 2012 08:10:23 +0000 (UTC) In-Reply-To: <4fc4e51d$0$6566$9b4e6d93@newsspool4.arcor-online.net> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=20.133.0.8; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G User-Agent: G2/1.0 Content-Type: text/plain; charset=ISO-8859-1 Date: 2012-05-30T01:10:22-07:00 List-Id: On Tuesday, May 29, 2012 4:02:47 PM UTC+1, Georg Bauhaus wrote: > On 29.05.12 16:29, Martin wrote: > > This addition to Ada is brilliant...I've just been refactoring some Ada05 into Ada2012 and this just makes the code so much clearer! No more nested subprograms (of nested subprograms (of nested ...)). > > > > -- Martin > > Could you illustrate a bit? How much of the clarity is caused, if > so, by being shorter, using assignment in place of nested subprograms, > reducing the number of names, or ... Here's a very small example... what we have now: procedure Save (This : in out Configuration; Filename : String; Header_Comments : String_Vectors.Vector := String_Vectors.Empty_Vector; Include_Last_Saved_Time : Boolean := False) is use type String_Vectors.Vector; File : File_Type; begin begin Create (File, Out_File, Filename); exception when others => Open (File, Out_File, Filename); end; for Comment of Header_Comments loop Put_Line (File, Comment_Str & Comment); end loop; if Include_Last_Saved_Time then Put_Line (File, Comment_Str & "Last saved: " & Image (Date => Clock)); end if; for Section of This.Sections loop Put_Line (File, "[" & To_String (Section.Name) & "]"); for Pair of Section.Pairs loop Put_Line (File, To_String (Pair.Key) & "=" & To_String (Pair.Value)); end loop; end loop; Close (File); exception -- implementation detail end Save; what we used to have (mocked up!): procedure Save (This : in out Configuration; Filename : String; Header_Comments : String_Vectors.Vector := String_Vectors.Empty_Vector; Include_Last_Saved_Time : Boolean := False) is use type String_Vectors.Vector; File : File_Type; ----------- -- Write -- ----------- procedure Write (C : Section_Vectors.Cursor) is ------------------- -- Write_Comment -- ------------------- procedure Write_Comment (C : String_Vectors.Cursor) is begin Put_Line (File, Comment_Str & String_Vectors.Element (C)); end Write_Comment; ----------- -- Write -- ----------- procedure Write (C : Key_Value_Pair_Vectors.Cursor) is Pair : constant Key_Value_Pair := Key_Value_Pair_Vectors.Element (C); begin Put_Line (File, To_String (Pair.Key) & "=" & To_String (Pair.Value)); end Write; This_Section : constant Section := Section_Vectors.Element (C); begin Put_Line (File, "[" & To_String (This_Section.Name) & "]"); This_Section.Pairs.Iterate (Write'Access); end Write; begin begin Create (File, Out_File, Filename); exception when others => Open (File, Out_File, Filename); end; Header_Comments.Iterator (Write_Comment'Access); if Include_Last_Saved_Time then Put_Line (File, Comment_Str & "Last saved: " & Image (Date => Clock)); end if; This.Sections.Iterate (Write'Access); Close (File); exception -- implementation detail end Save; That's 54 lines down to 29 lines, no 'Access and it's perfectly clear what's happening and it happens in a single subprogram. No need to talk about Cursor or calls to Element (C)... ...It also enhances the value of other nested subprograms - they're probably doing something more interesting, so I don't mind the comment-box headers for them as much as I used to. All thanks to a little syntatic sugar. To my eyes, this a big win. -- Martin