Thanks Erling -- I also try to test as much as possible and I agree it's
often better than documentation since it's executable and shows how the
data/code works together.

I updated the func helper to automatically test/assert examples:

NB. creates and documents a function
NB. could be improved to split out example
NB. or automatically assert on the example
func=: 3 : 0
doc=.[(y,'_doc')=: 0 : 0
lines=.LF cut doc
0!:0 > {: lines
examples=.[(y,'_examples')=:3 }. each (#~ (<'ex:') E. 3 {. each [) lines
assert each ". each examples
''
)

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

func 'rtrim'
Removes right most space, if it exists
ex: 'abc' -: rtrim 'abc '
ex: 'abc ' -: rtrim 'abc  ' NB. only one trimmed
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
)


NB. intentionally show a broken example
func 'deb_broken'
Removes leading,trailing and consecutive spaces
ex:  'abc 123' -: deb_broken ' abc     123 '
deb_broken=: rtrim  @: itrim
)



On Tue, Jul 15, 2014 at 2:05 PM, Raul Miller <[email protected]> wrote:

> Testing code is why we use an interactive language.
>
> I don't think anyone can learn J if they don't test their understanding of
> it
>
> Thanks,
>
> --
> Raul
>
>
>
> On Tue, Jul 15, 2014 at 1:58 PM, Erling Hellenäs <[email protected]
> >
> wrote:
>
> > Great job.
> >
> > The executable examples makes me think of TDD, Test Driven Design. Anyone
> > writes his tests before the code?
> >
> > Tests can also be a kind of documentation. When you use TDD and BDD, the
> > tests basically replaces the specifications and the requirements.
> >
> > With the open source package managers it is sometimes possible to deliver
> > test suites with the code. When you build a package you can automatically
> > run the test suite. Failing tests can trigger an automatic bug report to
> > the developers.
> >
> > So, with the code you could also deliver tests. Tests you probably had to
> > write anyway. They can act as a thorough functional specification of the
> > code.
> >
> > /Erling
> >
> >
> >
> > On 2014-07-15 19:15, Joe Bogner wrote:
> >
> >> 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
> >>
> >>
> > ----------------------------------------------------------------------
> > 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

Reply via email to