www.digitalmars.com         C & C++   DMDScript  

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

reply Era Scarecrow <rtcvb32 yahoo.com> writes:
downs wrote:
 bearophile wrote:
 I have already discussed about this topic a couple of

about :-)
 
 I think Python-like array comprehensions and the like

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


 
 
 Few examples in Python 3:
 
 # eager list (array) comp, 1D:
 a = [x * x for x in lazyit if x > 5]

/map/ ex!("x -> x*x");
 
 # eager list comp, 2D:
 parts = [[part.strip() for part in line.split()] for

 

"txt".read()).split("\n") /map/ (string s) { return s.split(" ").strip(); };
 # set comprehension
 squares = {x*x for x in xrange(100)}
 

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,




 flat = (abs(el) for row in mat for el in




 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)




--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
Jul 19 2008
next sibling parent reply JAnderson <ask me.com> writes:
Era Scarecrow wrote:
 downs wrote:
 bearophile wrote:
 I have already discussed about this topic a couple of

about :-)
 I think Python-like array comprehensions and the like

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

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]

/map/ ex!("x -> x*x");
 # eager list comp, 2D:
 parts = [[part.strip() for part in line.split()] for

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

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,




 flat = (abs(el) for row in mat for el in




 flat




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)




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

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
parent reply 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
parent JAnderson <ask me.com> writes:
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
prev sibling parent reply "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?

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
parent 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?

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 added are so nice :) That merely means the keyword 'lazy' is used; I kinda knew lazy wouldn't be the keyword used, if implemented. Perhaps something like CompileTime? However, the question still arises, can we embed a function (or call a function already made) to do the work for us? (For security reasons i'd say it would probably have to be something quickly and easily compiled (perhaps D Scripted?), but would not be able to do any weird memory accesses, or file IO) Era
Jul 20 2008