Skip to content

Commit

Permalink
Improve Julia
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuel Lima <[email protected]>
  • Loading branch information
emanuellima1 committed Mar 15, 2024
1 parent 360eef1 commit 5a9649b
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/jl_dive.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Basics](#basics)
- [Data Types and Structures](#data-types-and-structures)
- [Scalar Types](#scalar-types)
- [Const values](#const-values)
- [Basic Math](#basic-math)
- [Strings](#strings)
- [Arrays](#arrays)
Expand All @@ -23,6 +24,7 @@
- [I/O](#io)
- [Metaprogramming](#metaprogramming)
- [Exceptions](#exceptions)
- [REPL](#repl)
- [DataFrames](#dataframes)
- [References](#references)

Expand Down Expand Up @@ -53,7 +55,7 @@ In the REPL, by pressing "]" you can enter the "package mode", where you can wri

To use a package on a Julia script, write `using [package]` at the beginning of the script. To use a package without populating the namespace, write `import [package]`. But then, you will have to use the functions as `[package].function()`. You can also include local Julia scripts as such: `include("my_script.jl")`.

I think that `using [package]` is bad practice. The best way to import a package is this:
I think that `using [package]` is bad practice because it pollutes the namespace. The best way to import a package is this:

```julia
# Importing the JSON package through an alias
Expand All @@ -71,7 +73,15 @@ Some built-in data types and structures of the Julia language:

### Scalar Types

The usual scalar types are present: Int64, Float64, Char and Bool.
The usual scalar types are present: Int64, UInt128, BigInt, Float64, Char and Bool.

### Const values

Constant values are declared as such:

```julia
const foo = 1234
```

### Basic Math

Expand Down Expand Up @@ -182,6 +192,28 @@ Some operations on arrays:

Functions that end in '!' modify their first argument.

Map applies a function to every element in the input arrays:

```julia
map(func, my_array)
```

Filter takes a collection of values, `xs`, and returns a subset, `ys`, of those
values. The specific values from `xs` that are included in the resulting `ys` are deter-
mined by the predicate `p`. A predicate is a function that takes some value and always returns a Boolean value:

```julia
ys = filter(p, xs)
```

Reduce takes some binary function, `g`, as the first argument, and then uses this function to combine the elements in the collection, `xs`, provided as the second argument:

```julia
y = reduce(g, xs)
```

Mapreduce can be understood as `reduce(g, map(f, xs))`.

### Multidimensional and Nested Arrays

A matrix is an array of arrays that have the same length. The main difference between a matrix and an array of arrays is that, with a matrix, the number of elements on each column (row) must be the same and rules of linear algebra apply.
Expand Down Expand Up @@ -310,6 +342,7 @@ convertedObj = convert(T,x)
The typical control flow is present:

```julia
# 1 and 5 are included on this range
for i = 1:5
println(i)
end
Expand Down Expand Up @@ -465,6 +498,14 @@ f = i -> replace(i, "x" => "y")
myArray = f.(myArray)
```

Functions whose name is a singular symbol can be used on an infix or prefix form:

```julia
5 + 3

+(5, 3)
```

## Custom Types

There are two type operators:
Expand Down Expand Up @@ -616,6 +657,14 @@ function volume(region, year)
end
```

## REPL

One can load a Julia file into the REPL to experiment with it:

```julia
include("my_file.jl")
```

## DataFrames

Examples:
Expand Down

0 comments on commit 5a9649b

Please sign in to comment.