Author Topic: -={ Challenge }=- SubStr for Lists  (Read 11571 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
-={ Challenge }=- SubStr for Lists
« on: October 01, 2010, 03:11:11 PM »
The Challenge:

To construct a function to return a section of a list, with behaviour akin to the substr function (with the exception of a zero-based index)

Code: [Select]
(SubList <list> <start> [<length>])
Examples:

Code: [Select]
(SubList '(0 1 2 3 4 5) 2 3)
>> (2 3 4)

Code: [Select]
(SubList '(0 1 2 3 4 5) 2 nil)
>> (2 3 4 5)

Code: [Select]
(SubList '(0 1 2 3 4 5) 6 3)
>> nil


T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #1 on: October 01, 2010, 03:49:54 PM »
Code: [Select]
(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))
    )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- SubStr for Lists
« Reply #2 on: October 01, 2010, 03:50:13 PM »
Hi,

this one i posted here:

Code: [Select]
;; 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))
  )
)

Or a recursive form:

Code: [Select]
;; 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)
    )
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #3 on: October 01, 2010, 03:52:23 PM »
Nice one guys!, some interesting results  :-)

Here was mine:

Code: [Select]
(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
      )
    )
  )
)

Similar to Giles I think

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- SubStr for Lists
« Reply #4 on: October 01, 2010, 03:53:33 PM »
Code: [Select]
(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)
  )
)
« Last Edit: October 03, 2010, 09:18:37 AM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #5 on: October 01, 2010, 04:11:47 PM »
In case the list is long.
Code: [Select]
(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)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #6 on: October 01, 2010, 04:28:34 PM »
Here's one that was prepared earlier

Similar to most ..
Code - Auto/Visual Lisp: [Select]
  1. ;;;-----------------------------------------------------------------------------------
  2. ;;;-----------------------------------------------------------------------------------
  3. ;;;
  4. ;;; KDUB:sublist Return a sub-list
  5. ;;;
  6. ;;; Arguments
  7. ;;; lst : a list
  8. ;;; start : start index for the sub-list (first item = 0)
  9. ;;; leng : sub-list length (or nil)
  10. ;;;
  11. ;;; Examples :
  12. ;;; (KDUB:sublist '(1 2 3 4 5 6) 2 2) -> (3 4)
  13. ;;; (KDUB:sublist '(1 2 3 4 5 6) 2 nil) -> (3 4 5 6)
  14.  
  15. (defun KDUB:sublist (lst start leng / n r)
  16.   (if (or (not leng) (< (- (length lst) start) leng))
  17.     (setq leng (- (length lst) start))
  18.   )
  19.   (setq n (+ start leng))
  20.   (repeat leng (setq r (cons (nth (setq n (1- n)) lst) r)))
  21. )
« Last Edit: September 02, 2012, 08:43:15 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #7 on: October 01, 2010, 04:34:09 PM »
Interesting ::

Excluding Tim's routine for 'Long lists'
There is a 46% range difference in speed for those posts  :?

using
(setq TestList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30))
(doit:sublist TestList 9 13)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: -={ Challenge }=- SubStr for Lists
« Reply #8 on: October 01, 2010, 04:43:00 PM »
This one shouldn't beat any speed records but I wanted to be different.

Code: [Select]
(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)
 )


EDIT: Found a mistake (extra code) and fixed it. It will still operate as was before.
« Last Edit: October 01, 2010, 05:20:22 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #9 on: October 01, 2010, 05:00:54 PM »
Another, iterative version...

Code: [Select]
(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)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #10 on: October 01, 2010, 05:01:13 PM »
three sketches...
Code: [Select]
(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))))
 )
)
Code: [Select]
(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)
)
Code: [Select]
(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
 )
)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #11 on: October 01, 2010, 05:17:31 PM »
Nice coding Evgeniy  8-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #12 on: October 01, 2010, 05:18:18 PM »
 :oops:
Code: [Select]
(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
 )
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #13 on: October 01, 2010, 05:21:46 PM »
Moscow now has one in the morning.
I go to bed...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #14 on: October 01, 2010, 05:33:46 PM »
using
(setq TestList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30))

Haven't tested with any varying length Lists

