Paste number 23675: 3 Loops => 1 Loop

Index of paste annotations: 1

Paste number 23675: 3 Loops => 1 Loop
Pasted by: kingruedi
2 years, 5 months ago
#lisp
Paste contents:
Raw Source | XML | Display As
; I have a function to convert binary data into a list of  cons
; it looks something like that (I know you could do more with loop. But this is just for demo purpose)

(defun convert-to-cons (binary-data)
  (let ((pos 0) (list-of-cons '()))
    (loop do
       (let ((length (some-code-0 binary-data))
         (setf list-of-cons (append list-of-cons
                                    (some-code-1 binary-data pos length)
)
)

         (incf pos length)
)
)
)
)


; The only thing I do with that function is

(loop for i in (convert-to-cons binary-data)
      with coll
      do (case (car i)
           (foo (setf coll '(foo . bar)))
            ; ...
            
)

      collect coll
)


; and finally I pass the newly created list of cons to a function which creates binary-data from the list

(defun convert-to-binary (list-of-cons)
  (loop for i in list-of-cons do
    (some-code-2 i)
)
)

Annotations for this paste:

Annotation number 1: The updatet function
Pasted by: kingruedi
2 years, 5 months ago
Paste contents:
Raw Source | Display As
(defun read-name-value-pairs (buffer &optional (handle-cons #'(lambda (c) c)))
  (loop
     with ptr = 0
     with name-length
     with value-length
     when (>= ptr (length buffer)) do (loop-finish)
     do (progn
          (setf name-length (aref buffer ptr))
          (when (= (ash name-length -7) 1) ; 31 bit name-length?
             (setf name-length (+ (ash (logand name-length #x7f) 24)
                                  (ash (aref buffer (incf ptr)) 16)
                                  (ash (aref buffer (incf ptr)) 8)
                                  (aref buffer (incf ptr))
)
)
)


          (setf value-length (aref buffer (incf ptr)))
          (when (= (ash value-length -7) 1) ; 31 bit value-length?
               (setf value-length (+ (ash (logand value-length #x7f) 24)
                                  (ash (aref buffer (incf ptr)) 16)
                                  (ash (aref buffer (incf ptr)) 8)
                                  (aref buffer (incf ptr))
)
)
)
)

     collect (apply handle-cons (cons (subseq buffer (incf ptr)
                                    (incf ptr (1+ name-length))
)

                            (subseq buffer ptr
                                    (incf ptr (1+ value-length))
)
)
)
)
)

Colorize as:
Show Line Numbers
Index of paste annotations: 1

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