rather than a general any rank function, here is one optimized for rank1. There is really only a need for rank 0 option, but that can be more easily hand crafted anyway.
miP1 =: 2 : 'a: -.~ ([: ; [: <"1 leaf u each)^:v' a one line example: ( [: (#~ 0 = 4 | +/"1) [: (, +:)^:(2 = #@$) [: 2 2"_^:(0 = #) [: +: ]`(] ,: +:)`(''"_)`(] ,: *:)@.(3 | +/)) (miP1 2) 3 1 ; 4 2 ;1 1 ┌───┬────┬─────┬─────┬─────┬────┬─────┬─────┬─────┬───┬────┬───┬───┬───┬─────┐ │2 2│24 8│48 16│48 16│96 32│24 8│48 16│48 16│96 32│2 2│16 8│4 4│8 8│8 8│16 16│ └───┴────┴─────┴─────┴─────┴────┴─────┴─────┴─────┴───┴────┴───┴───┴───┴─────┘ The data is boxed rank 1 items. The core (rightmost) function makes 0 1 or 2 items. secondary functions in the train (affected by miP conj) process rank 1 items unboxed one level, and can transform based on the full rank output of core function, though rank 1 will operate naturally with items. the functions applied are: rank 0 +: rank _ empty items are turned into "default" 2 2 item rank _ lists of 2 items are turned into longer list rank _ items are filtered based on rank1 test. This boxing of lists approach seems to avoid corner case problems. ----- Original Message ----- From: 'Pascal Jasmin' via Programming <programm...@jsoftware.com> To: "programm...@jsoftware.com" <programm...@jsoftware.com> Cc: Sent: Friday, April 10, 2015 11:31 AM Subject: Re: [Jprogramming] techniques to return a variable number of items from a function the main application is path finding where a given state can produce multiple or no new states to search. the approach so far seems to work with returning no results as well ([: ;/ [: ; ]`(] , *:)`(''"_)@.(3&|) each)^:3 ;/ 1 2 3 4 5 6 ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬──┬───┬──┬───┬───┬─────┬─┐ │1│1│1│1│1│1│1│1│3│4│16│16│256│16│256│256│65536│6│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴──┴───┴──┴───┴───┴─────┴─┘ or this variation for non scalar items that are erased ([: ;/ [: ; a: -.~ (] ,: +:)`(''"_)@.(13 > +/) each)^:2 ;/ 1 2 3 ,: 4 5 6 ┌─────┬───────┬───────┬────────┐ │4 5 6│8 10 12│8 10 12│16 20 24│ └─────┴───────┴───────┴────────┘ ----- Original Message ----- From: Raul Miller <rauldmil...@gmail.com> To: Programming forum <programm...@jsoftware.com> Cc: Sent: Friday, April 10, 2015 10:58 AM Subject: Re: [Jprogramming] techniques to return a variable number of items from a function The traditional approach here is: "Why are you doing that"? Generally speaking you want to push complexity out of the code and into the data, and want to avoid conditionals except where you deliberately want things to be able to break. Anyways, I don't get a sense of your application domain here, so I'm at a loss for making useful suggestions. Thanks, -- Raul On Fri, Apr 10, 2015 at 10:49 AM, 'Pascal Jasmin' via Programming <programm...@jsoftware.com> wrote: > 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│ > └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴─┴──┴──┴────┘ > ---------------------------------------------------------------------- > 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm