Possible Lazy Keyword? (Re: Array comprehensions and the like)
Era Scarecrow
rtcvb32 at yahoo.com
Sat Jul 19 08:39:42 PDT 2008
downs wrote:
> bearophile wrote:
> > I have already discussed about this topic a couple of
> times in the past, but this time I have more things to talk
> about :-)
> >
> > I think Python-like array comprehensions and the like
> are quite useful, their light syntax replaces the need for
> map, filter, xfilter, xmap, some usages of flatten,
> xflatten, and some more things, while being quite readable
> still and short.
> >
> > Recently a DMDScript too has gained array
> comprehensions:
> >
> http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Array_comprehensions
> >
> >
> > Few examples in Python 3:
> >
> > # eager list (array) comp, 1D:
> > a = [x * x for x in lazyit if x > 5]
> auto a = lazyit /select/ ex!("x -> x > 5")
> /map/ ex!("x -> x*x");
> >
> > # eager list comp, 2D:
> > parts = [[part.strip() for part in line.split()] for
> line in file("txt")]
> >
> auto parts = (cast(string)
> "txt".read()).split("\n") /map/
> (string s) { return s.split(" ").strip(); };
>
> > # set comprehension
> > squares = {x*x for x in xrange(100)}
> >
> auto squares = Range[100] /map/ ex!("x ->
> x*x");
>
> > # dict comprehension
> > multi = {c:c*i for i,c in enumerate("abcd")}
> >
> Can't do that yet. Sad.
> > # lazy, 1D
> >>>> mat = [[0, 3, 0, 3], [9, 8, 1, 4], [6, 7,
> 6, 6]]
> >>>> flat = (abs(el) for row in mat for el in
> row if el % 2)
> >>>> flat
>
> T[] iterToField(T)(void delegate(void delegate(T)) dg) {
> T[] res; dg((T t) { res ~= t; });
> return res;
> }
>
> auto mat = [[0, 3, 0, 3], [9, 8, 1, 4], [6, 7, 6, 6]];
> auto flat = (void delegate(ref int) dg) {
> foreach (ref row; mat) foreach (ref el; row) if (el % 2)
> dg(el);
> };
>
> > <generator object at 0x00A8E1E8>
> >>>> list(flat)
> > [3, 3, 9, 1, 7]
> >
> writefln(iterToField(flat));
>
> --downs
I do have a serious question then, why note have inserted code that can be run at compile-time in order to build the static data? Assume the following.
bool []primes = {0,0,1,1,0,1,0,1/*. etc. etc.*/} //0-7 primes, 1 is true
however i have a code that already gives me this information but i merely need it to be present elsewhere. If the 'lazy' keyword was introduced, Would it not be nifty if i could let the compiler do it for me?
//needed static declaration
bool []primes = lazy boolPrimes(100);
module ??.lazyTypes;
bool[] boolPrimes(int x)
{
bool []tmp = new bool[x + 1];
for (int cnt = 0; cnt < bool.length; cnt++)
tmp[cnt] = isprime(cnt);
return tmp;
}
or as:
bool[] primes = lazy(array dimentions) {
bool[(array dimension carryover)] lazy; //lazy array type from primes declaration, this you don't have to enter, will already be there.
for (int cnt = 0; cnt < lazy.length; cnt++)
lazy[cnt] = isprime(cnt);
//no need for a return, lazy will be returned automatically
}
Using in regular code it might be useful to use like this.
module ??.lazyPreDefs
lazy(dimentions) bool boolPrimes() //bool lazy(dimentions) ?? Perhaps this would be better?
{
//lazy already declared based on return type and dimentions
//return lazy by default
}
This may seem like more work up front, however you can later on optionally build more and more static building types and insert them into a module for the compiler, making the job simpler in the long run.
Perhaps named as 'lazy' might be a bad idea, and perhaps a better long term name would be better, but you get the idea.
Any comments?
Era
More information about the Digitalmars-d
mailing list