New example

Adding an example

Examples are a great way to showcase how to make use of a specific feature. We consider two types of examples:

  1. a simple example (running in < 10 seconds)

  2. a complex example (the complement of the first category)

The first category is great as they can be used as tests and as documentation.

The process is somewhat automated here and essentially all you have to do is write the example in the test directory and comment it accordingly, we show this below.

Syntax for the example

Let's say you have an example which can be run in a few seconds and uses a new feature. You can effectively use it as a unit test by itself. To respect conventions, please name your example ex_NAME.jl and put it in test/.

Start your code by

using Base.Test

Then a few markers should be considered:

So it should look like (a full example can be seen here)

using Base.Test
#@startexample A simple example
#=
In this example we will show how to find the maximum over a collection of
random numbers in Julia for all the numbers that are *negative*.
=#
a = randn(500)
# we use a comprehension
m = maximum(a[i] for i in 1:length(a) if a[i]<0)
#=
That's it!
=#
#@endexample
@test m < 0.0

Make sure the tests pass! This will generate the following markdown (see next point for the command that generates it):

# Name of the Test

In this example we will show how to find the maximum over a collection of
random numbers in Julia for all the numbers that are negative.
 ```julia
a = randn(500)
# we use a comprehension
m = maximum(a[i] for i in 1:length(a) if a[i]<0)
 ```
That's it!

Remark, the spaces in front of the triple backticks in the markdown snippet above are not actually generated when you use readexamples.jl. The spaces are used here in order to escape these triple backticks so that the snippet does not end up being unduly fragmented in three pieces.

Declaring your example

You have to mention your example in a few spots:

Finally, to generate the markdown run the following command (this will act for all the examples at once so it will also refresh any other modification you may have added to other examples):

julia docs/readexamples.jl