The other day I was trying to find a way to find all possible combination of
2-4 particular letters of the alphabet (A, C, G, N, T), with repetition. The objective was
to try to build an associative array of letter combinations and the corresponding
ordered string. (See Sorting characters within a string).
Generating all combinations would usually takes a cartesian cross-product to be efficient (see japhy's post about that).
I came up with a simple but (very) inefficient method of doing the same thing:
I then began to wonder about the availability of a lazy form of evaluation for expression. For the uninitiated, lazy evaluation of an expression is delaying evaluation until the last moment, at which point you may realize that you only need to compute a part of the expression. The aim here would be to avoid storing the intermediary result of the .. operator in memory by applying the grep directly, as a filter function.
Well, programmers of Perl, rejoice. It seems Perl6 might have a lazy operator (see rfc123) that will allow to do exactly this. With the lazy operator, we could rewrite the previous piece of code like this:
Lazy evaluation is (to the best of my knowledge) mostly available in functionnal programming language. It is a powerful concept that can only reinforce the variety of Perl idioms you can use to easily solve complex problems.
So everything is good, Perl6 will allow us to be lazy and use lazy. I can't wait to see the impatience and hubris functions ;-)
Guillaume
Generating all combinations would usually takes a cartesian cross-product to be efficient (see japhy's post about that).
I came up with a simple but (very) inefficient method of doing the same thing:
This yields exactly what I want, but to the price of a high cost in memory and processing. The problem is with the .. operator. The number of elements generated before the grep by the .. operator is 361,173. After the grep you are left with only 774 "useful" strings. And if you wanted to get all strings up to 5 letters you would then have to deal with nearly 10,000,000 elements. And things only get worse.my @strings = (grep /[acgmt]{2}/, ('aa' .. 'tt'), grep /[acgmt]{3}/, ('aaa' .. 'ttt'), grep /[acgmt]{4}/, ('aaaa' .. 'tttt'));
I then began to wonder about the availability of a lazy form of evaluation for expression. For the uninitiated, lazy evaluation of an expression is delaying evaluation until the last moment, at which point you may realize that you only need to compute a part of the expression. The aim here would be to avoid storing the intermediary result of the .. operator in memory by applying the grep directly, as a filter function.
Well, programmers of Perl, rejoice. It seems Perl6 might have a lazy operator (see rfc123) that will allow to do exactly this. With the lazy operator, we could rewrite the previous piece of code like this:
This would causes a lazy list to be passed to the filter function grep, saving us from allocating the entire letter combinations array in memory. While this might not be the greatest example ever, I think it's simple enough to illustrate the possibilities.my @strings = (grep /[acgmt]{2}/, lazy ('aa' .. 'tt'), grep /[acgmt]{3}/, lazy ('aaa' .. 'ttt'), grep /[acgmt]{4}/, lazy ('aaaa' .. 'tttt'));
Lazy evaluation is (to the best of my knowledge) mostly available in functionnal programming language. It is a powerful concept that can only reinforce the variety of Perl idioms you can use to easily solve complex problems.
So everything is good, Perl6 will allow us to be lazy and use lazy. I can't wait to see the impatience and hubris functions ;-)
Guillaume
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Let's get lazy
by japhy (Canon) on Aug 28, 2001 at 20:47 UTC | |
Re: Let's get lazy
by Masem (Monsignor) on Aug 28, 2001 at 20:37 UTC | |
(MeowChow) Re: Let's get lazy
by MeowChow (Vicar) on Aug 28, 2001 at 22:10 UTC | |
Re: Let's get lazy
by dga (Hermit) on Aug 28, 2001 at 23:05 UTC | |
Re: Let's get lazy
by runrig (Abbot) on Nov 02, 2001 at 04:31 UTC | |
by tilly (Archbishop) on Nov 03, 2001 at 02:50 UTC | |
by tilly (Archbishop) on Nov 02, 2001 at 17:07 UTC | |
Re: Let's get lazy
by beretboy (Chaplain) on Nov 02, 2001 at 03:36 UTC | |
by blakem (Monsignor) on Nov 02, 2001 at 04:26 UTC |
Back to
Meditations