On 29/10/2012, at 11:14 PM, john skaller wrote:
>
> Anyhow, it should be easier to write this filter.
> Something like
Well, a cleanup of the compiler unifying anonymous functions and objects
by adding a parmeter to EXPR_lambda, so now we can have anonymous
generators.
Now I can write the filter:
var f = gen ( var g : 1 -> opt[int] ) () : opt[int] = {
match ?x when x > 10 in g do yield Some x; done
return None[int];
};
And now, using that fantastic new syntax definition feature <grin> ..
syntax SSTREAM {
spattern := "?" sinteger ":" "pattern" =># "`(PARSER_ARGUMENT ,_2)";
}
syntax STREAM {
open syntax SSTREAM;
x[let_pri] := "stream-filter" "[" sexpr "]" spattern "=>" sexpr =>#
(
gen ( var g : 1 -> opt[?3] ) () : opt[?3] =
{
match ?5:pattern in g do yield Some ?7; done
return None[?3];
}
);
};
open syntax STREAM;
and here's how it looks:
// The list
var x = list (99,88,1,5,10,15,2,6,11,16);
// The old way
var y = list$ f x.iterator;
println$ y;
// The new way:
var z = list$ (stream-filter [int] ?x when x > 10 => x ) x.iterator;
println$ z;
Now, in case you didn't quite "grok" how awesome this actually is:
var z2 = list$
(stream-filter [int] ?x when x > 10 => x ) $
(stream-filter [int] ?x when x < 90 => x )
x.iterator
;
println$ z2;
So as you can see we can "chain" filters together.
So we have
(a) An iterator to start with to convert the list to a stream
(b) a series of filters
(c) a comprehension to convert the stream back to a list
I think it is unpleasant this goes right to left and that they're
left associative so you have to use the $ sign to fix it.
So just to prove how awesome Felix is:
x
. iterator
. (stream-filter [int] ?x when x < 90 => x )
. (stream-filter [int] ?x when x > 10 => x )
. list
. println
;
also works :) :)
Now, there's an important point here.
Why stream filters?
Why not ..
stream-map
stream-fold
stream-zip
And of course! There are more.
The REALLY fun one is
stream-split
Can you figure what that means? To take a stream and split it into two
streams .. how would that work?????
[HINT: a stream consumer is a procedure. To consume two streams
you need two procedures]
--
john skaller
[email protected]
http://felix-lang.org
------------------------------------------------------------------------------
The Windows 8 Center - In partnership with Sourceforge
Your idea - your app - 30 days.
Get started!
http://windows8center.sourceforge.net/
what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language