Hi all !

Roger Hui once wrote a small function, magic, as a solution to a problem we discussed in this forum. I work on a twisted version of J, JWithATwist, mostly to learn F#. This is the first program larger than a line of code I write in this language. It is an implementation of the magic algorithm within the present limitations of JWithATwist. It is incredibly slow, probably because of the strange way I have to do Cut without any Cut adverb.

magic=: ; @ (<@(</\&.|.);.2)~

The start of the discussion: http://www.jsoftware.com/pipermail/general/2005-September/024408.html

All opinions are welcome !

{
//Take 100 to show
   100 <.-
// y, I switched x and y
( 40000 $ 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 )
   {!
//Catenate and unbox boxed results. Fold here has a left argument with the initial accumulator value.
        |>
        ( |< |i. 0 ) {! |< ( |> [ ) , |> ] } /
//Very complicated implementation of cut. Create boxed indices to take from x.
        (
            ( |i. 1 + 0 + / [ )
            {!
                |<
                ( [ = ] ) /:: |i. |# ]
            } '/ 0 1 /
            0 + \ [
        )
        {!
            |<
//Reverse the group
            {!
                ( |i. |- |# ] ) <- ]
            }
//First one in the group
            {!
               ( |_ 1 = 0 + \  ] ) * ]
            }
//Reverse the group
            {!
                ( |i. |- |# ] ) <- ]
            }
//Pack up indexes. Take the group items from x.
            (  |>  [ ) <- ]
        } '/ 0 1 / ]
    }
//x
( 40000 $ 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 )
}

Cheers !

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

Reply via email to