pike.git/
lib/
modules/
Array.pmod
Branch:
Tag:
Non-build tags
All tags
No tags
2003-09-04
2003-09-04 13:00:38 by Johan Sundström <oyasumi@gmail.com>
4d218bbf45be028f870f09ca050f9df7da2f12c5 (
22
lines) (+
22
/-
0
)
[
Show
|
Annotate
]
Branch:
7.9
Array.partition might be handy once in a while too.
Rev: lib/modules/Array.pmod:1.93
778:
return 1; return 0; }
+
+
//! Splits an array in two, according to an arbitration function
+
//! @[arbiter]. The elements in @[a] who return non-zero for the
+
//! expression @[arbiter]( @[a][@i{i@}], @extra_args ) end up in
+
//! the first sub-array, the others in the second. The order is
+
//! preserved from the original array.
+
//! @example
+
//! Array.partition( enumerate( 9 ), lambda(int n) { return n>3 && n<7; } );
+
//! > ({ ({ 4, 5, 6 }), ({ 0, 1, 2, 3, 7, 8 }) })
+
//! @seealso
+
//! @[filter], @[`/], @[`%]
+
array(array) partition( array a, function arbiter, mixed ... extra_args )
+
{
+
array first = ({}), second = ({});
+
foreach( a, mixed elem )
+
if( arbiter( elem, @extra_args ) )
+
first += ({ elem });
+
else
+
second += ({ elem });
+
return ({ first, second });
+
}