Dear Lee Mac, as per your reply, it means there is no way to properly break through foreach loop. Am i correct ?
You can use condition statements (ie; cond, if) within the foreach loop.It won't break the loop.
(defun c:test ( / n )
(setq n 0)
(vl-every
(function
(lambda ( x )
(if (/= x 3)
(setq n (1+ n))
)
)
)
'(0 1 2 3 4 5)
)
(princ "\nNumber of items processed: ")
(princ n)
(princ)
)
(mapcar_while
'(1 2 3 4 5 6 7)
'(lambda (x) (* x x))
'(lambda (x) (/= x 4))
)
-->(1 4 9)
parktaeeun is correct:...vl-every also seems faster...Code: [Select](defun c:test ( / n )
(setq n 0)
(vl-every
(function
(lambda ( x )
(if (/= x 3)
(setq n (1+ n))
)
)
)
'(0 1 2 3 4 5)
)
(princ "\nNumber of items processed: ")
(princ n)
(princ)
)
That said, it's not how I would normally approach it and on that Lee already illuminated.
(progn
(princ "\Posit: ")
(princ (setq posit 100000))
(setq aListInt '("Last") Countr 0)
(repeat 1000000 (setq aListInt (cons (setq Countr (1+ Countr)) aListInt)))
(princ "\nLength aListInt: ") (princ (length aListInt)) (princ "\n ") (princ)
)
Posit: 100
Length aListInt: 1000001
Elapsed milliseconds / relative speed for 4096 iteration(s):
(ALE_LIST_EVERYBYLEN POSIT ALISTINT).......1108 / 2.76 <fastest>
(ALE_LIST_FOREACHBYLEN POSIT ALISTINT).....1108 / 2.76
(ALE_LIST_EVERYBYLEN POSIT ALISTINT).......1295 / 2.36
(ALE_LIST_FOREACHBYLEN POSIT ALISTINT).....1342 / 2.28
(ALE_LIST_MAPCARBYLEN POSIT ALISTINT)......1623 / 1.88
(ALE_LIST_MAPCARBYLEN POSIT ALISTINT)......3057 / 1 <slowest>
Posit: 100000
Length aListInt: 1000001
Elapsed milliseconds / relative speed for 16 iteration(s):
(ALE_LIST_EVERYBYLEN POSIT ALISTINT).......1716 / 2.27 <fastest>
(ALE_LIST_FOREACHBYLEN POSIT ALISTINT).....1841 / 2.12
(ALE_LIST_MAPCARBYLEN POSIT ALISTINT)......2184 / 1.79
(ALE_LIST_EVERYBYLEN POSIT ALISTINT).......3354 / 1.16
(ALE_LIST_FOREACHBYLEN POSIT ALISTINT).....3869 / 1.01
(ALE_LIST_MAPCARBYLEN POSIT ALISTINT)......3900 / 1 <slowest>
; 03-01-2014
(defun ALE_List_ForeachByLen (n l / c r)
(setq c 1)
(vl-catch-all-apply
(function
(lambda ( )
(foreach x l
(if (> c n)
(exit)
(setq r (cons x r) c (1+ c))
)
)
)
)
)
(reverse r)
)
; 03-01-2014
(defun ALE_List_MapcarByLen (n l / c r)
(setq c 1)
(vl-catch-all-apply
'mapcar
(list
'(lambda (x)
(if (> c n)
(exit)
(setq r (cons x r) c (1+ c))
)
)
l
)
)
(reverse r)
)
;
(defun ALE_List_EveryByLen (n l / c r)
(setq c 1)
(vl-every
(function
(lambda (x)
(if (<= c n)
(setq r (cons x r) c (1+ c))
)
)
)
l
)
(reverse r)
)
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Posit: 100
Length aListInt: 1000001
Elapsed milliseconds / relative speed for 8192 iteration(s):
(ALE_LIST_EVERYBYLEN2 POSIT ALISTINT).......1170 / 5.52 <fastest>
(ALE_LIST_EVERYBYLEN2 POSIT ALISTINT).......2574 / 2.51
(ALE_LIST_FOREACHBYLEN2 POSIT ALISTINT).....3260 / 1.98
(ALE_LIST_FOREACHBYLEN2 POSIT ALISTINT).....3729 / 1.73
(ALE_LIST_MAPCARBYLEN2 POSIT ALISTINT)......5522 / 1.17
(ALE_LIST_MAPCARBYLEN2 POSIT ALISTINT)......6459 / 1 <slowest>
Posit: 100000
Length aListInt: 1000001
Elapsed milliseconds / relative speed for 16 iteration(s):
(ALE_LIST_EVERYBYLEN2 POSIT ALISTINT).......1107 / 2.76 <fastest>
(ALE_LIST_EVERYBYLEN2 POSIT ALISTINT).......2216 / 1.38
(ALE_LIST_MAPCARBYLEN2 POSIT ALISTINT)......2340 / 1.31
(ALE_LIST_MAPCARBYLEN2 POSIT ALISTINT)......2496 / 1.22
(ALE_LIST_FOREACHBYLEN2 POSIT ALISTINT).....2995 / 1.02
(ALE_LIST_FOREACHBYLEN2 POSIT ALISTINT).....3057 / 1 <slowest>
(defun ALE_List_ForeachByLen2 (n l / c r)
(setq c 1)
(vl-catch-all-apply
(function
(lambda ( ) (foreach x l (if (> c n) (exit) (setq c (1+ c)))))
)
)
)
(defun ALE_List_MapcarByLen2 (n l / c r)
(setq c 1)
(vl-catch-all-apply
'mapcar
(list '(lambda (x) (if (> c n) (exit) (setq c (1+ c)))) l)
)
)
(defun ALE_List_EveryByLen2 (n l / c r)
(setq c 1)
(vl-every
(function
(lambda (x) (if (<= c n) (setq c (1+ c))))
)
l
)
)
(generator
'TEST
'(lambda (a b / c )
(while (> a 0)
(setq c b)
(while (> b 0)
(print b)
(if (> (yield a) 5)
(yield (cons a b))
)
(setq b (1- b))
)
(setq a (1- a))
(setq b c)
"This is the end"
)
)
'(10 5)
)
The function 'generator' parses the given lambda-expression to a "generator-function" and stores it in the given Symbol (here 'TEST).(test) ;-> (T . 10)
(test) ;-> (T 10 . 5)
.
.
.
(test) ;-> (nil . "This is the end")