The range is about 10% to 50% difference
Code: [Select]

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>

 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: -={ Challenge }=- SubStr for Lists
« Reply #15 on: October 01, 2010, 05:38:49 PM »
*lmao* I didn't expect to be the fastest but i also didn't expect to be that far down the list either.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #16 on: October 01, 2010, 05:39:48 PM »
Wow.  Didn't think it would be that much slower, my second version.  Thanks for the testing Kerry.

I wanted to remove one of the ' reverse ' calls, so here is another version.  :-D

Code: [Select]
(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)
        )
    )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #17 on: October 01, 2010, 05:47:44 PM »

I wanted to remove one of the ' reverse ' calls, so here is another version.  :-D

Code: [Select]
(defun TW:SubList3 ( lst st len / tempList )
  ..<snip> 
 

About 25% Tim .. same as LM:SUBLIST-ITER
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: -={ Challenge }=- SubStr for Lists
« Reply #18 on: October 01, 2010, 05:56:38 PM »
ok, smth different and slow :)
Code: [Select]
(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
)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #19 on: October 01, 2010, 06:01:49 PM »
Just dissected your func Kerry - nice method my friend  :wink:

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #20 on: October 01, 2010, 06:03:46 PM »
ok, smth different and slow :)

I like it!  :-)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #21 on: October 01, 2010, 06:05:47 PM »

I wanted to remove one of the ' reverse ' calls, so here is another version.  :-D

Code: [Select]
(defun TW:SubList3 ( lst st len / tempList )
  ..<snip> 
 

About 25% Tim .. same as LM:SUBLIST-ITER

Hmmmm....... that is different than what I thought would happen.  Thanks again Kerry for running it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #22 on: October 01, 2010, 06:16:54 PM »
Another perhaps:

Code: [Select]
(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)
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #23 on: October 01, 2010, 06:23:06 PM »




Benchmarking [M.P. 2005 < revised kdub 2005>] ..................
Elapsed milliseconds for 32768 iteration(s)/ relative Timing :

    (TW:L-SUBLIST TESTLIST 9 13)........2871 / 2.4538 <slowest>
    (J7:SUBLIST TESTLIST 9 13)..........2028 / 1.7333
    (EE:F TESTLIST 9 13)................1779 / 1.5205
    (GILE:SUBLST TESTLIST 9 13).........1716 / 1.4667
    (LM:SUBLIST TESTLIST 9 13)..........1638 / 1.4000
    (LM:SUBLIST-ITER TESTLIST 9 13).....1498 / 1.2803
    (TW:SUBLIST3 TESTLIST 9 13).........1498 / 1.2803
    (VK_GETSUBLIST51 TESTLIST 9 13).....1482 / 1.2667
    (EE:F1 TESTLIST 9 13)...............1451 / 1.2402
    (LM:SUBLIST3 TESTLIST 9 13).........1450 / 1.2393
    (EE:F2 TESTLIST 9 13)...............1342 / 1.1470
    (AT:SUBLIST TESTLIST 9 13)..........1342 / 1.1470
    (GILE:SUBLIST TESTLIST 9 13)........1341 / 1.1462
    (TW:SUBLIST TESTLIST 9 13)..........1310 / 1.1197
    (KDUB:SUBLIST TESTLIST 9 13)........1170 / 1.0000 <fastest>

 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #24 on: October 01, 2010, 06:28:26 PM »
Thanks Kerry, I didn't think it would be too near the top with those recursive calls...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #25 on: October 01, 2010, 08:00:35 PM »
Just dissected your func Kerry - nice method my friend  :wink:

I have a feeling that the routine originated from gile.
I try to credit code but that particular function doesn't have an accreditation ...
If anyone recognises it, please stand up :-D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #26 on: October 02, 2010, 12:39:09 AM »
Thanks Kerry, as the change results in long lists?
Code: [Select]
(setq i 0)
(repeat 10000
 (setq l (cons i l)
       i (1+ i)
 )
)
(test l 4000 8000)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #27 on: October 02, 2010, 04:57:13 AM »

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 :

    (GILE:SUBLIST TESTLIST 4000 4000)........6599 / 70.2021 <slowest>
    (KDUB:SUBLIST TESTLIST 4000 4000)........4633 / 49.2872
    (J7:SUBLIST TESTLIST 4000 4000)..........1248 / 13.2766
    (EE:F TESTLIST 4000 4000).................312 / 3.3191
    (GILE:SUBLST TESTLIST 4000 4000)..........312 / 3.3191
    (TW:L-SUBLIST TESTLIST 4000 4000).........297 / 3.1596
    (LM:SUBLIST TESTLIST 4000 4000)...........266 / 2.8298
    (LM:SUBLIST3 TESTLIST 4000 4000)..........202 / 2.1489
    (LM:SUBLIST-ITER TESTLIST 4000 4000)......187 / 1.9894
    (TW:SUBLIST3 TESTLIST 4000 4000)..........187 / 1.9894
    (EE:F1 TESTLIST 4000 4000)................156 / 1.6596
    (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 :

    (GILE:SUBLIST TESTLIST 20 9980)........13478 / 123.6514 <slowest>
    (KDUB:SUBLIST TESTLIST 20 9980).........9890 / 90.7339
    (J7:SUBLIST TESTLIST 20 9980)...........5928 / 54.3853
    (EE:F TESTLIST 20 9980)..................515 / 4.7248
    (LM:SUBLIST TESTLIST 20 9980)............437 / 4.0092
    (TW:L-SUBLIST TESTLIST 20 9980)..........437 / 4.0092
    (GILE:SUBLST TESTLIST 20 9980)...........436 / 4.0000
    (LM:SUBLIST-ITER TESTLIST 20 9980).......374 / 3.4312
    (TW:SUBLIST3 TESTLIST 20 9980)...........343 / 3.1468
    (EE:F2 TESTLIST 20 9980).................218 / 2.0000
    (EE:F1 TESTLIST 20 9980).................202 / 1.8532
    (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>

 
« Last Edit: October 02, 2010, 05:24:35 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: -={ Challenge }=- SubStr for Lists
« Reply #28 on: October 02, 2010, 06:33:17 AM »
now i must edit my post and remove the "slow" adjective :)

David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- SubStr for Lists
« Reply #29 on: October 02, 2010, 07:31:20 AM »
Something simple ( like me )
Code: [Select]
(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
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- SubStr for Lists
« Reply #30 on: October 03, 2010, 11:54:16 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- SubStr for Lists
« Reply #31 on: October 04, 2010, 12:04:45 AM »
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))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #32 on: October 04, 2010, 05:25:15 AM »
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))

