mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-12 09:44:24 +02:00
Small update on julia.html.markdown, typos and comments
Small update on julia.html.markdown, typos and comments
This commit is contained in:
@@ -11,7 +11,7 @@ Julia is a new homoiconic functional language focused on technical computing.
|
|||||||
While having the full power of homoiconic macros, first-class functions,
|
While having the full power of homoiconic macros, first-class functions,
|
||||||
and low-level control, Julia is as easy to learn and use as Python.
|
and low-level control, Julia is as easy to learn and use as Python.
|
||||||
|
|
||||||
This is based on Julia 1.0.0
|
This is based on Julia version 1.0.0.
|
||||||
|
|
||||||
```julia
|
```julia
|
||||||
# Single line comments start with a hash (pound) symbol.
|
# Single line comments start with a hash (pound) symbol.
|
||||||
@@ -83,7 +83,7 @@ false
|
|||||||
1 > 10 # => false
|
1 > 10 # => false
|
||||||
2 <= 2 # => true
|
2 <= 2 # => true
|
||||||
2 >= 2 # => true
|
2 >= 2 # => true
|
||||||
# Comparisons can be chained
|
# Comparisons can be chained, like in Python but unlike many other languages
|
||||||
1 < 2 < 3 # => true
|
1 < 2 < 3 # => true
|
||||||
2 < 3 < 2 # => false
|
2 < 3 < 2 # => false
|
||||||
|
|
||||||
@@ -93,19 +93,20 @@ false
|
|||||||
# Character literals are written with '
|
# Character literals are written with '
|
||||||
'a'
|
'a'
|
||||||
|
|
||||||
# Strings are UTF8 encoded. Only if they contain only ASCII characters can
|
# Strings are UTF8 encoded, so strings like "π" or "☃" are not directly equivalent
|
||||||
# they be safely indexed.
|
# to an array of single characters.
|
||||||
ascii("This is a string")[1]
|
# Only if they contain only ASCII characters can they be safely indexed.
|
||||||
|
ascii("This is a string")[1] # => 'T'
|
||||||
# => 'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
|
# => 'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
|
||||||
# Julia indexes from 1
|
# Beware, Julia indexes everything from 1 (like MATLAB), not 0 (like most languages).
|
||||||
# Otherwise, iterating over strings is recommended (map, for loops, etc).
|
# Otherwise, iterating over strings is recommended (map, for loops, etc).
|
||||||
|
|
||||||
# String can be compared lexicographically
|
# String can be compared lexicographically, in dictionnary order:
|
||||||
"good" > "bye" # => true
|
"good" > "bye" # => true
|
||||||
"good" == "good" # => true
|
"good" == "good" # => true
|
||||||
"1 + 2 = 3" == "1 + 2 = $(1 + 2)" # => true
|
"1 + 2 = 3" == "1 + 2 = $(1 + 2)" # => true
|
||||||
|
|
||||||
# $ can be used for string interpolation:
|
# $(..) can be used for string interpolation:
|
||||||
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
|
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
|
||||||
# You can put any Julia expression inside the parentheses.
|
# You can put any Julia expression inside the parentheses.
|
||||||
|
|
||||||
@@ -113,7 +114,7 @@ ascii("This is a string")[1]
|
|||||||
println("I'm Julia. Nice to meet you!") # => I'm Julia. Nice to meet you!
|
println("I'm Julia. Nice to meet you!") # => I'm Julia. Nice to meet you!
|
||||||
|
|
||||||
# Another way to format strings is the printf macro from the stdlib Printf.
|
# Another way to format strings is the printf macro from the stdlib Printf.
|
||||||
using Printf
|
using Printf # this is how you load (or import) a module
|
||||||
@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000
|
@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000
|
||||||
|
|
||||||
|
|
||||||
@@ -137,8 +138,9 @@ end
|
|||||||
SomeOtherVar123! = 6 # => 6
|
SomeOtherVar123! = 6 # => 6
|
||||||
|
|
||||||
# You can also use certain unicode characters
|
# You can also use certain unicode characters
|
||||||
|
# here ☃ is a Unicode 'snowman' characters, see http://emojipedia.org/%E2%98%83%EF%B8%8F if it displays wrongly here
|
||||||
☃ = 8 # => 8
|
☃ = 8 # => 8
|
||||||
# These are especially handy for mathematical notation
|
# These are especially handy for mathematical notation, like the constant π
|
||||||
2 * π # => 6.283185307179586
|
2 * π # => 6.283185307179586
|
||||||
|
|
||||||
# A note on naming conventions in Julia:
|
# A note on naming conventions in Julia:
|
||||||
@@ -171,7 +173,7 @@ matrix = [1 2; 3 4] # => 2×2 Array{Int64,2}: [1 2; 3 4]
|
|||||||
b = Int8[4, 5, 6] # => 3-element Array{Int8,1}: [4, 5, 6]
|
b = Int8[4, 5, 6] # => 3-element Array{Int8,1}: [4, 5, 6]
|
||||||
|
|
||||||
# Add stuff to the end of a list with push! and append!
|
# Add stuff to the end of a list with push! and append!
|
||||||
# By convention, the exclamation mark '!'' is appended to names of functions
|
# By convention, the exclamation mark '!' is appended to names of functions
|
||||||
# that modify their arguments
|
# that modify their arguments
|
||||||
push!(a, 1) # => [1]
|
push!(a, 1) # => [1]
|
||||||
push!(a, 2) # => [1,2]
|
push!(a, 2) # => [1,2]
|
||||||
@@ -375,7 +377,8 @@ end
|
|||||||
# Iterable types include Range, Array, Set, Dict, and AbstractString.
|
# Iterable types include Range, Array, Set, Dict, and AbstractString.
|
||||||
for animal = ["dog", "cat", "mouse"]
|
for animal = ["dog", "cat", "mouse"]
|
||||||
println("$animal is a mammal")
|
println("$animal is a mammal")
|
||||||
# You can use $ to interpolate variables or expression into strings
|
# You can use $ to interpolate variables or expression into strings.
|
||||||
|
# In this special case, no need for parenthesis: $animal and $(animal) give the same
|
||||||
end
|
end
|
||||||
# => dog is a mammal
|
# => dog is a mammal
|
||||||
# => cat is a mammal
|
# => cat is a mammal
|
||||||
@@ -408,7 +411,7 @@ end
|
|||||||
let x = 0
|
let x = 0
|
||||||
while x < 4
|
while x < 4
|
||||||
println(x)
|
println(x)
|
||||||
x += 1 # Shorthand for x = x + 1
|
x += 1 # Shorthand for in place increment: x = x + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# => 0
|
# => 0
|
||||||
|
Reference in New Issue
Block a user