[F-Script-talk] Uniformly distributing results using message patterns
Brought to you by:
pmougin
From: Sarat K. <sar...@gm...> - 2006-04-04 03:01:08
|
Hi Guys, A couple of weeks ago I was trying to solve this problem of distributing results from different categories & sites to a fixed number of rows. This is similar to the way Spotlight uniformly distributes the search results in the menu like display. So you see results from each category (Photos, Documents, Folders, Music, Mail, etc). The case I was trying to solve is a little complicated because each result has an additional attribute "Site" apart from the "Category". Here is what I like to do. Say I have 10 rows to display the seach results and I get 40 results that match the search criteria. I want to make sure I display results uniformly from each Category and then uniformly across Sites in each category. F-Script seemed like a perform fit for these kinds of problems. Here is the code I came up with so far [:results | categoryResults :=3D results at: @ ((results objectForKey:'Category') =3D @((results objectForKey:'Category') distinct)). categorySiteResults :=3D [:a| Sites:=3D (a objectForKey:'Site') distinct. a at: @((a objectForKey:'Site') =3D @Sites)] value: @categoryResults. addBlock :=3D [:container :source | container addObject:(source objectAtIndex:0). source removeObjectAtIndex:0]. alternatePickBlock :=3D [:a :b | container :=3D NSMutableArray array. [ ((a count) > 0) | ((b count) > 0) ] whileTrue: [(a count) > 0 ifTrue:[addBlock value:container value:a]. (b count) > 0 ifTrue:[addBlock value:container value:b] ]. container]. distributedCategoryResults :=3D categorySiteResults @ \ alternatePickBlock. distributedResults :=3D distributedCategoryResults \ alternatePickBlock. distributedResults ] The input argument to this block is the array of search results. Each result is a dictionary (the two important keys are Category and Site). Is there a better way to do this? Thanks. Regards Sarat |