Wow - that takes me back  :lol:

David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- SubStr for Lists
« Reply #33 on: October 04, 2010, 07:37:55 AM »
CAB! Good Memory!
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))

I actually found that 1 in one of my library macros files when I was replying here.  It just didn't quite meet the OP needs.  -David

R12 Dos - A2K

chlh_jd

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #34 on: February 25, 2011, 09:14:27 AM »
Hi All  :-)
here's faster one , it's main frame like TW:sublist , just use while loops replace repeat .
Code: [Select]
(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)
_$
函数:TW:SUBLIST运行100000次测试结果00:00:02:219函数:SS-SUBLIST运行100000次测试结果00:00:01:796when the list len=10000, (ss-sublist testlist 9 13)
_$
函数:TW:SUBLIST运行10次测试结果00:00:03:688函数:SS-SUBLIST运行10次测试结果00:00:03:671
the testlist producted by this fun
Quote
Code: [Select]
(setq i 0
      TestList '()
)
(repeat 10000
  (setq TestList (cons i TestList)
        i        (1+ i)
  )
)
my test way
Code: [Select]
(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]
« Last Edit: February 25, 2011, 09:22:00 AM by chlh_jd »

chlh_jd

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #35 on: February 25, 2011, 09:22:57 AM »
it's wrong test for me , sorry to all .
 :-(

chlh_jd

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #36 on: October 03, 2011, 04:33:46 AM »
this one will fasterSee
Something simple ( like me )
Code: [Select]
(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
Code: [Select]
(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)
)
« Last Edit: October 03, 2011, 04:57:07 AM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #37 on: October 03, 2011, 08:11:10 AM »
Code: [Select]
_$ (sublist '(0 1 2 3 4 5) 2 nil)
; error: bad argument type: numberp: nil

 :?



kraz

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #38 on: October 03, 2011, 10:13:51 PM »
Wow~ It's very nice Kerry's BenchMarking tool  :-)
Here is my poor code. it will be very slow... :-(
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun KZ:SubList(lst stnum len / nlst c )
;----------------------------------------------------------------------------------------
;(KZ:SubList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) 5 -3)
;(6 5 4)
;(KZ:SubList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) 5 3)
;(6 7 8)
;----------------------------------------------------------------------------------------
  (setq nlst '() c 0)
  (if (> stnum -1)
    (progn
    (if (not len) (setq len (length lst)))
    (while (and (> (abs len) c) (< stnum (length lst)))
      (setq nlst (append nlst (list (nth stnum lst))))
      (if (> len 0)
         (setq stnum (1+ stnum))
         (setq stnum (1- stnum))
      )
      (if (>= stnum 0)
         (setq c (1+ c))
         (setq c (+ c (abs len)))
      )
    );while
   )
  );if
  nlst
)

chlh_jd

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #39 on: October 04, 2011, 01:59:03 AM »
Code: [Select]
_$ (sublist '(0 1 2 3 4 5) 2 nil)
; error: bad argument type: numberp: nil
:?
Hi , Lee
This will do
Code: [Select]
(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>
 


Did this need add the case (funname Testlist 20 5000) to Test ?

chlh_jd

  • Guest
Re: -={ Challenge }=- SubStr for Lists
« Reply #40 on: October 04, 2011, 02:14:12 AM »
in the following long list perhaps change the result ?
Code: [Select]
(setq i 1
      TestList '()
)
(repeat 10000 
  (setq TestList (cons (list i (1+ i) (+ i 2)) TestList)
        i        (+ i 3)
  )
)
Test fun
Code: [Select]
(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

Consumed time...............ms / rv ................ repeat times 100000...:

 GC:SUBLIST...............1046 / 1.77......<Fastest>
 LM:SUBLIST...............1061 / 1.75......
 EE:F..........................1077 / 1.72......
 GC:SUBLST................1107 / 1.68......
 TW:SUBLIST..............1216 / 1.53......
 AT:SUBLIST...............1232 / 1.51......
 TW:SUBLIST3.............1233 / 1.51......
 EE:F2........................1248 / 1.49......
 LM:SUBLIST3.............1310 / 1.42......
 EE:F1........................1544 / 1.20......
 VK_GETSUBLIST51......1669 / 1.11......
 TW:SUBLIST2.............1856 / 1.00......<Slowest>

Code: [Select]
(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...:
 GC:SUBLIST..............1045 / 1.79......<Fastest>
 LM:SUBLIST..............1061 / 1.76......
 EE:F.........................1092 / 1.71......
 GC:SUBLST...............1092 / 1.71......
 SS:SUBLIST..............1217 / 1.54......
 AT:SUBLIST..............1232 / 1.52......
 TW:SUBLIST3............1233 / 1.52......
 TW:SUBLIST..............1233 / 1.52......
 EE:F2........................1264 / 1.48......
 LM:SUBLIST3.............1326 / 1.41......
 EE:F1........................1529 / 1.22......
 VK_GETSUBLIST51......1670 / 1.12......
 TW:SUBLIST2.............1872 / 1.00......<Slowest>

Code: [Select]
(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...:
 GC:SUBLIST...............1045 / 1.81......<Fastest>
 LM:SUBLIST...............1061 / 1.78......
 EE:F..........................1076 / 1.75......
 GC:SUBLST................1093 / 1.73......
 SS:SUBLIST................1201 / 1.57......
 TW:SUBLIST3.............1233 / 1.53......
 TW:SUBLIST...............1233 / 1.53......
 AT:SUBLIST................1242 / 1.52......
 EE:F2.........................1248 / 1.51......
 LM:SUBLIST3..............1326 / 1.42......
 EE:F1.........................1544 / 1.22......
 VK_GETSUBLIST51......1669 / 1.13......
 TW:SUBLIST2.............1887 / 1.00......<Slowest>
« Last Edit: October 04, 2011, 02:58:29 AM by chlh_jd »