Author Topic: iteration code : short and efficiency challenge  (Read 13680 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #45 on: July 22, 2015, 10:27:19 AM »
Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)

(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst
    (cdr lst)
    )
  )


Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
Code: [Select]
; error: bad argument type: stringp 0.0

(distance) returns a double (real), not a string.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #46 on: July 22, 2015, 01:41:09 PM »
Thanks Lee

this solution is not working for me

Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(1 0 0)(2 0 0)(3 0 0)) )
Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 1
b = 2

3th iteration

a = 2
b = 3

while i want

Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 2
b = 3



Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #47 on: July 22, 2015, 01:59:39 PM »
i want
Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 2
b = 3

Use something like:

Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l )
  2.     (while (cadr l)
  3.         (princ (strcat "\nSegment length: " (rtos (distance (car l) (cadr l)))))
  4.         (setq l (cddr l))
  5.     )
  6.     (princ)
  7. )

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #48 on: July 22, 2015, 02:10:12 PM »
Why mapcar wasnt your defualt choise for thos task?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #49 on: July 22, 2015, 02:30:07 PM »
Why mapcar wasnt your defualt choise for thos task?

Because mapcar iterates over every item; you are asking to iterate over every other item.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #50 on: July 23, 2015, 12:31:46 AM »
Why mapcar wasnt your defualt choise for thos task?

Because mapcar iterates over every item; you are asking to iterate over every other item.

Thanks Lee (thats what i thought but i was sure that i wrong)

put it all together, i see in the Watch that the list tems are removed before the interpeter even get there and there for i cant get the return that i want

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc  PTLIST)
  (if (and (>= typ mn) (<= typ mx))                                                      ;; if typ is greater/smaller or equal to min/max
    (if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if ther is reminder
      (append (vl-remove (car PTLIST) PTLIST) (dividex stp etp 2))
       PTLIST
  )
        )
   
  )
   

the list i expect to get

Code: [Select]
(getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)

((20.0 0.0 0.0)(16.0 0.0 0.0) (12.0 0.0 0.0) (6.0 0.0 0.0) (0.0 0.0 0.0))

Thanks
Shay

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: iteration code : short and efficiency challenge
« Reply #51 on: July 23, 2015, 04:45:20 AM »
When you use append in your 3rd function the new start point is still in the list of points.
The rest you should be able to figure out yourself.

Note:
Please don't use this:
Code: [Select]
(vl-remove (car PTLIST) PTLIST)Instead of this:
Code: [Select]
(cdr PTLIST)

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #52 on: July 23, 2015, 08:25:55 AM »
When you use append in your 3rd function the new start point is still in the list of points.
The rest you should be able to figure out yourself.

Note:
Please don't use this:
Code: [Select]
(vl-remove (car PTLIST) PTLIST)Instead of this:
Code: [Select]
(cdr PTLIST)

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc  PTLIST)
  (if (and (>= typ mn) (<= typ mx))                                                      ;; if typ is greater/smaller or equal to min/max
    (if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if the distance between past last point to the end is different than typ
     ;; it means that there is a remaining
     

      (reverse (append  (dividex stp etp 2)(cddr PTLIST)))
      (reverse PTLIST)
  )
        )
   
  )


   

Code: [Select]
_$ (getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)
Code: [Select]
((0.0 0.0 0.0) (6.0 0.0 0.0) (12.0 0.0 0.0) (16.0 0.0 0.0) (20.0 0.0 0.0))
Thanks roy_43

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #53 on: July 25, 2015, 12:05:10 AM »
The code is measuring specified distance by specified segment length typ  and return resulting 3d point list,
If a reminder left over typ+reminder, its being tested for its length:
If the left over segment length is smaller or equal to maximum , remove the last point and add the end point to the list, return resulting list.
If the left over segment length is greater than the maximum limit, divide its length into I , and try to pass the test again.
If the left over segment is smaller than minimum than prompt the user and exit.

How can I code it more efficient , shorter , stable?

Thanks
Shay

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the specified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ml i w f )
  (setq i 1)
  (if (and (> typ mn) (< typ mx)(>= (distance stp etp) typ))                                                     
    (if (/= (setq dist (distance (setq stp (cadr (setq ml (measurex stp etp typ)))) etp)) typ ) ;_if there is a reminder
(while (null f)             ;_flag.is there a need to loop? t=no nil=yes
  (if (>= dist mn)     ;_is greater than minimum limit
    (if (<= dist mx)     ;_is smaller than maxiuum limit
      (if (null w)     ;_flag. segment were divided?. t=no nil-yes
  (setq f t ml (cdr ml) ml (cons etp ml))     ;_flag. no loop is needed. return list+end point
  (progn
    (setq f t)     ;_flag. no loop is needed.
    (append (reverse(cdr ml))(cdr(reverse(dividex stp etp i))))     ;_return list+divid seg+reminder equally
    )
      )

     
(setq i (1+ i) dist (/ (distance stp etp)i) w t)    ;_divid seg when it doesnt meet maximum length, start over

   
  )
    (princ "Cannot constract a layout with those limits")    ;_stop when seg length doesnt meet minimum length
         )
               )
       ml    ;_return list when no reminder left
             )
    )
  )