Author Topic: better coding...  (Read 2368 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
better coding...
« on: July 26, 2017, 09:26:45 AM »
Hi all,..

is there a better way or more elegant to do this:

Code: [Select]
(defun _splitdist (x_length x_dist / vallist rval)
  (setq val (/ x_length x_dist)
rval (/ x_length val)
vallist (list x_length)
  )
  (repeat (1- val)
    (setq vallist
   (append vallist (list (setq x_length (- x_length rval))))
    )
  )
  (reverse vallist)
)

;ex
;(_splitdist 360 45)
Keep smile...

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: better coding...
« Reply #1 on: July 26, 2017, 09:41:17 AM »
What about
Code: [Select]
(defun split-dist (len dis / result)
   (while (> len 0)
      (setq result (cons dis result))
            len (- len dis)))
   result)

If you want it from 0 onwards:
Code: [Select]
(defun split-dist (len dis / result)
   (while (> len 0)
      (setq result (cons dis result))
            len (- len dis)))
   (setq dis (car result))
   (mapcar '(lambda (x) (- x dis)) result))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #2 on: July 26, 2017, 09:51:09 AM »

Add a description of WHAT the program is meant to achieve.
and several example result sets.


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: better coding...
« Reply #3 on: July 26, 2017, 10:11:51 AM »

Add a description of WHAT the program is meant to achieve.
and several example result sets.

this funtion create a list of distance set by a user,..
example if you have a line 400 long and need to divide that line to 47 ,..then retreive all nearest point to each section by distance list form the start.

so the list must be something like this:
Code: [Select]
'(8.51063  17.0326 25.5432 .....)
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: better coding...
« Reply #4 on: July 26, 2017, 10:12:57 AM »
What about
Code: [Select]
(defun split-dist (len dis / result)
   (while (> len 0)
      (setq result (cons dis result))
            len (- len dis)))
   result)

If you want it from 0 onwards:
Code: [Select]
(defun split-dist (len dis / result)
   (while (> len 0)
      (setq result (cons dis result))
            len (- len dis)))
   (setq dis (car result))
   (mapcar '(lambda (x) (- x dis)) result))

Thank you .
Keep smile...

hugha

  • Newt
  • Posts: 103
Re: better coding...
« Reply #5 on: July 26, 2017, 10:28:09 PM »
Just pointing out that if len is very large compared to dis you may suffer floating point rounding error from the many successive subtractions of a small number from a large one.

Better to precalculate N = the number of points to generate and result = list(N*dis   (N-1)*dis   ....   dis).


hth
Hugh Adamson




kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #6 on: July 26, 2017, 11:40:49 PM »

This is what happens without decent documentation -->

(_splitdist 130 45)
     returns : (65 130)

(_splitdist 260 45)
     returns (52 104 156 208 260)

(split-dist 130 45)
     (45 45 45)

(_splitdist 400 47)
     returns (50 100 150 200 250 300 350 400)

This is not in conformance with the example result you posted ie
Quote
'(8.51063  17.0326 25.5432 .....)


For someone who claims to be a professional programmer this is abysmal.
If you expect help at least make an attempt to define the problem correctly.
 
« Last Edit: July 26, 2017, 11:45:26 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #7 on: July 26, 2017, 11:55:57 PM »


... and because repeat expects an integer, ..
this will fall on its head if either parameter ( x_length OR x_dist ) is a real ; so val will be a real
(repeat (1- val)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

bilançikur

  • Newt
  • Posts: 82
Re: better coding...
« Reply #8 on: July 28, 2017, 01:51:26 AM »
@ kdumb (Kerry)

I know that your signature is
Quote
"Called Kerry in my other life / Sometimes the question is more important than the answer."

You are absolutely right! If you need a good answer then ask a good question first, giving you that much of credit.
But reading your reaction to Andrea gives me the creeps;

For someone who claims to be a professional programmer this is abysmal.
If you expect help at least make an attempt to define the problem correctly.

"Who claims to be ..." <-- is the part that bothers me.
My reaction is "Who do you think you are with your almost too correct english?"
To me you seem like a grumpy old man, one that lives in the neighbourhood but does not say "Hello" when passing by.
At home always whining "They never talk to me".

It's good to know you have another life, I hope you are not such a grumpy one in that life.

You have no idea the amount of help Andrea has provide to many of us in the past years.
Keep that in mind if you will, okay? Thank you.

Also, do not forget the ideas wich come from Andrea.
Never seen such ideas coming from you (okay neither from me). Just saying though.
Remember DiLeader, how many times it was read discussed and I am sure used by others?
Neah.. I guess that was not good enough for you too.

My point is: if you feel like the question is not good enough for you then just don't post a reply.
Do not lecture if your not a teacher. The forum is for everyone and with any level of skills.
Don't make yourself ridiculous, I mean more ridiculous than you have already have to me.
Instead, maybe you could somehow revise your answers or maybe make just a little (the smallest) apology.

In case you wonder: "Why do I stand up for him?"
Because it seems no one else does. And that is a shame!
Too often I read negative reactions and no one ever stands up for anyone.
But I am sure whenever there is posted a new lisp tool we all would not hesitate to go download it.
Quickly!

There are 3 things I'd like to say, that be the end of my reaction as well:
1. Do not reply, I don't give a $
2. In case of a ban, I shall take it like a man!
3. Many of us on ths forum have forgotten about Fixo. A damn shame as well!
https://www.theswamp.org/index.php?topic=48828.0

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #9 on: July 28, 2017, 02:02:02 AM »
@ bilançikur

Thank you for your opinion.


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #10 on: July 28, 2017, 02:36:08 AM »
@ bilançikur

I wonder if you could assist me.
I asked Andrea this

Add a description of WHAT the program is meant to achieve.
and several example result sets.


.. and did not receive a satisfactory answer.

I'm wondering if you could give your understanding ( from his posts ) regarding the expected functionality of the code he posted for comment and suggestions?

Regards,
 
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

JohnK

  • Administrator
  • Seagull
  • Posts: 10631
Re: better coding...
« Reply #11 on: July 28, 2017, 10:10:39 AM »
bilançikur,
Please refrain from name calling. I do not want to see it again.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: better coding...
« Reply #12 on: July 28, 2017, 09:28:10 PM »
Just to close this thread on a positive note :
This is one way to solve the issue, assuming my assumptions regarding required functionality are correct.
Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.         _splitdist (dist segment_count)
  3.     kdub 2017/07/29 @ theSwamp
  4.    
  5.         Divide dist by segment_count portions.
  6.         Return a list of the progressive sum of the portion distances
  7.        
  8.         (_splitdist 360 8)
  9.                 => (45.0 90.0 135.0 180.0 225.0 270.0 315.0 360.0)
  10.         (_splitdist -400 6)
  11.                => (-66.6667 -133.333 -200.0 -266.667 -333.333 -400.0)
  12.        
  13.         assumes:
  14.                 segment_count is a non-zero integer            
  15. |;
  16. (defun _splitdist (dist segment_count / portion_distance returnlist)
  17.   (if (and (not (zerop segment_count))
  18.            (= (rem segment_count 1) 0)
  19.       )
  20.     (progn
  21.       (setq portion_distance (/ (float dist) segment_count)
  22.             returnlist       '()
  23.             index            1
  24.       )
  25.       (repeat segment_count
  26.         (setq returnlist
  27.                          (append returnlist (list (* index portion_distance)))
  28.               index      (1+ index)
  29.         )
  30.       )
  31.     )
  32.   )
  33.   returnlist
  34. )
  35.  
  36.  


« Last Edit: July 28, 2017, 09:35:55 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.