What is the function of the 'assert' keyword in the J programming language?

95 views Asked by At

What is the function of combining assert and -: in J? Under what circumstances will we apply assert in order to proceed? Thank you very much.

1

There are 1 answers

1
bob On

assert is a name for the defined verb

assert=: 0 0 $ 13!:8^:((0 e. ])`(12"_)) 

which looks complicated but actually is just testing for a 0 anywhere in a vector using (0 e. ]) If there is a zero then (12"_) ,which is a way to write a verb that returns 12 no matter what its argument, is sent to 13!:8 which is the foreign conjunction that signals errors. Not surprisingly 12 signals an assert error which is what you want having used assert. The 0 0 $ in the front assures that after the error interaction begins on the very next line.

The monadic form -: (Half) does not usually come into play when used with assert. Dyadic -: (Match) comes into play as a way to see if two things are a match to each other and it returns a 1 if they are and a 0 otherwise. This means that the standard way of using assert and -: together are in this form.

assert (expected result) -: (computed result)

If the computed result matches the expected result there is no assert error, if it does not then an assert error is raised. Using numerous assert tests allows a test driven development environment.