Henry Rich:
> I often have boxed data like
> 'a';'bc';'def'
> that I want to run together with delimiters, for example
> a-bc-def
>
> I admit this is not such a challenge; my best solution is
> }:@;@:(,&'-'&.>) 'a';'bc';'def'
> But I don't like the way it looks; [...]
I approach this from bottom up, starting out with simple primitives
doing simple things -- but nicely. In this case, the basic primitive
is "concatenate using a delimiter". The key is to find an appropriate
syntactic form for this. To concatenate two things, I feel a dyadic
function would by natural. To have this dyad with a flexible delimiter,
I use an adnoun deriving the dyad from its delimiter parameter:
dcat =. 1 : 0
NB. no monad needed
:
x. , m. , y.
)
'foo' '*'dcat 'bar'
foo*bar
1 (99 dcat) 2
1 99 2
It may appear silly to have a utility for such a basic operation,
but it is flexible and the flexibility scales nicely into larger
expressions:
99 dcat/ 1 2 3 4 NB. reduce a list
1 99 2 99 3 99 4
'-'dcat &": / 6!:0'' NB. with number->string conversion
2007-6-17-22-32-0
'-*-'dcat &:> / 'a';'bc';'def' NB. with unboxing
a-*-bc-*-def
I must admit that my first "dcat" version was a bloated do-it-all one,
namely
dcat2 =. 1 : (':'; '(":>x.) , m. , ":>y.')
'-'dcat2/ 6!:0''
2007-6-17-22-50-36
'-'dcat2/ 'a';'bc';'def'
a-bc-def
In retrorespect, I probably prefer the "more primitive" dcat. It doesn't
mess with types and is easy to adapt when needed.
Martin
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm