(SubList <list> <start> [<length>])
(SubList '(0 1 2 3 4 5) 2 3)
>> (2 3 4)
(SubList '(0 1 2 3 4 5) 2 nil)
>> (2 3 4 5)
(SubList '(0 1 2 3 4 5) 6 3)
>> nil
(defun SubList ( lst st len )
(repeat st
(setq lst (cdr lst))
)
(if
(or
(not len)
(zerop len)
)
lst
(reverse (SubList (reverse lst) (- (length lst) len) 0))
)
)
;; SUBLIST
;; Returns a sub list
;;
;; Arguments
;; lst : a list
;; start : start index (first item = 0)
;; leng : the sub list length (number of items) or nil
(defun sublist (lst start leng / n r)
(if (or (not leng) (< (- (length lst) start) leng))
(setq leng (- (length lst) start))
)
(setq n (+ start leng))
(while (< start n)
(setq r (cons (nth (setq n (1- n)) lst) r))
)
)
;; SUBLST
;; Returns a sub list
;;
;; Arguments
;; lst : a list
;; start : start index (first item = 0)
;; leng : the sub list length (number of items) or nil
(defun sublst (lst start leng)
(or leng (setq leng (length lst)))
(if (and lst (< 0 leng))
(if (zerop start)
(cons (car lst) (sublst (cdr lst) 0 (1- leng)))
(sublst (cdr lst) (1- start) leng)
)
)
)
(defun LM:SubList ( lst start len )
(if lst
(if (< 0 start)
(LM:SubList (cdr lst) (1- start) len)
(if len
(if (< 0 len)
(cons (car lst) (LM:SubList (cdr lst) start (1- len)))
)
lst
)
)
)
)
(defun AT:SubList (lst left lgth / _trim)
(defun _trim (l i)
(if (> i 0)
(_trim (cdr l) (1- i))
l
)
)
(if lgth
(reverse (_trim (reverse (_trim lst left)) (- (length lst) left lgth)))
(_trim lst left)
)
)
(defun SubList ( lst st len / tempList )
(setq len
(if (not len)
(length lst)
(1- (+ st len))
)
)
(vl-catch-all-apply
(function
(lambda ( / cnt )
(setq cnt 0)
(foreach i lst
(cond
((<= st cnt len)
(setq tempList (cons i tempList))
)
((> cnt len)
(exit)
)
)
(setq cnt (1+ cnt))
)
)
)
)
(reverse tempList)
)
(defun sublist ( lst start len / iterations)
(defun sublist-iter (sofar lis)
(cond
((or (null lis) (eq (length sofar) len))
(reverse sofar) )
((>= iterations start)
(setq iterations (1+ iterations))
(sublist-iter
(cons (car lis) sofar)
(cdr lis)))
( T (setq iterations (1+ iterations))
(sublist-iter '() (cdr lis)))) )
(setq iterations 0)
(sublist-iter '() lst)
)
(defun LM:SubList-iter ( lst start len / r )
(or len (setq len (- (length lst) start)))
(repeat start (setq lst (cdr lst)))
(setq i -1)
(while (and lst (< (setq i (1+ i)) len))
(setq r (cons (car lst) r) lst (cdr lst))
)
(reverse r)
)
(defun f (l s n)
(cond ((not l) nil)
((> s 0) (f (cdr l) (1- s) n))
((not n) l)
((> n 0) (cons (car l) (f (cdr l) 0 (1- n))))
)
)
(defun f1 (l s n / i)
(setq i -1
n (if n (+ s n i) (length l))
)
(vl-remove-if-not (function (lambda (a) (<= s (setq i (1+ i)) n))) l)
)
(defun f2 (l s n)
(repeat (/ s 4)
(setq l (cddddr l))
)
(repeat (rem s 4)
(setq l (cdr l))
)
(setq s nil)
(if (and l n)
(progn (repeat (/ n 4)
(setq s (cons (car l) (cons (cadr l) (cons (caddr l) (cons (cadddr l) s))))
l (cddddr l)
)
)
(repeat (rem n 4)
(setq s (cons (car l) s)
l (cdr l)
)
)
(reverse s)
)
l
)
)
(defun f2 (l s n)
(repeat (/ s 4)
(setq l (cddddr l))
)
(repeat (rem s 4)
(setq l (cdr l))
)
(setq s nil)
(if (and l n)
(progn (repeat (/ n 4)
(setq s (cons [color=red](cadddr l) (cons (caddr l) (cons (cadr l) (cons (car l)[/color] s))))
l (cddddr l)
)
)
(repeat (rem n 4)
(setq s (cons (car l) s)
l (cdr l)
)
)
(reverse s)
)
l
)
)
Benchmarking [M.P. 2005 < revised kdub 2005>] ..................
Elapsed milliseconds for 32768 iteration(s)/ relative Timing :
(TW:L-SUBLIST TESTLIST 9 13)........2886 / 2.4987 <slowest>
(J7:SUBLIST TESTLIST 9 13)..........1997 / 1.7290
(EE:F TESTLIST 9 13)................1747 / 1.5126
(GILE:SUBLST TESTLIST 9 13).........1700 / 1.4719
(LM:SUBLIST TESTLIST 9 13)..........1607 / 1.3913
(LM:SUBLIST-ITER TESTLIST 9 13).....1451 / 1.2563
(EE:F1 TESTLIST 9 13)...............1404 / 1.2156
(EE:F2 TESTLIST 9 13)...............1311 / 1.1351
(AT:SUBLIST TESTLIST 9 13)..........1311 / 1.1351
(GILE:SUBLIST TESTLIST 9 13)........1310 / 1.1342
(TW:SUBLIST TESTLIST 9 13)..........1279 / 1.1074
(KDUB:SUBLIST TESTLIST 9 13)........1155 / 1.0000 <fastest>
(defun TW:SubList3 ( lst st len / tempList )
(repeat st
(setq lst (cdr lst))
)
(if
(or
(not lst)
(not len)
(zerop len)
)
lst
(progn
(repeat len
(setq tempList (cons (car lst) tempList))
(setq lst (cdr lst))
)
(reverse tempList)
)
)
)
I wanted to remove one of the ' reverse ' calls, so here is another version. :-DCode: [Select](defun TW:SubList3 ( lst st len / tempList )
..<snip>
(defun vk_GetSubList51 (lst s l / i)
(repeat 2
(setq i 0
lst (reverse (vl-member-if (function (lambda (e) (< s (setq i (1+ i))))) lst))
s (- (length lst) l)
)
)
lst
)
ok, smth different and slow :)
I wanted to remove one of the ' reverse ' calls, so here is another version. :-DCode: [Select](defun TW:SubList3 ( lst st len / tempList )
..<snip>
About 25% Tim .. same as LM:SUBLIST-ITER
(defun LM:SubList3 ( l s n / _left _right )
(defun _left ( l s n )
(if (and l (< 0 s))
(_left (cdr l) (1- s) n)
(_right (reverse l) s (cond (n (- (length l) n)) ( 0 )))
)
)
(defun _right ( l s n )
(if (and l (< 0 n))
(_right (cdr l) s (1- n))
(reverse l)
)
)
(_left l s n)
)
Just dissected your func Kerry - nice method my friend :wink:
(setq i 0)
(repeat 10000
(setq l (cons i l)
i (1+ i)
)
)
(test l 4000 8000)
(defun db:subl (l s e / i tmp)
(setq i (1- s))
(or e (setq e (length l)))
(while (and (nth i l)
(< i (+ s e -1)))
(setq tmp (cons (nth i l) tmp)
i (1+ i)))
(reverse tmp))
-David
;; http://cadtutor.net/forum/showpost.php?p=218577&postcount=9
(defun sublst (l s n / tmp)
(repeat n (setq tmp (cons (nth s l) tmp))
(setq s (1+ s)))
(reverse tmp))
David remember this one:Code: [Select];; http://cadtutor.net/forum/showpost.php?p=218577&postcount=9
(defun sublst (l s n / tmp)
(repeat n (setq tmp (cons (nth s l) tmp))
(setq s (1+ s)))
(reverse tmp))
David remember this one:Code: [Select];; http://cadtutor.net/forum/showpost.php?p=218577&postcount=9
(defun sublst (l s n / tmp)
(repeat n (setq tmp (cons (nth s l) tmp))
(setq s (1+ s)))
(reverse tmp))
(defun ss-sublist (lst i n / len r)
(setq len (length lst)
n (if n (min (+ i n) len) len))
(while (> n i)
(setq r (cons (nth (setq n (1- n)) lst) r))
)
)
when the list len=30 , (ss-sublist testlist 9 13)my test wayCode: [Select](setq i 0
TestList '()
)
(repeat 10000
(setq TestList (cons i TestList)
i (1+ i)
)
)
(progn
(gc)
(times 10
'sublist
(list testlist
20
9980
)
)
(gc)
(times 10
'ss-sublist
(list testlist
20
9980
)
)
(gc)
)
(defun times (ti funname funarglist / t1 t2)
(setq t1 (getvar "date"))
(repeat ti
(vl-catch-all-apply
funname
funarglist
)
)
(setq t2 (getvar "date"))
(princ "函数:")
(princ funname)
(princ (strcat "运行" (rtos ti 2 0) "次" ))
(princ "测试结果")
(princ (menucmd (strcat "M=$(edtime,"
(rtos (- t2 t1) 2 16)
",HH:MM:SS:MSEC)"
)
)
)
)
[/s]
Something simple ( like me )Code: [Select](defun db:subl (l s e / i tmp)
-David
(setq i (1- s))
(or e (setq e (length l)))
(while (and (nth i l)
(< i (+ s e -1)))
(setq tmp (cons (nth i l) tmp)
i (1+ i)))
(reverse tmp))
(defun sublist (l s n / tmp)
(setq n (+ s n))
(while (<= s n)
(setq tmp (cons (nth s l) tmp))
(setq s (1+ s))
)
(reverse tmp)
)
_$ (sublist '(0 1 2 3 4 5) 2 nil)
; error: bad argument type: numberp: nil
Hi , LeeCode: [Select]_$ (sublist '(0 1 2 3 4 5) 2 nil)
:?
; error: bad argument type: numberp: nil
(defun sublist (l s n / tmp)
(setq n (if (not n)
(length l)
(1- (+ s n))
)
)
(while (<= s n)
(setq tmp (cons (nth s l) tmp))
(setq s (1+ s))
)
(reverse tmp)
)
Ahhhh That's another story :-)
(setq i 0
TestList '()
)
(repeat 10000
(setq TestList (cons i TestList)
i (1+ i)
)
)
Benchmarking [M.P. 2005] ........Elapsed milliseconds for 32 iteration(s)/ relative Timing :
...
(AT:SUBLIST TESTLIST 4000 4000)...........156 / 1.6596
(TW:SUBLIST TESTLIST 4000 4000)...........140 / 1.4894
(VK_GETSUBLIST51 TESTLIST 4000 4000)......125 / 1.3298
(EE:F2 TESTLIST 4000 4000).................94 / 1.0000 <fastest>
Benchmarking [M.P. 2005] ........Elapsed milliseconds for 32 iteration(s)/ relative Timing :
...
(VK_GETSUBLIST51 TESTLIST 20 9980).......109 / 1.0000
(LM:SUBLIST3 TESTLIST 20 9980)...........109 / 1.0000
(AT:SUBLIST TESTLIST 20 9980)............109 / 1.0000
(TW:SUBLIST TESTLIST 20 9980)............109 / 1.0000 <fastest>
(setq i 1
TestList '()
)
(repeat 10000
(setq TestList (cons (list i (1+ i) (+ i 2)) TestList)
i (+ i 3)
)
)
Test fun(defun ss-test (ti funlist / t1 t2 tilst ilst i)
;;时间测试函数,funlist 格式为 ((test1 arg) (test2 arg) (test3 arg))
;;GSLS(SS) 2011-5-21
(foreach funname funlist
(gc)
(setq t1 (getvar "DATE")
t1 (* 86400000.0 (- t1 (fix t1))))
(repeat ti
(vl-catch-all-apply
(car funname)
(cdr funname)
)
)
(setq t2 (getvar "DATE")
t2 (* 86400000.0 (- t2 (fix t2)))
tilst (append tilst (list(* 1. (- t2 t1))))
t1 nil
t2 nil)
)
(setq ilst (vl-sort-i tilst (function <)))
(princ (strcat "\n测试耗时......毫秒 / 相对速度 ...... 迭代次数" (rtos ti 2 0) "...:"))
(foreach i ilst
(princ (strcat "\n "
(vl-prin1-to-string
(car (nth i funlist))
)
"......"
(rtos (nth i tilst) 2 0)
" / "
(rtos (/ (nth (last ilst) tilst) (nth i tilst)) 2 2)
"......"
(cond ((= i (car ilst)) "<Fastest>")
((= i (last ilst)) "<Slowest>")
(t ""))
)
)
)
(princ)
)
(ss-test 100000 '((AT:SubList TestList 20 5000) (LM:SubList TestList 20 5000) (LM:SubList3 TestList 20 5000) (vk_GetSubList51 TestList 20 5000) (TW:SubList TestList 20 5000)
(TW:SubList2 TestList 20 5000) (TW:SubList3 TestList 20 5000) (gc:SubList TestList 20 5000) (gc:SubLst TestList 20 5000) (ee:f TestList 20 5000)
(ee:f1 TestList 20 5000) (ee:f2 TestList 20 5000)))
Tested in CPU:Core-i7 , Member:4G, System:32bits Win7 , Compiled: No , ACADversion : 2010(ss-test 100000 '((AT:SubList TestList 20 9980) (LM:SubList TestList 20 9980) (LM:SubList3 TestList 20 9980) (vk_GetSubList51 TestList 20 9980) (TW:SubList TestList 20 9980)
(TW:SubList2 TestList 20 9980) (TW:SubList3 TestList 20 9980) (gc:SubList TestList 20 9980) (gc:SubLst TestList 20 9980) (ee:f TestList 20 9980)
(ee:f1 TestList 20 9980) (ee:f2 TestList 20 9980) (ss:sublist TestList 20 9980)))
测试耗时......................毫秒 / 相对速度 ...... 迭代次数100000...:(ss-test 100000 '((AT:SubList TestList 4000 4000) (LM:SubList TestList 4000 4000) (LM:SubList3 TestList 4000 4000) (vk_GetSubList51 TestList 4000 4000) (TW:SubList TestList 4000 4000)
(TW:SubList2 TestList 4000 4000) (TW:SubList3 TestList 4000 4000) (gc:SubList TestList 4000 4000) (gc:SubLst TestList 4000 4000) (ee:f TestList 4000 4000)
(ee:f1 TestList 4000 4000) (ee:f2 TestList 4000 4000) (ss:sublist TestList 4000 4000)))
测试耗时.......................毫秒 / 相对速度 ...... 迭代次数100000...: