Re: [sawfish] Re: `position' in rep...?



30 minutes ago, Christopher Roy Bratusek wrote:
> Issue solved. Improved `position' now in rep.util.misc.
> SawfishConfig uses `option-index' instead.

It seems that something like `option-index' doesn't belong in there,
at least not with a name that makes it specific to functionality that
librep doesn't do...  To make it easier, here's a version that
receives an optional argument which is a comparison function:

  (define (position item l . r)
    (let ((=? (if (consp r) (car r) equal)))
      (let loop ((slow l) (l l) (i 0))
        (cond ((not (consp l)) #f)
              ((=? item (car l)) i)
              (#t (let ((l (cdr l)) (i (1+ i)))
                    (cond ((not (consp l)) #f)
                          ((=? item (car l)) i)
                          ((eq l slow) #f)
                          (#t (loop (cdr slow) (cdr l) (1+ i))))))))))

This could be use to implement the same `option-index' functionality
more conveniently, but that wouldn't be great, since the comparison
function needs to know that it's used with the desired item first and
the list member second.  A better solution is another function, which
uses a predicate:

  (define (position-if pred l)
    (let loop ((slow l) (l l) (i 0))
      (cond ((not (consp l)) #f)
            ((pred (car l)) i)
            (#t (let ((l (cdr l)) (i (1+ i)))
                  (cond ((not (consp l)) #f)
                        ((pred (car l)) i)
                        ((eq l slow) #f)
                        (#t (loop (cdr slow) (cdr l) (1+ i)))))))))

And now `option-index' is easy to implement properly.  In addition,
`position' could be implemented as:

  (define (position item l . r)
    (let ((=? (if (consp r) (car r) equal)))
      (position-if (lambda (x) (=? item x)) l)))

But my guess is that rep doesn't compile things enough and this will
be slower.  I don't know if that's important though -- if it is, I can
rewrite both using a single macro for the searching code.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]