Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In racket, I'd write something like

  (argmax (λ(c) (string-length (customer-name c)))
          customers)
http://docs.racket-lang.org/reference/pairs.html?q=argmax#(d...)

Haskell has argmaxBy: http://hackage.haskell.org/packages/archive/list-extras/0.3....

If your language doesn't have argmax, fold the list with the best value, like this in lisp:

  (define (my-argmax fun lst)
    (foldl (λ(prev-max elt)
             (if (< (fun prev-max) (fun elt))
                 elt
                 prev-max))
           (car lst)
           (cdr lst)))
No extra space usage, no temporary values, no sorting; all in O(N) time. :)


Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: