# function define

**URL:** <https://forum.kx.com/t/function-define/11263>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [September 30, 2016, 4:03pm UTC](https://forum.kx.com/t/function-define/11263 "2016-09-30T16:03:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rex\_gm\_yu](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@rex\_gm\_yu](https://forum.kx.com/u/rex_gm_yu)\
**Post date:** [September 30, 2016, 4:03pm UTC](https://forum.kx.com/t/function-define/11263/1 "2016-09-30T16:03:00Z")

</div>

s:`a`b`c`c`a`d`k&nbsp;

myfunction s

result:1 2 3 3 3 4 5

s &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums

* * *

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

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

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3

`a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3

`d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4

`k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 5

how to define myfunction?

thks

Rex Yu

\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*

---

<div class="post-metadata">

**Author:** ![JFSawyer88](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JFSawyer88](https://forum.kx.com/u/JFSawyer88)\
**Post date:** [September 30, 2016, 4:36pm UTC](https://forum.kx.com/t/function-define/11263/2 "2016-09-30T16:36:00Z")

</div>

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@,)}

---

<div class="post-metadata">

**Author:** ![sohagan](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@sohagan](https://forum.kx.com/u/sohagan)\
**Post date:** [September 30, 2016, 4:43pm UTC](https://forum.kx.com/t/function-define/11263/3 "2016-09-30T16:43:00Z")

</div>

one way..s:`a`b`c`c`a`d`k([]s),'exec flip `sl`sc!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:`a`b`c`c`a`d`kmyfunction sresult:1 2 3 3 3 4 5s s list s count sums-----------------------------------------------------`a `a 1`b `a`b 2`c `a`b`c 3`c `a`b`c 3`a `a`b`c 3`d `a`b`c`d 4`k `a`b`c`d`k 5how to define myfunction?thksRex Yu\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*– Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [September 30, 2016, 5:11pm UTC](https://forum.kx.com/t/function-define/11263/4 "2016-09-30T17:11:00Z")

</div>

q) {flip`s`sl`sc!enlist[x;m#\:u;]m:1+maxs(u:`u#distinct x)?x}`a`b`c`c`a`d`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) [[mailto:personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)] **Im Auftrag von** [rex.gm.yu@gmail.com](mailto: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:`a`b`c`c`a`d`k&nbsp;\<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 &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums\<o:p\>\</o:p\>

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

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

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

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4\<o:p\>\</o:p\>

`k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 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

---

<div class="post-metadata">

**Author:** ![mfbx9mk8](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mfbx9mk8](https://forum.kx.com/u/mfbx9mk8)\
**Post date:** [September 30, 2016, 6:20pm UTC](https://forum.kx.com/t/function-define/11263/5 "2016-09-30T18:20:00Z")

</div>

Another one;

q)flip `s`sl`scs!flip{(last;::;count)@\:/:distinct each(1+til count x)#\:x}`a`b`c`c`a`d`k

s sl &nbsp; &nbsp; &nbsp; &nbsp; scs

* * *

a ,`a &nbsp; &nbsp; &nbsp; &nbsp;1

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

c `a`b`c &nbsp; &nbsp; 3

c `a`b`c &nbsp; &nbsp; 3

c `a`b`c &nbsp; &nbsp; 3

d `a`b`c`d &nbsp; 4

k `a`b`c`d`k 5

---

<div class="post-metadata">

**Author:** ![TerryLynch1](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@TerryLynch1](https://forum.kx.com/u/TerryLynch1)\
**Post date:** [September 30, 2016, 6:34pm UTC](https://forum.kx.com/t/function-define/11263/6 "2016-09-30T18:34:00Z")

</div>

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

s s2

* * *

a `a

b `a`b

c `a`b`c

c `a`b`c

a `a`b`c

d `a`b`c`d

k `a`b`c`d`k

then count each.

Terry

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [September 30, 2016, 10:05pm UTC](https://forum.kx.com/t/function-define/11263/7 "2016-09-30T22:05:00Z")

</div>

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

\<o:p\> \</o:p\>

s:10000?`a`b`c`c`a`d`k\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

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

\t:10 {s,‘exec flip `sl`sc!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 {flip`s`sl`sc!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 `s`sl`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) [[mailto: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 `a`b\<o:p\>\</o:p\>

c `a`b`c\<o:p\>\</o:p\>

c `a`b`c\<o:p\>\</o:p\>

a `a`b`c\<o:p\>\</o:p\>

d `a`b`c`d\<o:p\>\</o:p\>

k `a`b`c`d`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:`a`b`c`c`a`d`k&nbsp;\<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 &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums\<o:p\>\</o:p\>

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

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

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

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>

`d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4\<o:p\>\</o:p\>

`k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 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

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [September 30, 2016, 10:52pm UTC](https://forum.kx.com/t/function-define/11263/8 "2016-09-30T22:52:00Z")

</div>

nice ideas

q)\ts:1000 {flip`s`sl`sc!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 {flip`s`sl`sc!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 {flip`s`sl`sc!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,

&nbsp; &nbsp;Attila

>

---

<div class="post-metadata">

**Author:** ![rex\_gm\_yu](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@rex\_gm\_yu](https://forum.kx.com/u/rex_gm_yu)\
**Post date:** [October 1, 2016, 3:34am UTC](https://forum.kx.com/t/function-define/11263/9 "2016-10-01T03:34:00Z")

</div>

Thanks for everybody’s solution!

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

> s:`a`b`c`c`a`d`k&nbsp;
> 
> myfunction s
> 
> result:1 2 3 3 3 4 5
> 
> s &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums
> 
> -----------------------------------------------------
> 
> `a &nbsp; &nbsp; &nbsp; `a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1
> 
> `b &nbsp; &nbsp; &nbsp; `a`b &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4
> 
> `k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 5
> 
> how to define myfunction?
> 
> thks
> 
> Rex Yu
> 
> \*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [October 1, 2016, 10:12am UTC](https://forum.kx.com/t/function-define/11263/10 "2016-10-01T10:12:00Z")

</div>

/ 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) [[mailto: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 {flip`s`sl`sc!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 {flip`s`sl`sc!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 {flip`s`sl`sc!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\>

&nbsp; &nbsp;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\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> s:10000?`a`b`c`c`a`d`k\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> / this is the worst case\<o:p\>\</o:p\>
> 
> \t:10 {s,‘exec flip `sl`sc!flip {(foo;zoo:count foo:distinct x til y)}[s]’[1+i] from (s)} s\<o:p\>\</o:p\>
> 
> 2195\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> \t:10 {flip`s`sl`sc!enlist[x;m#\:u;]m:1+maxs(u:`u#distinct x)?x}s\<o:p\>\</o:p\>
> 
> 6\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> \t:10 flip `s`sl`scs!flip{(last;::;count)@:/:distinct each(1+til count x)#:x}s\<o:p\>\</o:p\>
> 
> 981\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> \t:10 {(s:x;s2:(distinct@,))}s\<o:p\>\</o:p\>
> 
> 41\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> \t:10 {[s]update s2:union[s] from (s)}s\<o:p\>\</o:p\>
> 
> 33\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> Kim\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> **Von:** &nbsp;personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com]&nbsp; **Im Auftrag von&nbsp;** Terry Lynch  
> **Gesendet:** &nbsp;Freitag, 30. September 2016 19:34  
> **An:** &nbsp;Kdb+ Personal Developers  
> **Betreff:** &nbsp;Re: [personal kdb+] function define\<o:p\>\</o:p\>
> 
> &nbsp;\<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 `a`b\<o:p\>\</o:p\>
> 
> c `a`b`c\<o:p\>\</o:p\>
> 
> c `a`b`c\<o:p\>\</o:p\>
> 
> a `a`b`c\<o:p\>\</o:p\>
> 
> d `a`b`c`d\<o:p\>\</o:p\>
> 
> k `a`b`c`d`k\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> then count each.\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> Terry\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> &nbsp;\<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&nbsp;s&nbsp;you can do\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> (distinct@,)[s]\<o:p\>\</o:p\>
> 
> &nbsp;\<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\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> f:{count each (distinct@,)}\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> On Fri, Sep 30, 2016 at 11:03 AM, \<rex.gm.yu@gmail.com\> wrote:\<o:p\>\</o:p\>
> 
> s:`a`b`c`c`a`d`k&nbsp;\<o:p\>\</o:p\>
> 
> myfunction s\<o:p\>\</o:p\>
> 
> result:1 2 3 3 3 4 5\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> s &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums\<o:p\>\</o:p\>
> 
> -----------------------------------------------------\<o:p\>\</o:p\>
> 
> `a &nbsp; &nbsp; &nbsp; `a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1\<o:p\>\</o:p\>
> 
> `b &nbsp; &nbsp; &nbsp; `a`b &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2\<o:p\>\</o:p\>
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>
> 
> `a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3\<o:p\>\</o:p\>
> 
> `d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4\<o:p\>\</o:p\>
> 
> `k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 5\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> how to define myfunction?\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> thks\<o:p\>\</o:p\>
> 
> &nbsp;\<o:p\>\</o:p\>
> 
> Rex Yu\<o:p\>\</o:p\>
> 
> \*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\<o:p\>\</o:p\>
> 
> –&nbsp;  
> Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![rex\_gm\_yu](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@rex\_gm\_yu](https://forum.kx.com/u/rex_gm_yu)\
**Post date:** [October 1, 2016, 2:22pm UTC](https://forum.kx.com/t/function-define/11263/11 "2016-10-01T14:22:00Z")

</div>

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

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

s:`a`b`c`c`a`d`k

tb1:(s)

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

select s,scount:sumscount s from tb1

s &nbsp; &nbsp; &nbsp; 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:`a`b`c`c`a`d`k&nbsp;
> 
> myfunction s
> 
> result:1 2 3 3 3 4 5
> 
> s &nbsp; &nbsp; &nbsp; &nbsp; s list &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s count sums
> 
> -----------------------------------------------------
> 
> `a &nbsp; &nbsp; &nbsp; `a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1
> 
> `b &nbsp; &nbsp; &nbsp; `a`b &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `c &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `a &nbsp; &nbsp; &nbsp; `a`b`c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3
> 
> `d &nbsp; &nbsp; &nbsp; `a`b`c`d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4
> 
> `k &nbsp; &nbsp; &nbsp; `a`b`c`d`k &nbsp; &nbsp; &nbsp; 5
> 
> how to define myfunction?
> 
> thks
> 
> Rex Yu
> 
> \*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*\_\*
