Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- An offer for software developers: free IRC logging
- Sprixel, a 6 compiler powered by JavaScript
- Announcing try.rakudo.org, an interactive Perl 6 shell in your browser
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- This Week's Contribution to Perl 6 Week 5: Implement Str.trans
- This Week's Contribution to Perl 6
- This Week's Contribution to Perl 6 Week 8: Implement $*ARGFILES for Rakudo
- This Week's Contribution to Perl 6 Week 6: Improve Book markup
- This Week's Contribution to Perl 6 Week 2: Fix up a test
- This Week's Contribution to Perl 6 Week 9: Implement Hash.pick for Rakudo
- This Week's Contribution to Perl 6 Week 11: Improve an error message for Hyper Operators
- This Week's Contribution to Perl 6 - Lottery Intermission
- This Week's Contribution to Perl 6 Week 3: Write supporting code for the MAIN sub
- This Week's Contribution to Perl 6 Week 1: A website for proto
- This Week's Contribution to Perl 6 Week 4: Implement :samecase for .subst
- This Week's Contribution to Perl 6 Week 10: Implement samespace for Rakudo
- This Week's Contribution to Perl 6 Week 7: Implement try.rakudo.org
- What is the "Cool" class in Perl 6?
- Report from the Perl 6 Hackathon in Copenhagen
- Custom operators in Rakudo
- A Perl 6 Date Module
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- The case for distributed version control systems
- Perl 6: Failing Softly with Unthrown Exceptions
- Perl 6 Compiler Feature Matrix
- The first Perl 6 module on CPAN
- A Foray into Perl 5 land
- Gabor: Keep going
- First Grant Report: Structured Error Messages
- Second Grant Report: Structured Error Messages
- Third Grant Report: Structured Error Messages
- Fourth Grant Report: Structured Error Messages
- Google Summer of Code Mentor Recap
- How core is core?
- How fast is Rakudo's "nom" branch?
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- List.classify
- Longest Palindrome by Regex
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Monetize Perl 6?
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- My first YAPC - YAPC::EU 2010 in Pisa
- Trying to implement new operators - failed
- Programming Languages Are Not Zero Sum
- Perl 6 notes from February 2011
- Notes from the YAPC::EU 2010 Rakudo hackathon
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Pascal's Triangle in Perl 6
- Perl 6 in 2009
- Perl 6 in 2010
- Perl 6 in 2011 - A Retrospection
- Perl 6 ticket life cycle
- The Perl Survey and Perl 6
- The Perl 6 Advent Calendar
- Perl 6 Questions on Perlmonks
- Physical modeling with Math::Model and Perl 6
- How to Plot a Segment of a Circle with SVG
- Protected Attributes Make No Sense
- Publicity for Perl 6
- PVC - Perl 6 Vocabulary Coach
- Fixing Rakudo Memory Leaks
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- My personal "I want a PONIE" wish list for Rakudo Star
- Rakudo's rough edges
- Rats and other pets
- The Real World Strikes Back - or why you shouldn't forbid stuff just because you think it's wrong
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- The State of Regex Modifiers in Rakudo
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Thoughts on masak's Perl 6 Coding Contest
- The Three-Fold Function of the Smart Match Operator
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- Want to write shiny SVG graphics with Perl 6? Port Scruffy!
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why Rakudo needs NQP
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
- You are good enough!
Sat, 08 May 2010
List.classify
Permanent link
Yesterday I implement
a nice Perl 6 built-in called classify.
Just like map it takes a closure and a list (or can be called
as a method on a list). Unlike map it doesn't just return the values from the
closure calls, but instead it constructs Pair objects from them,
and groups them by equal keys. Here's one example:
say <red brown blue yellow black>.classify({ $_.substr(0, 1)}).perl; # output: # ("y" => ["yellow"], "b" => ["brown", "blue", "black"], "r" => ["red"])
This takes a list of color names, and groups them by their first letter. It returns a list of pairs, of which the key is always the first character (returned by the closure), the value is an array of the input values that have this first character.
Of course you can assign the result to a hash, and index by key you're interested in:
my %h = <red brown blue yellow black>.classify({ $_.substr(0, 1)}); say %h<b>.join(', '); # output: # brown, blue, black
So far I haven't come up with a real-world use case for
classify, but I strongly suspect I'll run across one soon,
and I kinda like the function.
Have you written any code lately where such a built-in would have been beneficial?
Comments / Trackbacks:
Trackback URL:
/blog-en/perl-6/list-classify.trackback
Sam wrote
Pretty much every program I write.
I'm not kidding, there's hardly a project I have that doesn't at some point need to, yet again, iterate over an array, pushing elements onto a hash of arrays.
The only thing that would be nice is a mechanism to allow you to map at the same time, so that the item pushed onto the arrays doesn't have to be the original item.
careytilden wrote
What Sam said
I would use this constantly. I might even throw a Perl 5 workalike into my standard toolkit.
Ricardo Signes wrote
yup, definitely
This is a better version of List::MoreUtils::part, which I wrote for perl5 and find pretty useful. Classifying into a hash rather than an array would clearly be more often useful.
colomon wrote
The specific example that made me long for this function is sorting loops on a topological face. Basically, you need to separate the counterclockwise and clockwise loops, and then look for which counterclockwise loops contain which clockwise ones. Figuring out whether a loop is clockwise is relatively expensive, so being able to go through the list of loops once and split it into two is very handy.
Darren Duncan wrote
I've already written a map-like classify function that groups into hashes, sort of. See my Set::Relation Perl 5 module. The version with map is called 'classification' and the one that just groups by values is called 'group' etc.
fagzal wrote
nice thing
I had a project where we got a list of words from SQL, which we wanted to list according to their first letter. Look at this:
http://idezetek.org/authors
So the example you used is a very good one :)
Sam wrote
Set::Relation
Darren: Set::Relation looks a bit heavy-weight and overkill for just this one task, however you've just successfully pushed back one of my other projects by at least a week, because it looks like it's an ideal fit and I won't be able to resist rewriting everything. :P
Klamsi wrote
I sometimes need this feature when selecting rows from Database.
Sometimes you need a mapping with:
column_value => [row, row, ...]
Klamsi wrote
For example think of a hierarchical forum and you want to select a whole thread.
To display the tree you can use a data structure like
%posts = @rows.classify({ $_.parent_id });
Paul "LeoNerd" Evans wrote
That looks like a Perl6 version of the partition_by functional, in Perl5's List::UtilsBy. Quite like List::MoreUtils::part() except it too has the hash valued result
Vinay wrote
This is equivalent to groovy groupBy method.
Write a comment
The comments on this blog post have been disabled; the comment form below will not work.