Hm, I envisioned a more spiced-up version when I read about partition, for doing equivalence classes. Perhaps not quite as commonly useful, though:
mapping(mixed:array) partition( array input, function classify ) { mapping result = ([]); foreach( input, mixed elem ) { mixed type = classify( elem ); if( has_index( result, type ) ) result[type] += ({ elem }); else result[type] = ({ elem }); } return result; }
array stuff = ({ 1, 4.0, "8", 2, Stdio.File("/dev/null"), 17, "!" }); partition( stuff, lambda( mixed x ) { return sprintf("%t", x); } );
([ "float":({ 4.0 }),
"int":({ 1, 2, 17 }), "object":({ Stdio.File("/dev/null", 0, 777 /* fd=10 */) }), "string":({ "8", "!" }) ])
/ Johan Sundström (utan sälskap)
Previous text:
2003-09-04 00:06: Subject: Re: Language comparisions
Sorry for my poor knowledge but what are three and four ways comparison functions ?
mixed compare(mixed a, mixed b) { if(uncomparable_types(a,b)) return CONST_P; if(a<b) return CONST_Q; if(a>b) return CONST_R; if(a==b) return CONST_S; error("uncomparable_types is broken\n"); }
What about ADT.Queue ?
You're right. That's the way to go.
Should we add Array.partition that splits an array into two (like filter with two outputs)?
Do you have an example ?
Array.partition( ({ 1,2,3,4,5,6,7 }), lambda(int x) { return x>3 && !(x%2); });
({ ({ 4,6 }), ({ 1,2,3,5,7 }) })
Should we add Array.exists/any to determine if at least one element satisfies a criterion and Array.every/all to determine if the all are?
Array.exists can be done with search, maybe you can add a sort of alias that makes a search. Array.every can be usefull.
No, search can only dermine the equality criterion. Array.exists( arr, `>, 17 )
BTW I think it would be good if more Pike developers could come on IRC more often, there are sometimes newbies on IRC (take Scandium example) and thus it shows IRC is usefull.
But the history function is usually broken in IRC. As is the log. Is there an IRC client in Pike so that at least the client side annoyances can be fixed easily?
/ Martin Nilsson (ja till euro, nej till cent)