Re: list comprehensions
Piers Cawley pointed out: %a_students = grep {.key ~~ :i/^a/}, %grades.kv; I think you could probably get away without the .kv there since, in a list context you're going to get a list of pairs anyway. In fact, the code is invalid as it stands. The following variations work as desired: %a_students = grep {.key ~~ /:i^a/} %grades; or: %a_students = grep {.key ~~ m:i/^a/} %grades; or: %a_students = grep sub($k,$v){$k ~~ m:i/^a/}, %grades.kv; Damian
Re: list comprehensions
On 2002-11-06 at 11:43:20, Jonathan Scott Duff wrote: Will there be some shorter-hand way to say these? @a = @grades[grep $_ = 90, @grades]; @b = @grades[grep 80 = $_ 90, @grades]; @c = @grades[grep 70 = $_ 80, @grades]; I think what you mean here is just @a = grep $_ = 90, @grades; etc. grep returns the actual elements, not their indices, so it doesn't make sense to use them as a slice. -- Mark REED| CNN Internet Technology 1 CNN Center Rm SW0831G | [EMAIL PROTECTED] Atlanta, GA 30348 USA | +1 404 827 4754
Re: list comprehensions
On Wed, Nov 06, 2002 at 12:54:12PM -0500, Mark J. Reed wrote: On 2002-11-06 at 11:43:20, Jonathan Scott Duff wrote: Will there be some shorter-hand way to say these? @a = @grades[grep $_ = 90, @grades]; @b = @grades[grep 80 = $_ 90, @grades]; @c = @grades[grep 70 = $_ 80, @grades]; I think what you mean here is just @a = grep $_ = 90, @grades; etc. grep returns the actual elements, not their indices, so it doesn't make sense to use them as a slice. Er, yeah. I must be subcaffienated right now. :-( What I was trying to get at was the ability to slice based on the values instead of the indices. But maybe it's just the python programming that I've been doing that makes me think it's useful. -Scott -- Jonathan Scott Duff [EMAIL PROTECTED]
Re: list comprehensions
Jonathan Scott Duff [EMAIL PROTECTED] writes: Will there be some shorter-hand way to say these? @a = @grades[grep $_ = 90, @grades]; @b = @grades[grep 80 = $_ 90, @grades]; @c = @grades[grep 70 = $_ 80, @grades]; Granted, it's fairly compact as it is but I'm wondering if there's some way to not have to mention @grades twice per statement. What's wrong with @a = grep { $_ = 90 } @grades; @b = grep { 80 = $_ 90 } @grades; @c = grep { 70 = $_ 80 } @grades; Or am I missing something? The examples you give seem to imply that you should use the value of the things contained in @grades as indices into @grades, and return the values thus indexed. There may be a good reason for doing this, but one escapes me for now. Something like: @a = @grades[$^_ = 90]; @b = @grades[80 = $^_ 90]; @c = @grades[70 = $^_ 80]; BTW, is there some other name for these things? I only know to call them list comprehensions from python. I've used the concept in other languages as well but never was it named. I confess I never quite understood why the python folks were so proud of list comprehensions, AFAICT they're just 'grep' and 'map' given fancy descriptions. -- Piers It is a truth universally acknowledged that a language in possession of a rich syntax must be in need of a rewrite. -- Jane Austen?
Re: list comprehensions
[EMAIL PROTECTED] (Piers Cawley) writes: I confess I never quite understood why the python folks were so proud of list comprehensions, AFAICT they're just 'grep' and 'map' given fancy descriptions. Well, sort of. They're more like this: @array[grep { func() } 0..$#array] -- If you want to travel around the world and be invited to speak at a lot of different places, just write a Unix operating system. (By Linus Torvalds)
Re: list comprehensions
Jonathan Scott Duff wrote: On Wed, Nov 06, 2002 at 12:54:12PM -0500, Mark J. Reed wrote: On 2002-11-06 at 11:43:20, Jonathan Scott Duff wrote: Will there be some shorter-hand way to say these? a = grades[grep $_ = 90, grades]; b = grades[grep 80 = $_ 90, grades]; c = grades[grep 70 = $_ 80, grades]; I think what you mean here is just a = grep $_ = 90, grades; etc. grep returns the actual elements, not their indices, so it doesn't make sense to use them as a slice. Er, yeah. I must be subcaffienated right now. :-( What I was trying to get at was the ability to slice based on the values instead of the indices. But maybe it's just the python programming that I've been doing that makes me think it's useful. -Scott I think that if there were a slice-based form of grep, it would most likely look like you are indexing by a subroutine (or method) reference that takes no arguments other than an element of the array. Something like: a = grades[{$^x 90}]; or a = grades[- x { $x 90}]; or my StudentGrade grades; a = grades[.isPassing]; I could see that as being useful. Of potentially more use would be: %grades = { Tom = 85, Mary = 95 }; %a_students = %grades{- $name, $grade { $grade 90 } }; print keys %a_students; # 'Mary'
Re: list comprehensions
Piers Cawley wrote: Jonathan Scott Duff [EMAIL PROTECTED] writes: Will there be some shorter-hand way to say these? @a = @grades[grep $_ = 90, @grades]; @b = @grades[grep 80 = $_ 90, @grades]; @c = @grades[grep 70 = $_ 80, @grades]; Granted, it's fairly compact as it is but I'm wondering if there's some way to not have to mention @grades twice per statement. What's wrong with @a = grep { $_ = 90 } @grades; @b = grep { 80 = $_ 90 } @grades; @c = grep { 70 = $_ 80 } @grades; Or am I missing something? The examples you give seem to imply that you should use the value of the things contained in @grades as indices into @grades, and return the values thus indexed. There may be a good reason for doing this, but one escapes me for now. I don't see why I'd want to do it with arrays, but... %a_students = %grades{grep /^a/i, keys %grades};
Re: list comprehensions
--- Buddha Buck [EMAIL PROTECTED] wrote: I think that if there were a slice-based form of grep, it would most likely look like you are indexing by a subroutine (or method) reference that takes no arguments other than an element of the array. Something like: @a = @grades[{$^x 90}]; or @a = @grades[- x { $x 90}]; or my StudentGrade @grades; @a = @grades[.isPassing]; I could see that as being useful. Of potentially more use would be: %grades = { Tom = 85, Mary = 95 }; %a_students = %grades{- $name, $grade { $grade 90 } }; print keys %a_students; # 'Mary' This is what makes designing an OODB so much fun. for keys %minima - $gr { @grades{ $gr } = grep { .getGrade() = $minima[$gr] }, @class; } This kind of searching is too easy in a transparent language like SQL, SELECT * FROM class WHERE curr_grade = %min, but it's miserable when you have to honor the objects. Being able to say something like: @grades{ $gr } = @class «st» {.getGrade() = $minima[$gr]}; makes for a decent start. Combining vectorizing with objects can help us build a more intuitive way to talk about operations over collections. Which kind of suggests that that idea of a standardized Collections interface isn't so unreasonable, after all -- I think there's value in being able to invoke map or grep or zip, etc., on an arbitrary collector (array, hash, lol, stream) and have it work in a consistent, predictable manner. (But this kind of thing can be a module, or be an extension, so long as the core collection types aren't blatantly incompatible.) («st» is, of course, the vectorized such that operator (aka grep). I shan't demand a single-character punctuation operator... :0) =Austin __ Do you Yahoo!? HotJobs - Search new jobs daily now http://hotjobs.yahoo.com/
Re: list comprehensions
Will there be some shorter-hand way to say these? [list comprehensions] (bb clarified that this is about hash slicing.) From A2: RFC 201: Hash Slicing ...Concise list comprehensions will require some other syntax within the subscript... And There are many ways we could reintroduce a slicing syntax ... but we'll defer the decision on that till Apocalypse 9 -- ralph
Re: list comprehensions
You know, guys, I already discussed this one in A4 or thereabouts. It's the use of an explicit boolean operator as a subscript that triggers selection. Larry
Re: list comprehensions
I don't see why I'd want to do it with arrays, but... %a_students = %grades{grep /^a/i, keys %grades}; Looks like that's just the same as %a_students = grep {.key ~~ :i/^a/}, %grades.kv; (after adjusting for perl6 syntax for a few things) -- Adam Lopresto ([EMAIL PROTECTED]) http://cec.wustl.edu/~adam/ Aleph-null bottles of beer on the wall, Aleph-null bottles of beer, You take one down, and pass it around, Aleph-null bottles of beer on the wall.
Re: list comprehensions
On Wed, Nov 06, 2002 at 11:36:50AM -0800, Larry Wall wrote: You know, guys, I already discussed this one in A4 or thereabouts. It's the use of an explicit boolean operator as a subscript that triggers selection. I thought so, but I couldn't find it. thanks, -Scott -- Jonathan Scott Duff [EMAIL PROTECTED]
Re: list comprehensions
Adam D. Lopresto [EMAIL PROTECTED] writes: I don't see why I'd want to do it with arrays, but... %a_students = %grades{grep /^a/i, keys %grades}; Looks like that's just the same as %a_students = grep {.key ~~ :i/^a/}, %grades.kv; I think you could probably get away without the .kv there since, in a list context you're going to get a list of pairs anyway. -- Piers It is a truth universally acknowledged that a language in possession of a rich syntax must be in need of a rewrite. -- Jane Austen?
Re: List Comprehensions (from Python)
"raptor" [EMAIL PROTECTED] writes: | hi, | I haven't used Python... but last days I read some stuff, wanted to compare | both languages for myself and found something interesting. | They are proposing extentinon to Pyhon 2 It's already in Python 2.0. -- http://www.dfan.org
Re: List Comprehensions (from Python)
On Sun, Oct 08, 2000 at 01:12:13PM +0100, raptor wrote: [expression for variable in sequence] Can this be done easly at the moment OR via some of the new proposals ?!!!? map { expression } sequence -- I used to be disgusted, now I find I'm just amused. -- Elvis Costello
Re: List Comprehensions (from Python)
Simon Cozens wrote: On Sun, Oct 08, 2000 at 01:12:13PM +0100, raptor wrote: [expression for variable in sequence] Can this be done easly at the moment OR via some of the new proposals ?!!!? map { expression } sequence See also RFC 81.