Skip to content

Commit

Permalink
More stylistic updates to the manual for consistency (#21463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored Apr 21, 2017
1 parent 5482f91 commit 5444473
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 83 deletions.
4 changes: 2 additions & 2 deletions doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ iterate over any array type.
If you write a custom `AbstractArray` type, you can specify that it has fast linear indexing using

```julia
Base.IndexStyle{T<:MyArray}(::Type{T}) = IndexLinear()
Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()
```

This setting will cause `eachindex` iteration over a `MyArray` to use integers. If you don't
Expand Down Expand Up @@ -716,7 +716,7 @@ Julia sparse matrices have the type `SparseMatrixCSC{Tv,Ti}`, where `Tv` is the
values, and `Ti` is the integer type for storing column pointers and row indices.:

```julia
type SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
struct SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
m::Int # Number of rows
n::Int # Number of columns
colptr::Vector{Ti} # Column i is in colptr[i]:(colptr[i+1]-1)
Expand Down
4 changes: 2 additions & 2 deletions doc/src/manual/dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ julia> dt2 = Date("2015-01-02",df)
You can also use the `dateformat""` string macro. This macro creates the `DateFormat` object once when the macro is expanded and uses the same `DateFormat` object even if a code snippet is run multiple times.

```jldoctest
julia> for i=1:10^5
Date("2015-01-01",dateformat"y-m-d")
julia> for i = 1:10^5
Date("2015-01-01", dateformat"y-m-d")
end
```

Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ instance, rather than just on the type itself. In these cases, you can add a met
for your custom type that returns the documentation on a per-instance basis. For instance,

```julia
type MyType
struct MyType
value::String
end

Expand Down
52 changes: 26 additions & 26 deletions doc/src/manual/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ further language bridges (e.g. calling Julia from Python or C#).

We start with a simple C program that initializes Julia and calls some Julia code:

```
```c
#include <julia.h>

int main(int argc, char *argv[])
Expand Down Expand Up @@ -83,15 +83,15 @@ shared data directory.
#### Example
```
```c
#include <julia.h>
int main(int argc, char *argv[])
{
jl_init();
(void)jl_eval_string("println(sqrt(2.0))");
jl_atexit_hook(0);
return 0;
jl_init();
(void)jl_eval_string("println(sqrt(2.0))");
jl_atexit_hook(0);
return 0;
}
```

Expand Down Expand Up @@ -143,7 +143,7 @@ Julia object. Storing simple data types like `Float64` in this way is called `bo
the stored primitive data is called `unboxing`. Our improved sample program that calculates the
square root of 2 in Julia and reads back the result in C looks as follows:

```
```c
jl_value_t *ret = jl_eval_string("sqrt(2.0)");

if (jl_typeis(ret, jl_float64_type)) {
Expand All @@ -163,7 +163,7 @@ is used in the above code snippet.

Corresponding `jl_box_...` functions are used to convert the other way:

```julia
```c
jl_value_t *a = jl_box_float64(3.0);
jl_value_t *b = jl_box_float32(3.0f);
jl_value_t *c = jl_box_int32(3);
Expand All @@ -177,7 +177,7 @@ While `jl_eval_string` allows C to obtain the result of a Julia expression, it d
passing arguments computed in C to Julia. For this you will need to invoke Julia functions directly,
using `jl_call`:

```julia
```c
jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
jl_value_t *argument = jl_box_float64(2.0);
jl_value_t *ret = jl_call1(func, argument);
Expand Down Expand Up @@ -211,7 +211,7 @@ safe to use pointers in between `jl_...` calls. But in order to make sure that v
`jl_...` calls, we have to tell Julia that we hold a reference to a Julia value. This can be done
using the `JL_GC_PUSH` macros:

```
```c
jl_value_t *ret = jl_eval_string("sqrt(2.0)");
JL_GC_PUSH1(&ret);
// Do something with ret
Expand All @@ -226,7 +226,7 @@ Several Julia values can be pushed at once using the `JL_GC_PUSH2` , `JL_GC_PUSH
macros. To push an array of Julia values one can use the `JL_GC_PUSHARGS` macro, which can be
used as follows:
```
```c
jl_value_t **args;
JL_GC_PUSHARGS(args, 2); // args can now hold 2 `jl_value_t*` objects
args[0] = some_value;
Expand All @@ -239,7 +239,7 @@ The garbage collector also operates under the assumption that it is aware of eve
object pointing to a young-generation one. Any time a pointer is updated breaking that assumption,
it must be signaled to the collector with the `jl_gc_wb` (write barrier) function like so:

```
```c
jl_value_t *parent = some_old_value, *child = some_young_value;
((some_specific_type*)parent)->field = child;
jl_gc_wb(parent, child);
Expand All @@ -253,7 +253,7 @@ can sometimes invoke garbage collection.
The write barrier is also necessary for arrays of pointers when updating their data directly.
For example:
```
```c
jl_array_t *some_array = ...; // e.g. a Vector{Any}
void **data = (void**)jl_array_data(some_array);
jl_value_t *some_value = ...;
Expand Down Expand Up @@ -286,15 +286,15 @@ struct that contains:
To keep things simple, we start with a 1D array. Creating an array containing Float64 elements
of length 10 is done by:

```julia
```c
jl_value_t* array_type = jl_apply_array_type(jl_float64_type, 1);
jl_array_t* x = jl_alloc_array_1d(array_type, 10);
```

Alternatively, if you have already allocated the array you can generate a thin wrapper around
its data:

```
```c
double *existingArray = (double*)malloc(sizeof(double)*10);
jl_array_t *x = jl_ptr_to_array_1d(array_type, existingArray, 10, 0);
```
Expand All @@ -305,21 +305,21 @@ referenced.

In order to access the data of x, we can use `jl_array_data`:

```
```c
double *xData = (double*)jl_array_data(x);
```

Now we can fill the array:

```
```c
for(size_t i=0; i<jl_array_len(x); i++)
xData[i] = i;
```

Now let us call a Julia function that performs an in-place operation on `x`:

```
jl_function_t *func = jl_get_function(jl_base_module, "reverse!");
```c
jl_function_t *func = jl_get_function(jl_base_module, "reverse!");
jl_call1(func, (jl_value_t*)x);
```
Expand All @@ -330,7 +330,7 @@ By printing the array, one can verify that the elements of `x` are now reversed.
If a Julia function returns an array, the return value of `jl_eval_string` and `jl_call` can be
cast to a `jl_array_t*`:
```
```c
jl_function_t *func = jl_get_function(jl_base_module, "reverse");
jl_array_t *y = (jl_array_t*)jl_call1(func, (jl_value_t*)x);
```
Expand All @@ -343,7 +343,7 @@ keep a reference to the array while it is in use.
Julia's multidimensional arrays are stored in memory in column-major order. Here is some code
that creates a 2D array and accesses its properties:

```
```c
// Create 2D array of float64 type
jl_value_t *array_type = jl_apply_array_type(jl_float64_type, 2);
jl_array_t *x = jl_alloc_array_2d(array_type, 10, 5);
Expand All @@ -369,14 +369,14 @@ in calling `jl_array_dim`) in order to read as idiomatic C code.

Julia code can throw exceptions. For example, consider:

```julia
```c
jl_eval_string("this_function_does_not_exist()");
```
This call will appear to do nothing. However, it is possible to check whether an exception was
thrown:
```julia
```c
if (jl_exception_occurred())
printf("%s \n", jl_typeof_str(jl_exception_occurred()));
```
Expand All @@ -390,22 +390,22 @@ was thrown, and then rethrows the exception in the host language.
When writing Julia callable functions, it might be necessary to validate arguments and throw exceptions
to indicate errors. A typical type check looks like:

```
```c
if (!jl_typeis(val, jl_float64_type)) {
jl_type_error(function_name, (jl_value_t*)jl_float64_type, val);
}
```

General exceptions can be raised using the functions:

```
```c
void jl_error(const char *str);
void jl_errorf(const char *fmt, ...);
```
`jl_error` takes a C string, and `jl_errorf` is called like `printf`:
```julia
```c
jl_errorf("argument x = %d is too large", x);
```

Expand Down
10 changes: 5 additions & 5 deletions doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ you can redefine types and constants. You can't import the type names into `Mai
to be able to redefine them there, but you can use the module name to resolve the scope. In other
words, while developing you might use a workflow something like this:

```
```julia
include("mynewcode.jl") # this defines a module MyModule
obj1 = MyModule.ObjConstructor(a, b)
obj2 = MyModule.somefunction(obj1)
Expand Down Expand Up @@ -108,10 +108,10 @@ have two options:

1. Use `import`:

```
```julia
import Foo
function bar(...)
... refer to Foo symbols via Foo.baz ...
# ... refer to Foo symbols via Foo.baz ...
end
```

Expand All @@ -120,12 +120,12 @@ have two options:
`Foo` symbols by their qualified names `Foo.bar` etc.
2. Wrap your function in a module:

```
```julia
module Bar
export bar
using Foo
function bar(...)
... refer to Foo.baz as simply baz ....
# ... refer to Foo.baz as simply baz ....
end
end
using Bar
Expand Down
10 changes: 5 additions & 5 deletions doc/src/manual/interacting-with-julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ history without prefix search, one could put the following code in `.juliarc.jl`
import Base: LineEdit, REPL

const mykeys = Dict{Any,Any}(
# Up Arrow
"\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)),
# Down Arrow
"\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_next(s, LineEdit.mode(s).hist))
# Up Arrow
"\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)),
# Down Arrow
"\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_next(s, LineEdit.mode(s).hist))
)

function customize_keys(repl)
repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)
repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)
end

atreplinit(customize_keys)
Expand Down
6 changes: 3 additions & 3 deletions doc/src/manual/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ julia> SparseArray{T,N}(::Type{T}, dims::NTuple{N,Int}) = SparseArray{T,N}(Dict{
julia> Base.size(A::SparseArray) = A.dims
julia> Base.similar{T}(A::SparseArray, ::Type{T}, dims::Dims) = SparseArray(T, dims)
julia> Base.similar(A::SparseArray, ::Type{T}, dims::Dims) where {T} = SparseArray(T, dims)
julia> Base.getindex{T,N}(A::SparseArray{T,N}, I::Vararg{Int,N}) = get(A.data, I, zero(T))
julia> Base.getindex(A::SparseArray{T,N}, I::Vararg{Int,N}) where {T,N} = get(A.data, I, zero(T))
julia> Base.setindex!{T,N}(A::SparseArray{T,N}, v, I::Vararg{Int,N}) = (A.data[I] = v)
julia> Base.setindex!(A::SparseArray{T,N}, v, I::Vararg{Int,N}) where {T,N} = (A.data[I] = v)
```

Notice that this is an `IndexCartesian` array, so we must manually define [`getindex()`](@ref) and [`setindex!()`](@ref)
Expand Down
14 changes: 7 additions & 7 deletions doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ I execute at runtime. The argument is: (1, 2, 3)

Macros are invoked with the following general syntax:

```
```julia
@name expr1 expr2 ...
@name(expr1, expr2, ...)
```
Expand All @@ -484,7 +484,7 @@ expressions in the first form, and the lack of whitespace after `@name` in the s
two styles should not be mixed. For example, the following syntax is different from the examples
above; it passes the tuple `(expr1, expr2, ...)` as one argument to the macro:

```
```julia
@name (expr1, expr2, ...)
```

Expand Down Expand Up @@ -661,7 +661,7 @@ with any user variables, and `time` and `println` will refer to the standard lib

One problem remains however. Consider the following use of this macro:

```
```julia
module MyModule
import Base.@time

Expand All @@ -675,7 +675,7 @@ Here the user expression `ex` is a call to `time`, but not the same `time` funct
uses. It clearly refers to `MyModule.time`. Therefore we must arrange for the code in `ex` to
be resolved in the macro call environment. This is done by "escaping" the expression with [`esc()`](@ref):

```
```julia
macro time(ex)
...
local val = $(esc(ex))
Expand Down Expand Up @@ -1075,7 +1075,7 @@ can be used to index into an array `A` using `A[i]`, instead of `A[x,y,z,...]`.
is the following:

```jldoctest sub2ind
julia> function sub2ind_loop{N}(dims::NTuple{N}, I::Integer...)
julia> function sub2ind_loop(dims::NTuple{N}, I::Integer...) where N
ind = I[N] - 1
for i = N-1:-1:1
ind = I[i]-1 + dims[i]*ind
Expand Down Expand Up @@ -1114,7 +1114,7 @@ we use generated functions to manually unroll the loop. The body becomes almost
instead of calculating the linear index, we build up an *expression* that calculates the index:

```jldoctest sub2ind_gen
julia> @generated function sub2ind_gen{N}(dims::NTuple{N}, I::Integer...)
julia> @generated function sub2ind_gen(dims::NTuple{N}, I::Integer...) where N
ex = :(I[$N] - 1)
for i = (N - 1):-1:1
ex = :(I[$i] - 1 + dims[$i] * $ex)
Expand All @@ -1132,7 +1132,7 @@ julia> sub2ind_gen((3, 5), 1, 2)
An easy way to find out is to extract the body into another (regular) function:

```jldoctest sub2ind_gen2
julia> @generated function sub2ind_gen{N}(dims::NTuple{N}, I::Integer...)
julia> @generated function sub2ind_gen(dims::NTuple{N}, I::Integer...) where N
return sub2ind_gen_impl(dims, I...)
end
sub2ind_gen (generic function with 1 method)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ Other known potential failure scenarios include:
code snippet:

```julia
type UniquedById
mutable struct UniquedById
myid::Int
let counter = 0
UniquedById() = new(counter += 1)
Expand Down
Loading

0 comments on commit 5444473

Please sign in to comment.