function define

s:abccad`k 

myfunction s

result:1 2 3 3 3 4 5

s         s list              s count sums


a       a                   1

b       a`b                2

c       abc             3

c       abc             3

a       abc             3

d       abc`d          4

k       abcdk       5

how to define myfunction?

thks

Rex Yu

*_*_*_*_*_*_*_*_*_*_*_*

For a running list of distinct values in your list s you can do

(distinct@,)[s]

and then you can count each element of the output list to get the result of the function you want. The full function can be written as

f:{count each (distinct@,)}



one way..s:abccadk([]s),'exec flip slsc!flip {(foo;zoo:count foo:distinct x til y)}[s]'[1+i] from ([]s)just place the above in a function paramaterized as you like..HTH,Sean ________________________________ From: personal-kdbplus@googlegroups.com [personal-kdbplus@googlegroups.com] on behalf of rex.gm.yu@gmail.com [rex.gm.yu@gmail.com]Sent: 30 September 2016 16:03To: Kdb+ Personal DevelopersSubject: [personal kdb+] function defines:abccadkmyfunction sresult:1 2 3 3 3 4 5s s list s count sums-----------------------------------------------------a a 1b ab 2c abc 3c abc 3a abc 3d abcd 4k abcdk 5how to define myfunction?thksRex Yu*_*_*_*_*_*_*_*_*_*_*_*– Submitted via Google Groups

q) {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}abccad`k<o:p></o:p>

<o:p> </o:p>

HTH,<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von rex.gm.yu@gmail.com
Gesendet: Freitag, 30. September 2016 17:03
An: Kdb+ Personal Developers
Betreff: [personal kdb+] function define<o:p></o:p>

<o:p> </o:p>

s:abccad`k <o:p></o:p>

myfunction s<o:p></o:p>

result:1 2 3 3 3 4 5<o:p></o:p>

<o:p> </o:p>

s         s list              s count sums<o:p></o:p>

-----------------------------------------------------<o:p></o:p>

a &nbsp; &nbsp; &nbsp; a                   1<o:p></o:p>

b &nbsp; &nbsp; &nbsp; a`b                2<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

a &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

d &nbsp; &nbsp; &nbsp; abc`d          4<o:p></o:p>

k &nbsp; &nbsp; &nbsp; abcdk       5<o:p></o:p>

<o:p> </o:p>

how to define myfunction?<o:p></o:p>

<o:p> </o:p>

thks<o:p></o:p>

<o:p> </o:p>

Rex Yu<o:p></o:p>

*_*_*_*_*_*_*_*_*_*_*_*<o:p></o:p>


Submitted via Google Groups

Another one;

q)flip sslscs!flip{(last;::;count)@\:/:distinct each(1+til count x)#\:x}abccadk

s sl         scs


a ,`a        1

b ab       2

c ab`c     3

c ab`c     3

c ab`c     3

d abcd   4

k abcd`k 5

q)update s2:union[s] from (s)

s s2


a `a

b ab

c ab`c

c ab`c

a ab`c

d abcd

k abcd`k

then count each.

Terry

If you are interested in performance here is the result:<o:p></o:p>

<o:p> </o:p>

s:10000?abccad`k<o:p></o:p>

<o:p> </o:p>

/ this is the worst case<o:p></o:p>

\t:10 {s,‘exec flip slsc!flip {(foo;zoo:count foo:distinct x til y)}[s]’[1+i] from (s)} s<o:p></o:p>

2195<o:p></o:p>

<o:p> </o:p>

