How to reduce a list?

Started by Fritz, July 03, 2010, 07:52:20 AM

Previous topic - Next topic

Fritz

I have a list like this:


'((1 Oil 2)
  (1 Oil 5)
  (1 Oil 7)
  (2 Gas 4)
  (2 Gas 12))


I want to reduce it and get list like this:


'((1 Oil 2 5 7)
  (2 Gas 4 12))


I can make it via "ref" or "find" or "filter" operators, but list is pretty big (3'000 records), so speed is important for me.



It takes about 3-4 seconds to reduce list on my PC, but I want 0.5 seconds or even less. May be, there is a special operator for list reducing in newLISP?

cormullion

#1
Hmm. These data structures are a bit awkward. If you could adapt them a bit, you could try this:


(set 'f '((1 Oil 2)
  (1 Oil 5)
  (1 Oil 7)
  (2 Gas 4)
  (2 Gas 12)))

(define D:D)

(dolist (r f)
   (set 'e (string (r 0) { } (r 1)))
   (if (D e)
       (D e (cons (r 2) $it))  
       (D e (r 2))))

(println (D))

;-> (("1 Oil" (7 5 2)) ("2 Gas" (12 4)))


It's not quite what you want, but it's quite quick.

Fritz

#2
Thank you! This method is much faster: 0,4 seconds vs 3 seconds with "filter" method.



So now I can load one hundred price-lists per second, so my task is accomplished.