2 * Copyright Vladimir Prus 2003.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
11 #include "../compile.h"
17 # define max(a,b) ((a)>(b)?(a):(b))
21 LIST
* sequence_select_highest_ranked( FRAME
* frame
, int flags
)
23 /* Returns all of 'elements' for which corresponding element in parallel */
24 /* list 'rank' is equal to the maximum value in 'rank'. */
26 LIST
* const elements
= lol_get( frame
->args
, 0 );
27 LIST
* const rank
= lol_get( frame
->args
, 1 );
30 int highest_rank
= -1;
33 LISTITER iter
= list_begin( rank
);
34 LISTITER
const end
= list_end( rank
);
35 for ( ; iter
!= end
; iter
= list_next( iter
) )
37 int const current
= atoi( object_str( list_item( iter
) ) );
38 highest_rank
= max( highest_rank
, current
);
43 LISTITER iter
= list_begin( rank
);
44 LISTITER
const end
= list_end( rank
);
45 LISTITER elements_iter
= list_begin( elements
);
46 LISTITER
const elements_end
= list_end( elements
);
47 for ( ; iter
!= end
; iter
= list_next( iter
), elements_iter
=
48 list_next( elements_iter
) )
49 if ( atoi( object_str( list_item( iter
) ) ) == highest_rank
)
50 result
= list_push_back( result
, object_copy( list_item(
57 LIST
* sequence_transform( FRAME
* frame
, int flags
)
59 LIST
* function
= lol_get( frame
->args
, 0 );
60 LIST
* sequence
= lol_get( frame
->args
, 1 );
62 OBJECT
* function_name
= list_front( function
);
63 LISTITER args_begin
= list_next( list_begin( function
) ), args_end
= list_end( function
);
64 LISTITER iter
= list_begin( sequence
), end
= list_end( sequence
);
65 RULE
* rule
= bindrule( function_name
, frame
->prev
->module
);
67 for ( ; iter
!= end
; iter
= list_next( iter
) )
73 inner
->prev_user
= frame
->prev_user
;
74 inner
->module
= frame
->prev
->module
;
76 lol_add( inner
->args
, list_push_back( list_copy_range( function
, args_begin
, args_end
), object_copy( list_item( iter
) ) ) );
77 result
= list_append( result
, evaluate_rule( rule
, function_name
, inner
) );
88 char const * args
[] = { "elements", "*", ":", "rank", "*", 0 };
89 declare_native_rule( "sequence", "select-highest-ranked", args
,
90 sequence_select_highest_ranked
, 1 );
93 char const * args
[] = { "function", "+", ":", "sequence", "*", 0 };
94 declare_native_rule( "sequence", "transform", args
,
95 sequence_transform
, 1 );