How to reduce a list?

Q&A's, tips, howto's
Locked
Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

How to reduce a list?

Post by Fritz »

I have a list like this:

Code: Select all

'((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:

Code: Select all

'((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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: How to reduce a list?

Post by cormullion »

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

Code: Select all

(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
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

Re: How to reduce a list?

Post by Fritz »

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.

Locked