\t:10 {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s<o:p></o:p>

6<o:p></o:p>

<o:p> </o:p>

\t:10 flip ssl`scs!flip{(last;::;count)@:/:distinct each(1+til count x)#:x}s<o:p></o:p>

981<o:p></o:p>

<o:p> </o:p>

\t:10 {(s:x;s2:(distinct@,))}s<o:p></o:p>

41<o:p></o:p>

<o:p> </o:p>

\t:10 {[s]update s2:union[s] from (s)}s<o:p></o:p>

33<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Terry Lynch
Gesendet: Freitag, 30. September 2016 19:34
An: Kdb+ Personal Developers
Betreff: Re: [personal kdb+] function define<o:p></o:p>

<o:p> </o:p>

q)update s2:union[s] from (s)<o:p></o:p>

s s2<o:p></o:p>

------------<o:p></o:p>

a `a<o:p></o:p>

b ab<o:p></o:p>

c ab`c<o:p></o:p>

c ab`c<o:p></o:p>

a ab`c<o:p></o:p>

d abcd<o:p></o:p>

k abcd`k<o:p></o:p>

<o:p> </o:p>

then count each.<o:p></o:p>

<o:p> </o:p>

Terry<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

On Fri, Sep 30, 2016 at 11:36 AM, Jorge Sawyer <jfsawyer88@gmail.com> wrote:<o:p></o:p>

For a running list of distinct values in your list s you can do<o:p></o:p>

<o:p> </o:p>

(distinct@,)[s]<o:p></o:p>

<o:p> </o:p>

and then you can count each element of the output list to get the result of the function you want. The full function can be written as<o:p></o:p>

<o:p> </o:p>

f:{count each (distinct@,)}<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

On Fri, Sep 30, 2016 at 11:03 AM, <rex.gm.yu@gmail.com> wrote:<o:p></o:p>

s:abccad`k <o:p></o:p>

myfunction s<o:p></o:p>

result:1 2 3 3 3 4 5<o:p></o:p>

<o:p> </o:p>

s         s list              s count sums<o:p></o:p>

-----------------------------------------------------<o:p></o:p>

a &nbsp; &nbsp; &nbsp; a                   1<o:p></o:p>

b &nbsp; &nbsp; &nbsp; a`b                2<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

a &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

d &nbsp; &nbsp; &nbsp; abc`d          4<o:p></o:p>

k &nbsp; &nbsp; &nbsp; abcdk       5<o:p></o:p>

<o:p> </o:p>

how to define myfunction?<o:p></o:p>

<o:p> </o:p>

thks<o:p></o:p>

<o:p> </o:p>

Rex Yu<o:p></o:p>

*_*_*_*_*_*_*_*_*_*_*_*<o:p></o:p>


Submitted via Google Groups

nice ideas

q)\ts:1000 {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s

1545 263664

avoiding the /: altogether seems like a good idea

q)\ts:1000 {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s

302 329504

q)s:10000?-1000?`7

q)\ts {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s

11 34770144

q)\ts {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s

1 3080048

q)s:100000?-10000?`7

q)\ts {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s

wsfull (on 32bit)

q)\ts {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s

60 300920688

Cheers,

   Attila

Thanks for everybody’s solution!

? 2016?9?30??? UTC+8??11:10:04?rex…@gmail.com???

s:abccad`k 

myfunction s

result:1 2 3 3 3 4 5

s         s list              s count sums

-----------------------------------------------------

a &nbsp; &nbsp; &nbsp; a                   1

b &nbsp; &nbsp; &nbsp; a`b                2

c &nbsp; &nbsp; &nbsp; abc             3

c &nbsp; &nbsp; &nbsp; abc             3

a &nbsp; &nbsp; &nbsp; abc             3

d &nbsp; &nbsp; &nbsp; abc`d          4

k &nbsp; &nbsp; &nbsp; abcdk       5

how to define myfunction?

thks

Rex Yu

*_*_*_*_*_*_*_*_*_*_*_*

/ avoiding the /: altogether seems like a good idea<o:p></o:p>

q)\ts {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s<o:p></o:p>

1 3080048<o:p></o:p>

<o:p> </o:p>

Interesting.<o:p></o:p>

<o:p> </o:p>

Normally scan is slow but since the distinct numbers are small <=7 the loop does not slow down the calculation.<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Attila Vrabecz
Gesendet: Freitag, 30. September 2016 23:53
An: [kdb+] [kdb+]
Betreff: Re: [personal kdb+] function define<o:p></o:p>

<o:p> </o:p>

nice ideas<o:p></o:p>

<o:p> </o:p>

q)\ts:1000 {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s<o:p></o:p>

1545 263664<o:p></o:p>

<o:p> </o:p>

avoiding the /: altogether seems like a good idea<o:p></o:p>

q)\ts:1000 {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s<o:p></o:p>

302 329504<o:p></o:p>

<o:p> </o:p>

q)s:10000?-1000?`7<o:p></o:p>

q)\ts {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s<o:p></o:p>

11 34770144<o:p></o:p>

q)\ts {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s<o:p></o:p>

1 3080048<o:p></o:p>

<o:p> </o:p>

q)s:100000?-10000?`7<o:p></o:p>

