Giter Club home page Giter Club logo

Comments (6)

tuscland avatar tuscland commented on May 27, 2024

Here is an attempt (I'm still interested in your expertise!).

(defun interval-search (seq value &key (key #'identity))
  "Search members of SEQ for an interval containing VALUE. Return two
values being the boundary elements of the interval found, one value on
an exact match, or NIL otherwise."
  (labels ((rec (low high)
             (cond
              ((eq (- low high) 1)
               (values (fset:@ seq high)
                       (fset:@ seq low)))
              ((< high low)
               nil)
              (t
               (let* ((mid (floor (/ (+ low high) 2)))
                      (mid-value (funcall key (fset:@ seq mid))))
                  (case (fset:compare mid-value value)
                    (:greater (rec low (- mid 1)))
                    (:less    (rec (+ mid 1) high))
                    (t        (fset:@ seq mid))))))))
    (unless (fset:empty? seq)
      (rec 0 (- (fset:size seq) 1)))))

from fset.

slburson avatar slburson commented on May 27, 2024

There's an existing operation 'rank' that will do what you need. Put your
values in a map, and then call 'rank' to find the nearest key. Use
'at-rank' to extract the key/value pairs of that and the previous rank, as
appropriate.

(It annoys me that I defined 'rank' to return the higher index rather than
the lower one in the case of a miss; returning the lower one would have
been more natural. But I don't think I should risk breaking users' code by
changing it now.)

-- Scott

On Fri, Mar 21, 2014 at 2:59 AM, Camille Troillard <[email protected]

wrote:

Here is an attempt (I'm still interested in your expertise!).

(defun interval-search (seq value &key (key #'identity))
"Search members of SEQ for an interval containing VALUE. Return twovalues being the boundary elements of the interval found, one value onan exact match, or NIL otherwise."
(labels ((rec (low high)
(cond
((eq (- low high) 1)
(values (fset:@ seq high)
(fset:@ seq low)))
((< high low)
nil)
(t
(let* ((mid (floor (/ (+ low high) 2)))
(mid-value (funcall key (fset:@ seq mid))))
(case (fset:compare mid-value value)
(:greater (rec low (- mid 1)))
(:less (rec (+ mid 1) high))
(t (fset:@ seq mid))))))))
(unless (fset:empty? seq)
(rec 0 (- (fset:size seq) 1)))))

Reply to this email directly or view it on GitHubhttps://github.com//issues/5#issuecomment-38261919
.

from fset.

tuscland avatar tuscland commented on May 27, 2024

Thanks Scott!
I will have a look at RANK.

from fset.

tuscland avatar tuscland commented on May 27, 2024

Just an additional comment:
RANK is internal to the FSET package, so I don't believe it is much used.
I guess you could change it in a future release, don't you think?

from fset.

slburson avatar slburson commented on May 27, 2024

Ah, I never exported RANK or AT-RANK; alas, that probably means I never
tested them very much either. If you could code-review and/or test them,
that would be great.

Nonetheless, since RANK wasn't exported, as you suggest I feel free to
tweak its interface. I have done so, and exported both symbols.

-- Scott

On Fri, Mar 21, 2014 at 1:23 PM, Camille Troillard <[email protected]

wrote:

Just an additional comment:
RANK is internal to the FSET package, so I don't believe it is much used.
I guess you could change it in a future release, don't you think?

Reply to this email directly or view it on GitHubhttps://github.com//issues/5#issuecomment-38319850
.

from fset.

tuscland avatar tuscland commented on May 27, 2024

The updated version works fine, thank you for making this adjustment!

from fset.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.