I find SQL's groupby or Unix's uniq is quite a useful concept.

  gb=. 1 : '[: (u&.>@{. , <)&> (1,~2 ~:/\ u&.>) <;.2 ]'

u is the key verb, which returns keys for groupby.

  ] gb 'aaaabbaacc'
+-+----+
|a|aaaa|
+-+----+
|b|bb  |
+-+----+
|a|aa  |
+-+----+
|c|cc  |
+-+----+
  d=.'May 17';'May 18';'June 5';'July 10';'July 21'
  (>@{.;#@>@{:)"1  >@{.@;: gb d
+----+-+
|May |2|
+----+-+
|June|1|
+----+-+
|July|2|
+----+-+

Maybe groupby as a conjunction would be simpler:

  gbc=. 2 : '[: (u&>@{. ; v)&> (1,~2 ~:/\ u&.>) <;.2 ]'
   (>@{.@;: gbc #) d
+----+-+
|May |2|
+----+-+
|June|1|
+----+-+
|July|2|
+----+-+

I'd appreciate any suggestion for improvement -- better and more
elegant approaches.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to