Paste number 353413: loop excitingness

Index of paste annotations: 1 | 2

Paste number 353413: loop excitingness
Pasted by: fiddlerwoaroof
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+7KP1
Channel:None
Paste contents:
Raw Source | XML | Display As
(defun merge- (A B)
  (let* ((n (+ (length A)
          (length B)))
    (C (make-array n))
    (i 0)
    (j 0))
    (prog1 C
      (loop
    for k from 0 to (1- n)

    while (and (< i (length A))
          (< j (length B)))

    for A-elt = (elt A i)
    for B-elt = (elt B j)
    for A-less = (< A-elt B-elt)

    do
      (setf (elt C k)
       (if A-less A-elt B-elt))

    if A-less do (incf i)
    else do (incf j)

    finally

      (loop
         with first-smaller? = (< i (length A))
         with source = (if first-smaller? A B)
         with start = (if first-smaller? i j)

         for idx from start to (1- (length (if first-smaller? A B)))

         do
      (setf (elt C k)
            (elt source idx))
      (incf k))))))

Annotations for this paste:

Annotation number 1: fixed annotations
Pasted by: fiddlerwoaroof
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+7KP1/1
Paste contents:
Raw Source | Display As
(defun merge- (A B)
  (declare (optimize (speed 3) (safety 0))
           (type (simple-array fixnum) A B))
  (let* ((n (+ (length A)
               (length B)))
         (C (make-array n))
         (i 0)
         (j 0))
    (prog1 C
      (loop
         for k from 0 to (1- n)

         while (and (< i (length A))
                    (< j (length B)))

         for A-elt = (elt A i)
         for B-elt = (elt B j)
         for A-less = (< A-elt B-elt)

         do
           (setf (elt C k)
                 (if A-less A-elt B-elt))

         if A-less do (incf i)
         else do (incf j)

         finally

           (loop
              with first-smaller? = (< i (length A))
              with source = (if first-smaller? A B)
              with start = (if first-smaller? i j)

              for idx from start to (1- (length (if first-smaller? A B)))

              do
                (setf (elt C k)
                      (elt source idx))
                (incf k))))))

Annotation number 2: iterate version
Pasted by: sjl
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+7KP1/2
Paste contents:
Raw Source | Display As
(defun merge- (A B)
  (declare (optimize (speed 3) (safety 0))
           (type (simple-array fixnum) A B))
  (let* ((n (+ (length A)
               (length B)))
         (C (make-array n))
         (i 0)
         (j 0))
    (prog1 C
      (iterate
        (for k from 0 to (1- n))

        (while (and (< i (length A))
                    (< j (length B))))

        (for A-elt = (elt A i))
        (for B-elt = (elt B j))
        (for A-less = (< A-elt B-elt))

        (setf (elt C k)
              (if A-less A-elt B-elt))

        (if A-less
          (incf i)
          (incf j))

        (finally

          (iterate
            (with first-smaller? = (< i (length A)))
            (with source = (if first-smaller? A B))
            (with start = (if first-smaller? i j))

            (for idx from start to (1- (length (if first-smaller? A B))))

            (setf (elt C k)
                  (elt source idx))
            (incf k)))))))

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.