Collecting multiple maximum values

I have a list of items. Each element is structured as follows:

('symbol "string" int-score)

List of examples:

(list (list 'object1 "wabadu" 0.5)
      (list 'object2 "xezulu" 0.6)
      (list 'object1 "yebasi" 0.5)
      (list 'object1 "tesora" 0.2))

I want to get the maximum values ​​for a specific character. When I search with a symbol object2, I have to go back:

('object2 "xezulu" 0.6)

If I search with object1, I must return:

(('object1 "wabadu" 0.5) ('object1 "yebasi" 0.5))

I want to collect all the highest elements of a particular object. I can do this: suppose the list above is the list that is used below, and what I'm looking for object1. I can get all the elements of a specific object:

(loop for element in list
     when (equal 'object1 (first element))
     collect element)

I can also extract one of the highest list items:

(loop for element in list
     when (equal 'object1 (first element))
     maximize (third element))

. , , . collect maximize, . " "?

+4
7

loop ( into, loop ), loop finally , :

(loop for triple in *l*
      for (key nil score) = triple
      when (eq key 'object1)
        collect triple into selection
        and maximize score into max-score
      finally (return (loop for triple in selection
                            when (eql (third triple) max-score)
                              collect triple)))

. delete :

(loop for triple in *l*
      for (key name score) = triple
      when (eq key 'object1)
        collect triple into selection
        and maximize score into max-score
      finally (return (delete max-score selection
                              :test #'/=
                              :key #'third)))
0

LOOP:

(defun mymax (target list &aux result max)
  (loop for (item name value) in list
        when (eql item target)
        do (cond ((or (null result)
                      (> value max))
                  (setf result (list (list item name value))
                        max value))
                 ((= value max)
                  (push (list item name value) result))))
  result)
+2

hash-table, , , (maximum . (list of strings corresponding to maximum))

(let ((data (list (list 'object1 "wabadu" 0.5)
                  (list 'object2 "xezulu" 0.6)
                  (list 'object1 "yebasi" 0.5)
                  (list 'object1 "tesora" 0.2))))
  (loop
     :with table := (make-hash-table)
     :for (item string num) :in data :do
     (destructuring-bind (&optional max strings)
         (gethash item table)
       (cond
         ((or (null max) (< max num))
          (setf (gethash item table) (list num (list string))))
         ((= max num)
          (setf (cdr strings) (cons string (cdr strings))))))
     :finally (return table)))

;; #<HASH-TABLE {1005C6BE93}>
;; --------------------
;; Count: 2
;; Size: 16
;; Test: EQL
;; Rehash size: 1.5
;; Rehash threshold: 1.0
;; [clear hashtable]
;; Contents: 
;; OBJECT1 = (0.5 ("wabadu" "yebasi")) [remove entry]
;; OBJECT2 = (0.6 ("xezulu")) [remove entry]

, -, , .

+1

maximize . 3- , (). :

;;; suppose a copy of the data is stored in l

;; get all 'object1 and sort them
(setf l (sort (remove-if-not
                (lambda (x) (equal (first x) 'object1)) l)
              #'> :key #'third))
;; remove the ones with smaller value than the first one
(setf l (remove-if
          (lambda (x) (< (third x) (third (first l)))) l))
0

; :

(defun make-foo (type name score)
   (list type name score))

(defun foo-type (foo) (elt foo 0))
;; ...

(defun make-foos (&rest foos)
  foos)

(defun foos-find-if (foos predicate)
  ;; return all foos satisfying predicate
  )

(defun foos-maximize (foos orderer)
  ;; return the maximum foo (any one)
  )

(defun foos-find-if-maximized (foos)
  (foos-find-if foos 
    (let ((max (foos-maximize foos #'foo-score)))
      (lambda (foo)
        (= (foo-score max) (foo-score foo))))))
0

, symbol-list, . .

(defun foo (symbol list)
  (let* ((symbol-list (remove-if-not #'(lambda (l) (eq (first l) symbol))
                       list))
         (max (apply #'max (mapcar #'third symbol-list))))
    (remove-if-not #'(lambda (l) (= (third l) max))
     symbol-list)))

: (foo 'object1 l)

0

, , .

:

(defun collect-maxima-by-third (list)
  (reduce
   #'(lambda (max-list next-element)
       (let ((max-value (third (first max-list)))
             (next-value (third next-element)))
         (cond ((< max-value next-value)
                (list next-element))
                ((= max-value next-value)
                 (cons next-element max-list))
                (t max-list)))) ; the greater-than case
   (rest list)
   :initial-value (list (first list))))

, , , , , , , .

(, ) ; Haskell (Learn You the Haskell ).

0

All Articles