44# Distributed under the Boost Software License, Version 1.0.
55# (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
66
7- import assert ;
87import numbers ;
98import modules ;
109
11-
1210# Note that algorithms in this module execute largely in the caller's module
13- # namespace, so that local rules can be used as function objects. Also note that
14- # most predicates can be multi-element lists. In that case, all but the first
15- # element are prepended to the first argument which is passed to the rule named
16- # by the first element.
11+ # namespace, so that local rules can be used as function objects (predicates.)
12+ # Also note that predicates can be multi-element lists, any further argument
13+ # to be passed is simply appended before the call.
1714
1815
19- # Return the elements e of $( sequence) for which [ $(predicate) e ] has a
16+ # Return the elements e of ' sequence' for which [ $(predicate) e ] has a
2017# non-null value.
2118#
2219rule filter ( predicate + : sequence * )
@@ -34,9 +31,8 @@ rule filter ( predicate + : sequence * )
3431 return $(result) ;
3532}
3633
37-
38- # Return a new sequence consisting of [ $(function) $(e) ] for each element e of
39- # $(sequence).
34+ # Return a new sequence consisting of [ $(function) e ] for each
35+ # element e of 'sequence'.
4036#
4137rule transform ( function + : sequence * )
4238{
@@ -55,7 +51,7 @@ if [ HAS_NATIVE_RULE sequence : transform : 1 ]
5551 NATIVE_RULE sequence : transform ;
5652}
5753
58- # Returns the elements of 's' in reverse order
54+ # Returns the elements of 's' in reverse order.
5955rule reverse ( s * )
6056{
6157 local r ;
@@ -66,7 +62,9 @@ rule reverse ( s * )
6662 return $(r) ;
6763}
6864
69-
65+ # NOTE: This is primarily for internal use, but cannot be a local rule
66+ # otherwise neither insertion-sort nor merge rules will be able
67+ # to find it in caller module.
7068rule less ( a b )
7169{
7270 if $(a) < $(b)
@@ -75,8 +73,8 @@ rule less ( a b )
7573 }
7674}
7775
78-
79- # Insertion-sort s using the BinaryPredicate ordered .
76+ # Insertion-sort 's' using the 'ordered' predicate or
77+ # lexicagraphically (i.e. using '<') when no predicate is supplied .
8078#
8179rule insertion-sort ( s * : ordered * )
8280{
@@ -87,7 +85,6 @@ rule insertion-sort ( s * : ordered * )
8785 else
8886 {
8987 local caller = [ CALLER_MODULE ] ;
90- ordered ?= sequence.less ;
9188 local result = $(s[1]) ;
9289 if $(ordered) = sequence.less
9390 {
@@ -123,25 +120,25 @@ rule insertion-sort ( s * : ordered * )
123120 }
124121}
125122
126-
127- # Merge two ordered sequences using the BinaryPredicate ordered .
123+ # Merge two ordered sequences using the 'ordered' predicate or
124+ # lexicagraphically (i.e. using '<') when no predicate is supplied .
128125#
129126rule merge ( s1 * : s2 * : ordered * )
130127{
131128 ordered ?= sequence.less ;
132- local result__ ;
129+ local result ;
133130 local caller = [ CALLER_MODULE ] ;
134131
135132 while $(s1) && $(s2)
136133 {
137134 if [ modules.call-in $(caller) : $(ordered) $(s1[1]) $(s2[1]) ]
138135 {
139- result__ += $(s1[1]) ;
136+ result += $(s1[1]) ;
140137 s1 = $(s1[2-]) ;
141138 }
142139 else if [ modules.call-in $(caller) : $(ordered) $(s2[1]) $(s1[1]) ]
143140 {
144- result__ += $(s2[1]) ;
141+ result += $(s2[1]) ;
145142 s2 = $(s2[2-]) ;
146143 }
147144 else
@@ -150,13 +147,14 @@ rule merge ( s1 * : s2 * : ordered * )
150147 }
151148
152149 }
153- result__ += $(s1) ;
154- result__ += $(s2) ;
150+ result += $(s1) ;
151+ result += $(s2) ;
155152
156- return $(result__ ) ;
153+ return $(result ) ;
157154}
158155
159- # Compares two sequences lexicagraphically
156+ # Compares two sequences using the 'ordered' predicate or
157+ # lexicagraphically (i.e. using '<') when no predicate is supplied.
160158#
161159rule compare ( s1 * : s2 * : ordered * )
162160{
@@ -193,16 +191,14 @@ rule compare ( s1 * : s2 * : ordered * )
193191 }
194192}
195193
196- # Join the elements of s into one long string. If joint is supplied, it is used
197- # as a separator.
194+ # Join the elements of 's' using 'joint' as separator if supplied.
198195#
199196rule join ( s * : joint ? )
200197{
201198 joint ?= "" ;
202199 return $(s:J=$(joint)) ;
203200}
204201
205-
206202# Find the length of any sequence.
207203#
208204rule length ( s * )
@@ -215,15 +211,13 @@ rule length ( s * )
215211 return $(result) ;
216212}
217213
218- # Removes duplicates from 'list'. If 'stable' is
219- # passed, then the order of the elements will
220- # be unchanged.
214+ # Removes duplicates from 'list'. If 'stable' is passed,
215+ # then the order of the elements will be unchanged.
221216#
222217# rule unique ( list * : stable ? )
223218
224219NATIVE_RULE sequence : unique ;
225220
226-
227221# Returns the maximum number in 'elements'. Uses 'ordered' for comparisons or
228222# 'numbers.less' if none is provided.
229223#
@@ -242,34 +236,16 @@ rule max-element ( elements + : ordered ? )
242236 return $(max) ;
243237}
244238
245-
246239# Returns all of 'elements' for which corresponding element in parallel list
247240# 'rank' is equal to the maximum value in 'rank'.
248241#
249- rule select-highest-ranked ( elements * : ranks * )
250- {
251- if $(elements)
252- {
253- local max-rank = [ max-element $(ranks) ] ;
254- local result ;
255- while $(elements)
256- {
257- if $(ranks[1]) = $(max-rank)
258- {
259- result += $(elements[1]) ;
260- }
261- elements = $(elements[2-]) ;
262- ranks = $(ranks[2-]) ;
263- }
264- return $(result) ;
265- }
266- }
267- NATIVE_RULE sequence : select-highest-ranked ;
242+ # rule select-highest-ranked ( elements * : ranks * )
268243
244+ NATIVE_RULE sequence : select-highest-ranked ;
269245
270246rule __test__ ( )
271247{
272- # Use a unique module so we can test the use of local rules.
248+ # Use a module so we can test the use of local rules.
273249 module sequence.__test__
274250 {
275251 import assert ;
0 commit comments