Comments (6)
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.
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.
Thanks Scott!
I will have a look at RANK
.
from fset.
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.
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.
The updated version works fine, thank you for making this adjustment!
from fset.
Related Issues (20)
- fset:position returns position relative to :start keyword parameter HOT 2
- Metadata for Quicklisp HOT 1
- Support for named-readtables? HOT 1
- count-if and count-if-not return nil HOT 2
- Readme mentions FSet project page on common-lisp.net but it is not available HOT 1
- `@` only with function argument
- build order issues HOT 1
- Second value for `lookup` on sequences
- Faulty map lookup/building
- @ operator with symbol as first argument
- intersection on relations is bugged HOT 1
- style-warning: FSET:ITERATOR clobbers an earlier FTYPE proclamation HOT 2
- a few function shadowed but not exported HOT 1
- Add iterate extensions for fset types
- Review test suite for completeness HOT 1
- Tuple test should be multithreaded
- Need tests for CL compatibility functions HOT 2
- Reversing a char-containing sequence doesn't work
- map syntax too complicated HOT 1
- SBCL internals have changed - SB-KERNEL::%MAKE-RANDOM-STATE HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fset.