Skip to content

Implement min_by, min_by_key, max_by, and max_by_key #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cedric-h
Copy link
Contributor

@cedric-h cedric-h commented Oct 2, 2020

The implementation isn't exactly elegant, but it compiles.

This was useful to me because I had a list of entities and wanted to get the one closest to a given entity for a simple script, but the only function the std provided for that was min, which, when followed by a map, allowed me to easily get the distance of the closest entity, but not the entity itself. min-by-key makes that easier.

; Returns the Ent of the closest instance of a certain prefab.
; The first argument should be the position relative to which "closeness" is gauged.
; The prefab is provided as the second argument, optionally as the third argument
; one can supply an Ent to be excluded unless it is the only Ent of this prefab.
(let-fn closest-instance (rel inst (? exclude))
    (min-by-key
        (fn1 (if (eq? exclude _)
              (inf.0)
              (magn2 (delta rel (.pos _)))))
        ..(instances-of inst)))

Prior art: https://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket%2Flist..rkt)._argmin))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant