Author Topic: Count Items in a list  (Read 8037 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #30 on: December 05, 2016, 08:57:28 AM »
Of all the weird ways to say "that flavor has already been written" that was the most recent I've read.

I don't recall seeing Stefan's submission before -- great minds think alike?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #31 on: December 05, 2016, 09:18:42 AM »
Of all the weird ways to say "that flavor has already been written" that was the most recent I've read.

I don't recall seeing Stefan's submission before -- great minds think alike?
https://www.theswamp.org/index.php?topic=50539.msg556727#msg556727
 :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #32 on: December 05, 2016, 09:42:06 AM »
Cool beans, thanks Marc.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Count Items in a list
« Reply #33 on: December 05, 2016, 06:06:37 PM »
Nice thread, thanks for bumping it up. Now I've remembered that gotta figure out such counter for an association lists.
Code: [Select]
(list
'("A" "B" 10)
'("A" "C" 20)
'("B" "C" 10)
'("A" "B" 10)
'("A" "C" 20)
'("A" "B" 10)
)
-->
(list
'(("A" "B" 10) . 3)
'(("A" "C" 20) . 2)
'(("B" "C" 10) . 1)
)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #34 on: December 05, 2016, 06:21:13 PM »
It's been done. I'd search for you but I'm sick as a dawg right now.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Count Items in a list
« Reply #35 on: December 05, 2016, 06:34:12 PM »
It's been done. I'd search for you but I'm sick as a dawg right now.
Thanks for comfirming, I feel relatively new for theswamp so I have no idea whats below the surface, expect big fishes.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #36 on: December 05, 2016, 06:51:18 PM »
Given a couple quick and dirty functions (not hardly optimized) ...

Code: [Select]
(defun _Positions ( x lst / p )
    ;;  find all the positions of x in lst
    ;;  (_Positions 1 '(0 0 1 0 0 1)) >> (2 5)
    (if (setq p (vl-position x lst))
        (   (lambda ( lst result )
                (while (setq p (vl-position x lst))
                    (setq
                        result (cons (+ 1 p (car result)) result)
                        lst    (cdr (member x lst))
                    )
                )
                (reverse result)
            )
            (cdr (member x lst))
            (list p)
        )
    )   
)

Code: [Select]
(defun _Tally ( x lst )
    ;;  count all the occurances of x in lst
    ;;  (_Tally 1 '(0 0 1 0 0 1)) >> 2
    (length (_Positions x lst))
)

Code: [Select]
(defun _Distil ( lst / result )
    (while lst
        (setq
            result (cons (car lst) result)
            lst    (vl-remove (car result) (cdr lst))
        )
    )
    (reverse result)       
)

Code: [Select]
(defun _TallyHo ( lst )
    (mapcar
        (function (lambda (x) (cons x (_Tally x lst))))
        (_Distil lst)
    )
)

Then ...

(_TallyHo
   '(
       ("A" "B" 10)
       ("A" "C" 20)
       ("B" "C" 10)
       ("A" "B" 10)
       ("A" "C" 20)
       ("A" "B" 10)
    )
)


>>

(
   (("A" "B" 10) . 3)
   (("A" "C" 20) . 2)
   (("B" "C" 10) . 1)
)


Cheers (now where'd I'd leave that big bowl ...).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #37 on: December 06, 2016, 03:06:05 AM »
Cool beans, thanks Marc.
If we can go on demential:
Code: [Select]
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Length   : 160000    Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
    (ALE_POSITIONSD 1 ALIST).....1828 / 2.56 <fastest>
    (ALE_POSITIONSD 1 ALIST).....1844 / 2.53
    (MP_POSITIONS 1 ALIST).......4625 / 1.01
    (MP_POSITIONS 1 ALIST).......4672 / 1 <slowest>
Code: [Select]
; 20161206
(defun ALE_PositionSd (i l / n r c) ; ALE_PositionS(demential)
  (setq n -1)
  (while (setq c (vl-position i l))
    (setq l (cdr (ALE_List_NthCdr c l))  r (cons (setq n (+ 1 c n)) r))
  )
  (reverse r)
)
ALE_List_NthCdr  see: https://www.theswamp.org/index.php?topic=46419.msg514475#msg514475

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Count Items in a list
« Reply #38 on: December 06, 2016, 04:15:52 AM »
Given a couple quick and dirty functions (not hardly optimized) ...
<...>
Cheers (now where'd I'd leave that big bowl ...).
This is a good start, thanks alot MP!  :yay!:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #39 on: December 06, 2016, 10:55:24 PM »
You're very welcome, thanks for the thanks, cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst