digitalmars.D - Possible Lazy Keyword? (Re: Array comprehensions and the like)

• Era Scarecrow (33/100) Jul 19 2008 I do have a serious question then, why note have inserted code that can...
• JAnderson (5/120) Jul 19 2008 I guess that could be useful. One way you could do prime numbers is
• Era Scarecrow (5/8) Jul 19 2008 I wasn't meaning strictly prime numbers, it's just a good example to co...
• JAnderson (4/13) Jul 21 2008 Check out mixins. They are seriously a powerful concept and should
• Stewart Gordon (15/23) Jul 20 2008 "Era Scarecrow" wrote in message
• Era Scarecrow (5/30) Jul 20 2008 Very interesting. I'm currently still learning D, but some of the thing...
Era Scarecrow <rtcvb32 yahoo.com> writes:
```downs wrote:
bearophile wrote:

times in the past, but this time I have more things to talk

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)
(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")}

# 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.

Era

```
Jul 19 2008
```Era Scarecrow wrote:
downs wrote:
bearophile wrote:

times in the past, but this time I have more things to talk
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)
(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")}

# 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.

Era

I guess that could be useful.  One way you could do prime numbers is
with a string mixin.  However you couldn't do anything that relies on
stuff available at run-time with that.

-Joel
```
Jul 19 2008
Era Scarecrow <rtcvb32 yahoo.com> writes:
```JAnderson Wrote:
I guess that could be useful.  One way you could do prime numbers is
with a string mixin.  However you couldn't do anything that relies on
stuff available at run-time with that.

I wasn't meaning strictly prime numbers, it's just a good example to connect
code to. I was meaning, being able to use a compiled function (or a function we
can compile and run without having to keep) in order to fill in some static
data. Requirements? How about for those functions that you can duplicate your
data with only what you input to it, and no real library/filesystem help. But
most of those examples are mathematically easy to program.

At the very worse case i suppose you could just do a module level this() that
builds the data when the module is loaded, but some data is better off being
static to start with rather than filling in every time. That way i can have it
fill out X number range, and only give it in source code something like 1-2
functions or 400 bytes to fill in data that could take up a few K.

Could someone refer me to the 'maps' and other 'lazy' shortcuts that i see
mentioned in the other examples? They might be good to use at once time or
another.

Era
```
Jul 19 2008
```Era Scarecrow wrote:
JAnderson Wrote:
I guess that could be useful.  One way you could do prime numbers is
with a string mixin.  However you couldn't do anything that relies on
stuff available at run-time with that.

I wasn't meaning strictly prime numbers, it's just a good example to connect
code to. I was meaning, being able to use a compiled function (or a function we
can compile and run without having to keep) in order to fill in some static
data. Requirements? How about for those functions that you can duplicate your
data with only what you input to it, and no real library/filesystem help. But
most of those examples are mathematically easy to program.

Era

Check out mixins.  They are seriously a powerful concept and should
allow you to do what you want, albit a little verbose.

-Joel
```
Jul 21 2008
"Stewart Gordon" <smjg_1998 yahoo.com> writes:
```"Era Scarecrow" <rtcvb32 yahoo.com> wrote in message
news:mailman.31.1216481994.32098.digitalmars-d puremagic.com...
<snip>
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.

D already has something called compile-time function evaluation, which I
beleve can achieve things like this.
http://www.digitalmars.com/d/1.0/function.html#interpretation

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?

<snip>

D already has a 'lazy' keyword.
http://www.digitalmars.com/d/1.0/lazy-evaluation.html

But its semantics are nothing like what I can make of what you're
describing.

Stewart.

--
My e-mail address is valid but not my primary mailbox.  Please keep replies
on the 'group where everybody may benefit.
```
Jul 20 2008
Era Scarecrow <rtcvb32 yahoo.com> writes:
```Stewart Gordon Wrote:

"Era Scarecrow" <rtcvb32 yahoo.com> wrote in message
news:mailman.31.1216481994.32098.digitalmars-d puremagic.com...
<snip>
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.

D already has something called compile-time function evaluation, which I
beleve can achieve things like this.
http://www.digitalmars.com/d/1.0/function.html#interpretation

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?

<snip>

D already has a 'lazy' keyword.
http://www.digitalmars.com/d/1.0/lazy-evaluation.html

But its semantics are nothing like what I can make of what you're
describing.

Very interesting. I'm currently still learning D, but some of the things being