This function seems generally useful, or at least is convenient for me.  Its 
also an easy way to "decorate" any function to automagically deal with nil 
arguments in what I think is the best generic way.  It makes sense to keep this 
as user code, because the "right" function is personal or situational anyway.

NB. adverb treats u as dyad. If either argument is nil, it returns the other.  
If both are
NB. nil, it returns the identity function value, or nil if not defined.

smartnils =: 1 : 0 NB. assumes all 0 element arguments (0=#y) are equivalent to 
nil.
if. 0=#y do. (u/ :: ]) i.0 return. end.
y
:
if. 0=#x do. 
    if. 0=#y do. (u/ :: ]) i.0 return. end.
    y return. end.
if. 0=#y do. x return. end.
x u y
)

   (1 ; '' ; 2  ; '')  ( [ each + smartnils each ]) 2 ;'' ; 4 ; 5
+-+-+-+-+
|3|0|6|5|
+-+-+-+-+
   (1 ; '' ; 2  ; '')  ( [ each +  each ]) 2 ;'' ; 4 ; 5
+-++-++
|3||6||
+-++-++
  (1 ; '' ; 2  ; '')  ( [ each +:@* smartnils each ]) 2 ;'' ; 4 ; 5
+-++--+-+
|4||16|5| NB. +:@*/ i.0 is undefined.
+-++--+-+
   +: each (1 ; '' ; 2  ; '')  ( [ each * smartnils each ]) 2 ;'' ; 4 ; 5
+-+-+--+--+
|4|2|16|10|
+-+-+--+--+

The one downside of this definition for smartnils is that 1 -: (i.0) *. i.0
but its a good template for tweaking variations that give a better answer.

----- Original Message ----
From: Pascal Jasmin <[EMAIL PROTECTED]>
To: Programming forum <[email protected]>
Sent: Sunday, October 22, 2006 5:17:55 PM
Subject: Re: [Jprogramming] Dyads and identity function

The ident adverb I was looking for would be like:

ident2 =: 1 : 0 NB. assumes all 0 element arguments (0=#y)return identity 
behaviour
y
:
if. 0=#x do. y return. end.
if. 0=#y do. x return. end.
x u y
)

ident3 =: 1 : 0 NB. assumes only i.0 arguments causes identity behaviour

y

:

if. x-:i.0 do. y return. end.

if. y-:i.0 do. x return. end.

x u y

)

    +ident2 33
33
    (i.0) +ident2 33
33
   ((i.0),ident2 22) +ident2 i.0
22
   $ 22 +ident3 i.0 0
0 0

incidentally, there is a bug in the documentation for identity function, 
because it says that it should behave like ident2, but it really behaves like 
ident3.





----- Original Message ----
From: "Miller, Raul D" <[EMAIL PROTECTED]>
To: Programming forum <[email protected]>; Programming forum 
<[email protected]>
Sent: Sunday, October 22, 2006 3:09:30 PM
Subject: RE: [Jprogramming] Dyads and identity function

   ident=:1 :'a:1 :(u b.1)'



For example:

   +ident 99

0

   +ident i. 9 9

0 0 0 0 0 0 0 0 0



-- 

Raul






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




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





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

Reply via email to