diag=: (= {"1 ' '&,.)&.|.
reflect=: |:@(, }.@|.)^:2
diamond=: (reflect @ diag) f.

NB. Starting from these expressions, I generalized the capability to accept
any type 
NB. of argument on the right and an optional filler on the left.

NB. It may not be the most elegant nor the most compact but it is general,
fast 
NB. and completely tacit.
NB. By the way, this message can be treated as a J script.

DiagMona=: ($...@] $ ,@] #^:_1 [) =/~...@i.@#
DiagDyad=: -...@i.@#...@] |."0 1 (],"0 1(<:@#&]&# # [))
Reflect=: (] , |....@}:)@:((|. , }.)"1)
Diamond=: (Reflect @ DiagMona : ([: Reflect DiagDyad)) f.

ts=: 6!:2

NB.       Diamond NB. True tacit
NB.     (] , |....@}:)@:((|. , }.)"1)@(($...@] $ ,@] #^:_1 [) =/~...@i.@#) :([: 
(] ,
|....@}:)@:((|. , }.)"1) -...@i.@#...@] |."0 1 ] ,"0 1 <:@#&]&# # [)

NB.       (diamond a.)-: Diamond a.
NB.     1

NB.       (diamond a.)-: ' 'Diamond a.
NB.     1

NB.       10 ts'diamond a.'
NB.     0.00493828

NB.       10 ts'Diamond a.'
NB.     0.00180445

NB.       10 ts'''.''Diamond a.'
NB.     0.00173338

NB.        Diamond'ABC'
NB.       A  
NB.      B B 
NB.     C   C
NB.      B B 
NB.       A  
   
NB.        '.'Diamond'ABA'
NB.     ..A..
NB.     .B.B.
NB.     A...A
NB.     .B.B.
NB.     ..A..
   
NB.        Diamond 1 2 3
NB.     0 0 1 0 0
NB.     0 2 0 2 0
NB.     3 0 0 0 3
NB.     0 2 0 2 0
NB.     0 0 1 0 0
   
NB.        _ Diamond 1 2 3
NB.     _ _ 1 _ _
NB.     _ 2 _ 2 _
NB.     3 _ _ _ 3
NB.     _ 2 _ 2 _
NB.     _ _ 1 _ _
   
NB.        Diamond ;:'ab cd ef'
NB.     +--+--+--+--+--+
NB.     |  |  |ab|  |  |
NB.     +--+--+--+--+--+
NB.     |  |cd|  |cd|  |
NB.     +--+--+--+--+--+
NB.     |ef|  |  |  |ef|
NB.     +--+--+--+--+--+
NB.     |  |cd|  |cd|  |
NB.     +--+--+--+--+--+
NB.     |  |  |ab|  |  |
NB.     +--+--+--+--+--+
   
NB.        (<'..')Diamond ;:'ab cd ef'
NB.     +--+--+--+--+--+
NB.     |..|..|ab|..|..|
NB.     +--+--+--+--+--+
NB.     |..|cd|..|cd|..|
NB.     +--+--+--+--+--+
NB.     |ef|..|..|..|ef|
NB.     +--+--+--+--+--+
NB.     |..|cd|..|cd|..|
NB.     +--+--+--+--+--+
NB.     |..|..|ab|..|..|
NB.     +--+--+--+--+--+

NB. Try this

1!:2&2 Diamond 'J';'APL';<(<'Bla...')Diamond (<"0 i. 2 2);(<"0 i. 3 3);<(<"0
i. 4 4)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to