[Jprogramming] techniques to return a variable number of items from a function

'Pascal Jasmin' via Programming programming at jsoftware.com
Fri Apr 10 14:49:39 UTC 2015


takes 1 item as input, returns 1 or 2 items

f =: (] , *:)^:(2&|)"0

  f 1 2 3 
1 1 
2 0 
3 9 


I found this technique for avoiding fills in repeated calls.  Is there any other technique possible, especially one that would avoid boxes?  If not possible (don't really expect it to be), is there a way that is more resilient to the definition of item?
 
   ([: ;/ [: ; (] , *:)^:(2&|)"0^:1 each)^:3  ;/ 1 2 3 
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬──┬──┬────┐ 
│1│1│1│1│1│1│1│1│2│3│9│9│81│9│81│81│6561│ 
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴─┴──┴──┴────┘ 


an attempt at "multiitemPower" conjunction, (where hook allows f to be ambivalent)

hook =: 2 : '([: u v) : (u v) '

miP =: 2 : '(([: ;/ [: ; u^:1 each)^:v) hook (<"_1)'

f miP 3 ] 1 2 3 
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬──┬──┬────┐ 
│1│1│1│1│1│1│1│1│2│3│9│9│81│9│81│81│6561│ 
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴─┴──┴──┴────┘


More information about the Programming mailing list