I neglected to paste into a clean session and sent the wrong version. This
version works as I expected.

Upon running it, you'll be able to access the functions and run the
examples like:

   'abc 123' -: deb ' abc     123 '
1

The helper function evaluates the last line in the block to create the
function

NB. creates and documents a function
NB. could be improved to split out example
NB. or automatically assert on the example
func=: 3 : 0
0!:0 > {: LF cut ][(y,'_doc')=: 0 : 0
)

func 'ltrim'
Removes left most space, if it exists
ex: 'abc' -: ltrim ' abc'
ltrim=: }.^:(' ' = {.)
)

func 'rtrim'
Removes right most space, if it exists
ex: 'abc' -: rtrim 'abc '
rtrim=: }:^:(' ' = {:)
)

func 'itrim'
Removes consecutive inner spaces
ex:  'abc 123' -: itrim 'abc     123'
itrim=: (#~ (0='  'E.]))
)

func 'deb'
Removes leading,trailing and consecutive spaces
ex:  'abc 123' -: deb ' abc     123 '
deb=: rtrim @: ltrim @: itrim
)



On Tue, Jul 15, 2014 at 11:46 AM, Joe Bogner <[email protected]> wrote:

> Here's my version of a alternative, possibly more readable example. It's
> likely excessive and unconventional:
>
> First, I need a helper:
>
>
> NB. creates and documents a function
> NB. could be improved to split out example
> NB. or automatically assert on the example
> func=: 3 : 0
> (0!:0 > {: LF cut (y,'_doc'))[(y,'_doc')=: 0 : 0
> )
>
> func 'ltrim'
> Removes left most space, if it exists
> ex: 'abc' -: ltrim ' abc'
> ltrim=: }.^:(' ' = {.)
> )
>
> func 'rtrim'
> Removes right most space, if it exists
> ex: 'abc' -: rtrim 'abc '
> rtrim=: }:^:(' ' = {:)
> )
>
> func 'itrim'
> Removes consecutive inner spaces
> ex:  'abc 123' -: itrim 'abc     123'
> itrim=: (#~ (0='  'E.]))
> )
>
> func 'deb'
> Removes leading,trailing and consecutive spaces
> ex:  'abc 123' -: deb ' abc     123 '
> deb=: rtrim @: ltrim @: itrim
> )
>
>
> You can then do things like:
>
>    deb_doc
>
> Removes leading,trailing and consecutive spaces
>
> ex: 'abc 123' -: deb ' abc 123 '
>
> deb=: rtrim @: ltrim @: itrim
>
>
>
> It would be neat to also be able to have all the examples automatically
> assert, but I'm not sure how to do that without sinking another few hours
> (probably) into it
>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to