Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 36 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kenneth Tilton  
View profile  
(3 users)  More options Jan 16, 8:02 am
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Fri, 16 Jan 2009 08:02:22 -0500
Local: Fri, Jan 16 2009 8:02 am
Subject: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
You are given a simple-vector and a list of malformed ranges, malformed
in that the second value is the last one to /keep/ vs. what subseq wants
as the end parameter: the first one to omit. But all ranges are pairs
(even if only one value is thus delimited) and the pairs are guarnteed
to be ordered and well-formed and not to go out of bounds, so there is that.

Your mission is to return a simple-vector with those values excised.

(defun excise-ranges (seq ranges)
    ....)

such that:

(excise-ranges #(0 1 2 3) '((0 0)))
-> #(1 2 3)

(excise-ranges #(0 10 20 30 40) '((1 2)(4 4)))
-> #(0 30)

And do it better than I, which should not be too hard from the looks of
the mess below (if you scroll down far enough).

hth,kt

(bk-defun excise-ranges (seq ranges)
   (loop with length = (length seq)
       and s1 = (when (plusp (caar ranges))
                    (subseq seq 0 (caar ranges)))
       for (r1 r2) on ranges
       collect (subseq seq (1+ (cadr r1)) (if r2 (car r2) length))
           into ss
       finally (return (apply 'concatenate 'simple-vector s1 ss))))


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Willem Broekema  
View profile  
(1 user)  More options Jan 16, 10:45 am
Newsgroups: comp.lang.lisp
From: Willem Broekema <metaw...@gmail.com>
Date: Fri, 16 Jan 2009 07:45:13 -0800 (PST)
Local: Fri, Jan 16 2009 10:45 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 16, 2:02 pm, Kenneth Tilton <kentil...@gmail.com> wrote:

> (bk-defun excise-ranges (seq ranges)
>    (loop with length = (length seq)
>        and s1 = (when (plusp (caar ranges))
>                     (subseq seq 0 (caar ranges)))
>        for (r1 r2) on ranges
>        collect (subseq seq (1+ (cadr r1)) (if r2 (car r2) length))
>            into ss
>        finally (return (apply 'concatenate 'simple-vector s1 ss))))

This version is a bit longer, but more efficient due to REPLACE:

(defun ex (seq ranges)
  (loop with new-length = (- (length seq)
                             (loop for (start end) in ranges sum (1+
(- end start))))
      with dest = (subseq seq 0 new-length)
      for ((s1 e1) (s2 nil)) on ranges ;; s, e inclusive
      for gap-start-ix = (1+ e1)
      for gap-end-ix = (or s2 (length seq)) ;; end-ix exclusive
      for gap-length = (- gap-end-ix gap-start-ix)
      for dest-ix = s1 then dest-ix
      when (plusp gap-length)
      do (replace dest seq :start1 dest-ix :start2 gap-start-ix :end2
gap-end-ix)
         (incf dest-ix gap-length)
      finally (return dest)))

- Willem


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Madhu  
View profile  
(1 user)  More options Jan 16, 11:17 am
Newsgroups: comp.lang.lisp
From: Madhu <enom...@meer.net>
Date: Fri, 16 Jan 2009 21:47:01 +0530
Local: Fri, Jan 16 2009 11:17 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

* Kenneth Tilton <49708557$0$20284$607ed...@cv.net> :
Wrote on Fri, 16 Jan 2009 08:02:22 -0500:

[snip]

| (bk-defun excise-ranges (seq ranges)
|   (loop with length = (length seq)
|       and s1 = (when (plusp (caar ranges))
|                    (subseq seq 0 (caar ranges)))
|       for (r1 r2) on ranges
|       collect (subseq seq (1+ (cadr r1)) (if r2 (car r2) length))
|           into ss
|       finally (return (apply 'concatenate 'simple-vector s1 ss))))

;; same algorithm as gnus-list-range-difference in gnus/gnus-range.el

(defun excise-ranges  (vector ranges)
  "Return a list of elements in VECTOR whose indices are not members of
RANGES."
  (loop for number from 0 for elem across list
        do (loop while (and ranges (< (cadar ranges) number))
                 do (setq ranges (cdr ranges)))
        when (or (not ranges) (< number (caar ranges)))
        collect elem))
--
Madhu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Madhu  
View profile  
(1 user)  More options Jan 16, 11:24 am
Newsgroups: comp.lang.lisp
From: Madhu <enom...@meer.net>
Date: Fri, 16 Jan 2009 21:54:00 +0530
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

* Kenneth Tilton <49708557$0$20284$607ed...@cv.net> :
Wrote on Fri, 16 Jan 2009 08:02:22 -0500:

[snip]

| (bk-defun excise-ranges (seq ranges)
|   (loop with length = (length seq)
|       and s1 = (when (plusp (caar ranges))
|                    (subseq seq 0 (caar ranges)))
|       for (r1 r2) on ranges
|       collect (subseq seq (1+ (cadr r1)) (if r2 (car r2) length))
|           into ss
|       finally (return (apply 'concatenate 'simple-vector s1 ss))))

;; same algorithm as gnus-list-range-difference in gnus/gnus-range.el

(defun excise-ranges  (vector ranges)
  "Return a list of elements in VECTOR whose indices are not members of
RANGES."
  (loop for number from 0 for elem across vector
        do (loop while (and ranges (< (cadar ranges) number))
                 do (setq ranges (cdr ranges)))
        when (or (not ranges) (< number (caar ranges)))
        collect elem))
--
Madhu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
(3 users)  More options Jan 16, 11:24 am
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Fri, 16 Jan 2009 11:24:20 -0500
Local: Fri, Jan 16 2009 11:24 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

Whoa, extra credit for the replace!

An email entry gave me an idea, I might be entering my own contest with
    a five-liner.

I was looking for short and elegant, but we can have a category for
effiency. I'd let it be nexcise-ranges but we'll need to support undo.

cheers,kth


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
(3 users)  More options Jan 16, 11:45 am
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Fri, 16 Jan 2009 11:45:22 -0500
Local: Fri, Jan 16 2009 11:45 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

well, it /feels/ like three lines :(

(defun excise-ranges (seq ranges)
   (coerce
    (loop with bounds = (loop for (min max) in ranges
                             collect min
                             collect (1+ max))
         and collecting = t
         for elt across seq
         for n upfrom 0
         when (and bounds (= n (car bounds)))
         do (setf collecting (not collecting))
         (pop bounds)
         when collecting collect elt)
    'simple-vector)

lightly tested.

kt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile  
 More options Jan 16, 12:12 pm
Newsgroups: comp.lang.lisp
From: Pascal Costanza <p...@p-cos.net>
Date: Fri, 16 Jan 2009 18:12:54 +0100
Local: Fri, Jan 16 2009 12:12 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

(defun excise-ranges-helper (vector ranges)
   (declare (vector vector))
   (loop with nada = vector ;; ;)
         for (lower upper) in ranges
         do (fill vector nada :start lower :end (1+ upper))
         finally (return (loop for entry across vector
                               unless (eq entry nada)
                               collect entry))))

(defgeneric excise-ranges (sequence ranges)
   (:method ((sequence vector) ranges)
    (coerce (excise-ranges-helper (copy-seq sequence) ranges) 'vector))
   (:method ((sequence list) ranges)
    (excise-ranges-helper (coerce sequence 'vector) ranges)))

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patrick May  
View profile  
 More options Jan 16, 2:38 pm
Newsgroups: comp.lang.lisp
From: Patrick May <p...@spe.com>
Date: Fri, 16 Jan 2009 14:38:11 -0500
Local: Fri, Jan 16 2009 2:38 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

     I'll be different and not use loop:

(defun excise-ranges (sequence ranges)
  "Return a sequence that consists of the elements in SEQUENCE not in
  the inclusive ranges of RANGES."
  (flet ((expand-range (range)
           (let ((result nil))
             (dotimes (i (1+ (- (second range) (first range))) result)
               (push (+ i (first range)) result)))))
    (let ((expanded-ranges (reduce #'append (mapcar #'expand-range ranges)))
          (result nil))
      (dotimes (i (length sequence)
                (make-array (length result)
                            :initial-contents (nreverse result)))
        (unless (find i expanded-ranges)
          (push (aref sequence i) result))))))

Oh, wait, you wanted it to be _efficient_, too?

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc.  | Large scale, mission-critical, distributed OO
                       | systems design and implementation.
http://www.spe.com/pjm | (C++, Java, Common Lisp, Jini, middleware, SOA)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
w_a_x_...@yahoo.com  
View profile  
(3 users)  More options Jan 16, 3:07 pm
Newsgroups: comp.lang.lisp
From: w_a_x_...@yahoo.com
Date: Fri, 16 Jan 2009 12:07:58 -0800 (PST)
Local: Fri, Jan 16 2009 3:07 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 16, 7:02 am, Kenneth Tilton <kentil...@gmail.com> wrote:

Ruby:

def delete_ranges seq, ranges
  indices = (0 ... seq.size).to_a
  ranges.each{|r| indices -= Range.new( *r ).to_a }
  seq.values_at *indices
end

p delete_ranges( [2,3,4,5,6,7,8], [[1,1], [4,5]] )
p delete_ranges( [0,1,2,3], [[0, 0]] )
p delete_ranges( [0,10,20,30,40], [[1,2],[4,4]] )

--- output ---
[2, 4, 5, 8]
[1, 2, 3]
[0, 30]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
(2 users)  More options Jan 16, 7:57 pm
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Fri, 16 Jan 2009 19:57:17 -0500
Local: Fri, Jan 16 2009 7:57 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

Nice! Is the nested loop necessary better than?:

(defun excise-ranges  (vector ranges)
   (loop for n from 0
       for elem across vector
       for rr = (member n ranges :key 'cadr :test '<=)
       when (or (not ranges) (< n (caar rr)))
       collect elem))

kth


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
 More options Jan 16, 8:10 pm
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Fri, 16 Jan 2009 20:10:10 -0500
Local: Fri, Jan 16 2009 8:10 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

Kenneth Tilton wrote:
 > Madhu wrote:

 >> * Kenneth Tilton <49708557$0$20284$607ed...@cv.net> :
 >> Wrote on Fri, 16 Jan 2009 08:02:22 -0500:
 >>
 >> [snip]
 >>
 >> | (bk-defun excise-ranges (seq ranges)
 >> |   (loop with length = (length seq)
 >> |       and s1 = (when (plusp (caar ranges))
 >> |                    (subseq seq 0 (caar ranges)))
 >> |       for (r1 r2) on ranges
 >> |       collect (subseq seq (1+ (cadr r1)) (if r2 (car r2) length))
 >> |           into ss
 >> |       finally (return (apply 'concatenate 'simple-vector s1 ss))))
 >>
 >>
 >> ;; same algorithm as gnus-list-range-difference in gnus/gnus-range.el
 >>
 >> (defun excise-ranges  (vector ranges)
 >>   "Return a list of elements in VECTOR whose indices are not members of
 >> RANGES."
 >>   (loop for number from 0 for elem across vector
 >>         do (loop while (and ranges (< (cadar ranges) number))
 >>                  do (setq ranges (cdr ranges)))
 >>         when (or (not ranges) (< number (caar ranges)))
 >>         collect elem))
 >
 > Nice! Is the nested loop necessary better than?:
 >
 > (defun excise-ranges  (vector ranges)
 >   (loop for n from 0
 >       for elem across vector
 >       for rr = (member n ranges :key 'cadr :test '<=)
 >       when (or (not ranges) (< n (caar rr)))
 >       collect elem))

Silly optimization but only because I failed to say the average number
of ranges would be 1.0001 and we would almost never see more than 2 so
since others are taking care to truncate the range list:

(defun excise-ranges (vector ranges)
   (loop for n upfrom 0
       for elem across vector
       for rrc = ranges then rr
       for rr = (member n rrc :key 'cadr :test '<=)
       when (or (not rr) (< n (caar rr)))
       collect elem))

I still like my "three-liner". :(

kt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ariel Badichi  
View profile  
 More options Jan 16, 8:13 pm
Newsgroups: comp.lang.lisp
From: Ariel Badichi <a...@tidexsystems.com>
Date: Fri, 16 Jan 2009 17:13:17 -0800 (PST)
Local: Fri, Jan 16 2009 8:13 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 16, 7:12 pm, Pascal Costanza <p...@p-cos.net> wrote:

That's a good approach, but there are some unnecessary complications
here.  I would write the following:

(defun excise-ranges (sequence ranges)
  (loop with nada = (load-time-value (cons nil nil))
        for (lower upper) in ranges
        do (fill sequence nada :start lower :end (1+ upper))
        finally (return (remove nada sequence :test #'eq))))

Note that I use a "private" cons, which is guaranteed not to be an
element
of the sequence at function entry or exit.  Note that the sequence is
passed
just to FILL and REMOVE, which are generic sequence operators - no
need
to coerce anything.

Ariel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ariel Badichi  
View profile  
 More options Jan 16, 8:42 pm
Newsgroups: comp.lang.lisp
From: Ariel Badichi <a...@tidexsystems.com>
Date: Fri, 16 Jan 2009 17:42:25 -0800 (PST)
Local: Fri, Jan 16 2009 8:42 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 17, 3:13 am, Ariel Badichi <a...@tidexsystems.com> wrote:

> (defun excise-ranges (sequence ranges)
>   (loop with nada = (load-time-value (cons nil nil))
>         for (lower upper) in ranges
>         do (fill sequence nada :start lower :end (1+ upper))
>         finally (return (remove nada sequence :test #'eq))))

I should have used DELETE rather than REMOVE here, since the function
is clearly destructive.  Also, I apologize for the formatting errors
in that post.

Ariel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ron Garret  
View profile  
 More options Jan 17, 3:21 am
Newsgroups: comp.lang.lisp
From: Ron Garret <rNOSPA...@flownet.com>
Date: Sat, 17 Jan 2009 00:21:03 -0800
Local: Sat, Jan 17 2009 3:21 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
In article <49708557$0$20284$607ed...@cv.net>,
 Kenneth Tilton <kentil...@gmail.com> wrote:

(defun excise-ranges (seq ranges)
  (loop for (a b) in ranges do
    (loop for i from a to b do (setf (elt seq i) '#1=#:zap)))
  (remove '#1# seq))

rg


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bakul Shah  
View profile  
 More options Jan 17, 3:31 am
Newsgroups: comp.lang.lisp
From: Bakul Shah <bakul+use...@bitblocks.com>
Date: Sat, 17 Jan 2009 00:31:18 -0800
Local: Sat, Jan 17 2009 3:31 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

If closed intervals for elements to be excluded as in your problem
are converted to half open intervals for elements to be *included*,
things might get simpler.  As an example, your [1 2] [4 4] becomes
[0 1) [3 4) [5...). Now you can do something like

(defun squeeze (seq ranges)
   (reduce #'append
     (mapcar
       (lambda (r) (apply #'subseq seq (if (consp r) r (list r (length seq)))))
       ranges)))

(squeeze '(0 10 20 30 40) '((0 1) (3 4))) => (0 30)

Since append is not generalized for sequences other than lists,
actual solution will be messier.

I'd actually prefer to represent the same range as (0 1 3 4 5)
instead of ((0 1) (3 4) (5 ...)) as one can think of alternating
numbers starting inclusion/exclusion of sequential elements. This
seems like a generally useful idiom. Ideally I should be able to
do e.g.

   (squeeze "abcdef" '(0)) => "abcdef"
   (squeeze "abcdef" '(0 1) => "a"
   (squeeze "abcdef" '(0 1 3) => "adef"
   (squeeze "abcdef" '(0 1 3 4) => "ad"
   (squeeze "abcdef" '(0 1 3 4 5) => "adf"


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
(1 user)  More options Jan 17, 4:01 am
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Sat, 17 Jan 2009 04:01:40 -0500
Local: Sat, Jan 17 2009 4:01 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

I thought that might be a fun way to do it, albeit excessive by some
measure that did not appear clearly to me in my usual mist.

   As an example, your [1 2] [4 4] becomes

Did you see my "three line" solution that came out to ten? I also took
the second "max" value and incremented it to be an "end" value.

kt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
 More options Jan 17, 4:04 am
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Sat, 17 Jan 2009 04:04:36 -0500
Local: Sat, Jan 17 2009 4:04 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

The teacher said we cannot use setf.

Nice, tho. I learned something there about how to use uninterned symbols.

kt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile  
 More options Jan 17, 8:25 am
Newsgroups: comp.lang.lisp
From: Pascal Costanza <p...@p-cos.net>
Date: Sat, 17 Jan 2009 14:25:29 +0100
Local: Sat, Jan 17 2009 8:25 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

That's also true for the value I use in my code, because the vector that
I use as a marker is a freshly created vector. But, indeed, I meant that
as a joke. ;)

> Note that the sequence is
> passed
> just to FILL and REMOVE, which are generic sequence operators - no
> need
> to coerce anything.

Nice, also wrt to using DELETE, as you state in your own follow-up.

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile  
 More options Jan 17, 11:17 am
Newsgroups: comp.lang.lisp
From: Pascal Costanza <p...@p-cos.net>
Date: Sat, 17 Jan 2009 17:17:48 +0100
Local: Sat, Jan 17 2009 11:17 am
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

Using the LispWorks multiprocessing API:

(defconstant +nof-cores+ 4)

(defstruct (rep (:constructor make-rep (target source length)))
   target source length)

(defun extract-ranges (target source reps)
   (declare (vector source target reps))
   (loop with reps-per-core = (ceiling (length reps) +nof-cores+)
         with mailbox = (mp:make-mailbox :size +nof-cores+)
         for index from 0 to (length reps) by reps-per-core
         for count from 0 do
         (let ((index index))
           (mp:process-run-function
            "extract ranges" '()
            (lambda ()
              (loop for i from index below (min (+ index reps-per-core)
                                                (length reps))
                    for rep = (aref reps i) do
                    (replace target source
                             :start1 (rep-target rep)
                             :end1 (+ (rep-target rep) (rep-length rep))
                             :start2 (rep-source rep)
                             :end2 (+ (rep-source rep) (rep-length rep)))
                    finally (mp:mailbox-send mailbox 'done)))))
         finally (loop repeat count do
                       (assert (eq (mp:mailbox-read mailbox) 'done))
                       finally (return-from extract-ranges target))))

(defun excise-ranges (sequence ranges)
   (loop with reps = (make-array (ceiling (length sequence) 2)
                                 :adjustable t :fill-pointer 0)
         for target = 0 then (+ target length)
         for source = 0 then (1+ upper)
         for (lower upper) in ranges
         for length = (- lower source)
         unless (zerop length)
         do (vector-push-extend (make-rep target source length) reps)
         finally
         (let ((length (- (length sequence) upper 1)))
           (unless (zerop length)
             (vector-push-extend (make-rep target source length) reps))
           (return (extract-ranges
                     (make-array (+ target length))
                     sequence
                     reps)))))

Only lightly tested, without any guarantees that this is actually
efficient. (It's probably not, so consider this more as an exercise.)

What would a Clojure version look like?

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ron Garret  
View profile  
 More options Jan 17, 12:36 pm
Newsgroups: comp.lang.lisp
From: Ron Garret <rNOSPA...@flownet.com>
Date: Sat, 17 Jan 2009 09:36:40 -0800
Local: Sat, Jan 17 2009 12:36 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
In article <49719f28$0$4870$607ed...@cv.net>,
 Kenneth Tilton <kentil...@gmail.com> wrote:

Changing the rules after the game has started?  OK, here's a setf-free
version:

(defun excise-ranges (seq ranges)
  (coerce
   (loop for (a b) on (cons -1 (apply 'append ranges)) by 'cddr
     append (coerce (subseq seq (1+ a) b) 'list)) 'simple-vector))

I note in passing that the first version did not depend on the ranges
being in order.  This one does.

rg


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adde  
View profile  
 More options Jan 17, 1:21 pm
Newsgroups: comp.lang.lisp
From: Adde <a...@trialcode.com>
Date: Sat, 17 Jan 2009 10:21:05 -0800 (PST)
Local: Sat, Jan 17 2009 1:21 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 17, 2:42 am, Ariel Badichi <a...@tidexsystems.com> wrote:

> On Jan 17, 3:13 am, Ariel Badichi <a...@tidexsystems.com> wrote:

> > (defun excise-ranges (sequence ranges)
> >   (loop with nada = (load-time-value (cons nil nil))
> >         for (lower upper) in ranges
> >         do (fill sequence nada :start lower :end (1+ upper))
> >         finally (return (remove nada sequence :test #'eq))))

> I should have used DELETE rather than REMOVE here, since the function
> is clearly destructive.  Also, I apologize for the formatting errors
> in that post.

> Ariel

Since the original problem didn't mention anything about the list
containing junk (or maybe I missed something?), I don't see why we
couldn't just use nil. Also the upper bound was not to be removed
according to the description (or maybe...).

(defun excise-ranges (sequence ranges)
  (loop for (lower upper) in ranges
        do (fill sequence nil :start lower :end upper)
        finally (return (delete-if #'null sequence)))))

Adde


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile  
 More options Jan 17, 1:24 pm
Newsgroups: comp.lang.lisp
From: Pascal Costanza <p...@p-cos.net>
Date: Sat, 17 Jan 2009 19:24:47 +0100
Local: Sat, Jan 17 2009 1:24 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

The OP didn't give a full specification. ;)

However, it was stated that the upper was to be removed. Also, it wasn't
stated that the sequence wouldn't contain nil.

Finally, (delete-if #'null ...) is a bit verbose, (delete nil ...) would
be sufficient.

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adde  
View profile  
 More options Jan 17, 1:27 pm
Newsgroups: comp.lang.lisp
From: Adde <a...@trialcode.com>
Date: Sat, 17 Jan 2009 10:27:31 -0800 (PST)
Local: Sat, Jan 17 2009 1:27 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 17, 7:21 pm, Adde <a...@trialcode.com> wrote:

I should definitely learn to read more carefully, the description
explicitly state that the upper bound should be removed. Sorry about
that.

(defun excise-ranges (sequence ranges)
  (loop for (lower upper) in ranges
        do (fill sequence nil :start lower :end (1+ upper))
        finally (return (delete-if #'null sequence)))))


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Tilton  
View profile  
 More options Jan 17, 1:36 pm
Newsgroups: comp.lang.lisp
From: Kenneth Tilton <kentil...@gmail.com>
Date: Sat, 17 Jan 2009 13:36:18 -0500
Local: Sat, Jan 17 2009 1:36 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26

Only for certain players.

> OK, here's a setf-free
> version:

> (defun excise-ranges (seq ranges)
>   (coerce
>    (loop for (a b) on (cons -1 (apply 'append ranges)) by 'cddr
>      append (coerce (subseq seq (1+ a) b) 'list)) 'simple-vector))

Nice. Why not use concatenate and stay as a vector?

kt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adde  
View profile  
 More options Jan 17, 1:42 pm
Newsgroups: comp.lang.lisp
From: Adde <a...@trialcode.com>
Date: Sat, 17 Jan 2009 10:42:09 -0800 (PST)
Local: Sat, Jan 17 2009 1:42 pm
Subject: Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26
On Jan 17, 7:24 pm, Pascal Costanza <p...@p-cos.net> wrote:

Sloppy reading on my part, thanks ;)
Nitpicking here but isn't the default test for (delete) #'eql?
Changing the test to match the runtime complexity of #'null would make
it just as verbose as (delete-if).
(delete nil sequence :test #'eq)

Adde


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 36   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google