Skip to content

Commit 0f6d482

Browse files
LisaSchlueteroschulz
authored andcommittedFeb 29, 2024·
legend-julia-cheat-sheet coding help for software dev in julia
1 parent 70e771d commit 0f6d482

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
 

‎legend-julia-cheat-sheet.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Legend Julia Cheat Sheet
2+
This is a cheat sheet to help you with your coding projects in Julia.
3+
4+
## In the Julia REPL (read-eval-print loop —> command window) one can enter different modes:
5+
* `?` → help mode
6+
* `]` → package manager
7+
* `;` → terminal/shell mode
8+
9+
## VSCode (Editor) shortcuts:
10+
* `⌘+shift+P` (Mac) open command palette → from there open Julia REPL
11+
12+
## Useful Julia commands:
13+
* `typeof(x)` returns variable type of x
14+
* `size(array)` , `length(array)` similar to size and numel in matlab
15+
* `readdir()` similar to "ls" in shell
16+
* `replace(string,"pattern"=>"new_pattern")`
17+
* `isequal(x,y)` returns bool
18+
* `ifelse(condition, value_if_true, value_if_false)` function that chooses between two values based on a condition
19+
20+
### Anonymous functions:
21+
* define function without giving it a name
22+
* example: x->x*2
23+
* can be used in other functions, example: map(x->x*2,[1,2,3])
24+
25+
### Map: `map()`
26+
* Apply function to each element of array/vector/...
27+
* example see anonymous function.
28+
29+
### Pipe operator: `|>`
30+
* passes result of the expression on its left-hand side of `|>` and passes it as the first argument to the function on its right-hand side
31+
* example: `[3,1,2] |> x-> sort(x)` returns 3-element `Vector{Int64}: 1, 2, 3`
32+
33+
### Macros: `@`
34+
* symbol is used to denote macros, e.g. build-in macros like @time begin a = 5 end to measure the execution time
35+
* macros are different from functions (compile time vs. runtime)?
36+
37+
### PropertyFunctions `@pf`
38+
* Macro (Doc) to broadcast (element-wise) operation to properties of an object
39+
* Faster (reads only relevant entries, less data traffic) than other methods
40+
```
41+
Using StructArrays
42+
xs = StructArrays.StructArray((
43+
a = [1,2,3,4,5],
44+
b = [6,7,8,9,10],
45+
c = [11,12,13,14,15] ))
46+
out = xs.b + xs.a.^2 # standard wat to do some elementwise operation
47+
out_pf = @pf($b + $a.^2)(xs) # same result as out, but faster
48+
```
49+
50+
### Base.Fix1
51+
* Function `fun1` with mutltiple argument `arg1, arg2`
52+
* Create a new function `fun2` that is identical to `fun1` except that one of the arguments is fixed to a certain value
53+
```
54+
fun1 = (arg1, arg2) -> print("$arg1 $arg2")
55+
fun2 = Base.Fix1(fun1,"Hallo") #fix first arg1 in fun1
56+
fun2("Duda") # returns: "Hallo Duda"
57+
```
58+
* also possible to fix 2nd, 3rd,..argument. Using the same example as above:
59+
```
60+
fun3 = Base.Fix2(fun1,"Hallo") #fix first arg2 in fun1
61+
fun3("Duda") # returns: "Duda Hallo"
62+
```
63+
### Regular expressions (regex):
64+
* standard julia package
65+
* define a regex via r"hallo"
66+
* `match("pattern","string", startindex (opt))` search for pattern in string, pattern has to be regex
67+
* pattern examples:
68+
+ `r"\d+"` one or more digits, `r"\D"` any non-digit
69+
+ `r"l.*"` the character "l" and everything that comes after
70+
+ `r"(?=)"`
71+
72+
### Let block:
73+
* let blocks create a new scope and (optionally) introduce new local variables
74+
```
75+
let var1 = value1, var2, var3 = value3
76+
code
77+
end
78+
```
79+
* in this examble `var1`, `var2`, `var3` are new variables that are only accessible within let block
80+
* the values `value1` and `value3` are either actual values or variables defined before the let block
81+
82+
## Julia package manager:
83+
* `import Pkg` load package manager (alternatively enter package manager in REPL with `]`)
84+
* `Pkg.activate("Environmentpath")` create a new or activate existing environment ---> can also be defined in settings.json (see "Environments:")
85+
* `Pkg.add("LegendDataManagement")` add to package to project environment. clones package from git/main brain. Only for registered package
86+
* `Pkg.update("Packagename")` update from git
87+
* `Pkg.instantiate()` download/precompile correct version of packages that are listed in Manifest.toml
88+
* `Pkg.resolve()` check that the Manifest.toml is consistent with Project.tomls. then does instantiate()
89+
90+
### Package development:
91+
* `pathof(PackageName)` → location of package source code (package has to be used in environment)
92+
* Development of a package:
93+
1. Clone (fork of) package, e.g. LegendSpecFits in `path/LegendSpecFits.jl`
94+
2. In package manager: `dev path/LegendSpecFits.jl` , `using LegendSpecFits`
95+
note that packages can be developed "live", that means changed in functions are seen immediately
96+
3. To go back to the "official" package version: `add LegendSpecFits.jl` , `using LegendSpecFits`
97+
* Test packet changes with julia benchmark tools --> Google
98+
99+
### Environments (package versions):
100+
* In each folder with Julia code, there is a hidden directory called .vscode
101+
* Inside .vscode there is a file called `settings.json` that configures settings for this directory
102+
* example:
103+
```
104+
{
105+
"julia.environmentPath": "/home/iwsatlas1/schluete/.julia/environments/legend-dev",
106+
"git.ignoreLimitWarning": true // Ignores the warning when there are too many changes in a repository
107+
}
108+
```
109+
* The path in julia.environmentPath points to a Project.toml and Manifest.toml
110+
* → they define the package environment for all subfolder inside the main folder.

0 commit comments

Comments
 (0)
Please sign in to comment.