Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all boxes with one element

Tags:

j

How do I remove all boxes with one element? E.g. I would like to turn this:

┌─────┬───┬─┬─────┐
│1 1 1│2 2│3│2 2 2│
└─────┴───┴─┴─────┘

Into this:

┌─────┬───┬─────┐
│1 1 1│2 2│2 2 2│
└─────┴───┴─────┘
like image 880
Darth Egregious Avatar asked Aug 03 '26 09:08

Darth Egregious


1 Answers

Suppose your value is in a:

   ]a =. 1 1 1; 2 2; 3; 2 2 2
┌─────┬───┬─┬─────┐
│1 1 1│2 2│3│2 2 2│
└─────┴───┴─┴─────┘

The trick here is to get the length of each box:

   #@> a
3 2 1 3

If the length > 1, you get a "mask" you can pass to copy.

   1 < #@> a
1 1 0 1

   a #~ 1 < #@> a
┌─────┬───┬─────┐
│1 1 1│2 2│2 2 2│
└─────┴───┴─────┘

13: will tell us how to make this tacit:

   13 : 'y #~ 1<#@>y'
] #~ 1 < #@>

As it happens, needing to put id in the last position of a fork is exactly what a hook does, so we can even simplify one more step:

   remsingles =. #~ 1 < #@>
   remsingles a
┌─────┬───┬─────┐
│1 1 1│2 2│2 2 2│
└─────┴───┴─────┘
like image 142
Daniel Lyons Avatar answered Aug 08 '26 10:08

Daniel Lyons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!