Message 1 of 14
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
For this list
(210.655 98.7758)
How to apply mapcar to rtos 2 2
to get
("210.65" "98.77")
Solved! Go to Solution.
For this list
(210.655 98.7758)
How to apply mapcar to rtos 2 2
to get
("210.65" "98.77")
Solved! Go to Solution.
Use something like a loop,
iterate thru the list and do what you want..
FOREACH WHILE REPEAT... or
a mapcar with a fancy anonymous lambda ...
(setq MyList '(210.655 98.7758))
(mapcar '(lambda(n)(rtos n 2 2 ))MyList)
Sebastian
This works.. but I guess you can't pass the options to the rtos function
(setq l1 '(210.655 98.7758))
(Setq l2 (mapcar 'rtos l1))
That's why you had to make a lambda function?
That it is.
Also it will give the chance to at last understand LAMBDA .
Since I see it , I never understand LAMBDA .
Hope it will be, LAMBDA, clear to me.
hmm - I guess i have a translation problem, SORRY:
I don't know what you asked about, devitg didn't ask about options and passing!?
MAPCAR expected ONE function and one or more lists,
so i defined a function like i need.
If you want a function for using more often, define a named function and add it to your library.
(defun rtos+ (LI MODE PREC) ; never forget dimzin unitmode luprec
(if (= 'LIST (type LI))
(mapcar '(lambda (N) (rtos N (if MODE MODE (getvar 'DIMLUNIT)) (if PREC PREC (getvar 'LUPREC))) LI)
(rtos N (if MODE MODE (getvar 'DIMLUNIT)) (if PREC PREC (getvar 'LUPREC)))
)
)
Sebastian
lambda - on the surface
is like defun, but without a name, so you can use it only at one place.
(if you would create a named function by defun, it is easy to use this function at multiple locations in your code)
So:
If you are sure that you need a function only at one place in your code, you can use lambda to save memory and free symbolnames.
If you knowing you can use a function multiple times in the code, or this function is an extra logical part of your program, use defun.
Sebastian
I was just saying that, this works:
(setq l1 '(210.655 98.7758))
(Setq l2 (mapcar 'rtos l1))
but returns this (for me): ("17'-6 21/32\"" "8'-2 199/256\"")
...and there is no way to pass rtos to mapcar withe the options the op wanted. the 2's:
(setq l1 '(210.655 98.7758))
(Setq l2 (mapcar 'rtos 2 2 l1))<-- Does not work.
Hi @devitg
My post is maybe a bit out of topic regarding using mapcar and rtos, but you may
find this useful.
Here is a function that converts real number to conform to ISO number representation,
i.e. number is formatted to # ### ###,### or # ### ###.### pattern.
(defun format_number (num decimals delimiter / por b i ret neg)
(if (minusp num) (setq neg "-") (setq neg ""))
(setq rest(- (abs num) (fix (abs num))))
(if (and (> rest 0.5) (= decimals 0)) (setq num (+ num 1)))
(setq rest (substr (rtos rest 2 decimals) 3) ret "")
(setq poz (vl-string-position (ascii ".") rest))
(setq num (abs (fix num)) i 0 j 0)
(while (> num 0.0)
(setq por (cons (fix (rem num 1000.0)) por)
num (/ (- num (car por)) 1000.0)
)
)
(while (< i (length por))
(setq b (rtos (nth i por) 2 0))
(if (and (eq (strlen b)1) (> i 0)) (setq b (strcat "00" b)))
(if (and (eq (strlen b)2) (> i 0)) (setq b (strcat "0" b)))
(if (< i (- (length por) 1)) (setq ret (strcat ret b " ")) (setq ret (strcat ret b)))
(setq i (+ 1 i))
)
(if (= (strlen ret) 0) (setq ret "0"))
(if (= decimals 0) (setq delimiter "" rest ""))
(strcat neg ret delimiter rest)
)
(defun format_list_of_numbers (lst decimals delimiter)
(mapcar '(lambda (x) (format_number x decimals delimiter)) lst))
Here is a usage sample:
(format_list_of_numbers '(1050250.353 -25254.343545 35.35356) 3 ",")
>>("1 050 250,353" "-25 254,344" "35,354")
Miljenko Hatlak
You can also pass a lambda function
(defun myfunct (lst fun) (mapcar fun lst))
Command: (myfunct (list (list 1 "A") (list 2 "B") (list 3 "C")) '(lambda (x) (cadr x))) => ( "A" "B" "C")
I am not one of the robots you're looking for
@Shneuph wrote:
...and there is no way to pass rtos to mapcar withe the options the op wanted. the 2's:
Right, because of...how mapcar is defined and works.
(Setq l2 (mapcar 'rtos 2 2 l1))<-- Does not work.
Sure not, Press [F1] and read about the MAPCAR Lispfunction instead of wild testings.
Mapcar want one function (first argument) and one (or more) lists.
(mapcar 'DoThis withthisList1)
http://help.autodesk.com/view/ACD/2020/DEU/?guid=GUID-8802AE73-1A05-457E-8A51-09677C23E26E
No place for passing arguments to the function, so i need a function which includes my rtos arguments... my lambda function!
Sebastian
I get it, but it seems to me that if MODE and PREC are both nil, then just
(mapcar 'rtos LI)
John F. Uhden
Understood. I was just chiming in with a note of interest since I am bored at home basically doing nothing.
My wife wishes that we had a big yacht to disappear out into the ocean to avoid this mess.
John F. Uhden
@john.uhden wrote:
Understood. I was just chiming in with a note of interest since I am bored at home basically doing nothing.
My wife wishes that we had a big yacht to disappear out into the ocean to avoid this mess.
You made my day 🤣
Sebastian