q)\ts {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s<o:p></o:p>

wsfull (on 32bit)<o:p></o:p>

q)\ts {(x;l:((,)scan d)m;c:1+m:maxs(d:`u#distinct x)?x)}s<o:p></o:p>

60 300920688<o:p></o:p>

<o:p> </o:p>

Cheers,<o:p></o:p>

   Attila<o:p></o:p>

On 30 Sep 2016, at 22:05, Kim Tang <kuentang@vodafone.de> wrote:<o:p></o:p>

<o:p> </o:p>

If you are interested in performance here is the result:<o:p></o:p>

 <o:p></o:p>

s:10000?abccad`k<o:p></o:p>

 <o:p></o:p>

/ this is the worst case<o:p></o:p>

\t:10 {s,‘exec flip slsc!flip {(foo;zoo:count foo:distinct x til y)}[s]’[1+i] from (s)} s<o:p></o:p>

2195<o:p></o:p>

 <o:p></o:p>

\t:10 {flipsslsc!enlist[x;m#\:u;]m:1+maxs(u:u#distinct x)?x}s<o:p></o:p>

6<o:p></o:p>

 <o:p></o:p>

\t:10 flip ssl`scs!flip{(last;::;count)@:/:distinct each(1+til count x)#:x}s<o:p></o:p>

981<o:p></o:p>

 <o:p></o:p>

\t:10 {(s:x;s2:(distinct@,))}s<o:p></o:p>

41<o:p></o:p>

 <o:p></o:p>

\t:10 {[s]update s2:union[s] from (s)}s<o:p></o:p>

33<o:p></o:p>

 <o:p></o:p>

Kim<o:p></o:p>

 <o:p></o:p>

Von:  personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.comIm Auftrag von  Terry Lynch
Gesendet:  Freitag, 30. September 2016 19:34
An:  Kdb+ Personal Developers
Betreff:  Re: [personal kdb+] function define<o:p></o:p>

 <o:p></o:p>

q)update s2:union[s] from (s)<o:p></o:p>

s s2<o:p></o:p>

------------<o:p></o:p>

a `a<o:p></o:p>

b ab<o:p></o:p>

c ab`c<o:p></o:p>

c ab`c<o:p></o:p>

a ab`c<o:p></o:p>

d abcd<o:p></o:p>

k abcd`k<o:p></o:p>

 <o:p></o:p>

then count each.<o:p></o:p>

 <o:p></o:p>

Terry<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

On Fri, Sep 30, 2016 at 11:36 AM, Jorge Sawyer <jfsawyer88@gmail.com> wrote:<o:p></o:p>

For a running list of distinct values in your list s you can do<o:p></o:p>

 <o:p></o:p>

(distinct@,)[s]<o:p></o:p>

 <o:p></o:p>

and then you can count each element of the output list to get the result of the function you want. The full function can be written as<o:p></o:p>

 <o:p></o:p>

f:{count each (distinct@,)}<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

On Fri, Sep 30, 2016 at 11:03 AM, <rex.gm.yu@gmail.com> wrote:<o:p></o:p>

s:abccad`k <o:p></o:p>

myfunction s<o:p></o:p>

result:1 2 3 3 3 4 5<o:p></o:p>

 <o:p></o:p>

s         s list              s count sums<o:p></o:p>

-----------------------------------------------------<o:p></o:p>

a &nbsp; &nbsp; &nbsp; a                   1<o:p></o:p>

b &nbsp; &nbsp; &nbsp; a`b                2<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

c &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

a &nbsp; &nbsp; &nbsp; abc             3<o:p></o:p>

d &nbsp; &nbsp; &nbsp; abc`d          4<o:p></o:p>

k &nbsp; &nbsp; &nbsp; abcdk       5<o:p></o:p>

 <o:p></o:p>

how to define myfunction?<o:p></o:p>

 <o:p></o:p>

thks<o:p></o:p>

 <o:p></o:p>

Rex Yu<o:p></o:p>

*_*_*_*_*_*_*_*_*_*_*_*<o:p></o:p>

– 
Submitted via Google Groups

I use following function,as if it get 30 times perfermance.

In my example,I just want to get last column.

s:abccad`k

tb1:(s)

sumscount:{1+maxs(`u#distinct x)?x}

select s,scount:sumscount s from tb1

s       scount


a1

b2

c3

c3

a3

d4

k5

It’s really incredible!

thks!

Rex

? 2016?9?30??? UTC+8??11:10:04?rex…@gmail.com???

s:abccad`k 

myfunction s

result:1 2 3 3 3 4 5

s         s list              s count sums

-----------------------------------------------------

a &nbsp; &nbsp; &nbsp; a                   1

b &nbsp; &nbsp; &nbsp; a`b                2

c &nbsp; &nbsp; &nbsp; abc             3

c &nbsp; &nbsp; &nbsp; abc             3

a &nbsp; &nbsp; &nbsp; abc             3

d &nbsp; &nbsp; &nbsp; abc`d          4

k &nbsp; &nbsp; &nbsp; abcdk       5

how to define myfunction?

thks

Rex Yu

*_*_*_*_*_*_*_*_*_*_*_*