1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/julia_course.git synced 2024-11-16 13:28:10 +00:00

Add information about type hierarchy

This commit is contained in:
Mo8it 2022-03-26 14:38:33 +01:00
parent b8cb7cbe30
commit 63fee57cbd

View file

@ -318,33 +318,33 @@ In Julia, indexing starts with 1
# ⚠️ This results in an error! # ⚠️ This results in an error!
# first_vector[0] # first_vector[0]
# ╔═╡ 7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
# Last element
first_vector[end]
# ╔═╡ 45b64c7a-850b-402c-b7ce-2a0bf6d77060 # ╔═╡ 45b64c7a-850b-402c-b7ce-2a0bf6d77060
# ⚠️ No negative indexing, too! # ⚠️ No negative indexing!
# first_vector[-1] # first_vector[-1]
# ╔═╡ d1ed1515-cd59-4e10-a15c-b64325bc44c2 # ╔═╡ d1ed1515-cd59-4e10-a15c-b64325bc44c2
# Instead, this can be used # Instead, this can be used
first_vector[end-1] first_vector[end-1]
# ╔═╡ 7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
# Last element
first_vector[end]
# ╔═╡ 6c80e009-30de-4232-9a1b-ac954242a5a6 # ╔═╡ 6c80e009-30de-4232-9a1b-ac954242a5a6
md""" md"""
### Slicing ### Slicing
""" """
# ╔═╡ b1426df5-a083-4977-a72c-81e03fd7719d # ╔═╡ b1426df5-a083-4977-a72c-81e03fd7719d
# start:end # Syntax: start_index:end_index
first_vector[2:end] first_vector[2:4]
# ╔═╡ 5b16ca43-1f56-4934-a420-5ffa5ed437ec # ╔═╡ 5b16ca43-1f56-4934-a420-5ffa5ed437ec
# start:step:end # start_index:step:end_index
first_vector[1:2:end] first_vector[1:2:end]
# ╔═╡ 628852dc-16e5-4a03-93a9-be209b1e8fb4 # ╔═╡ 628852dc-16e5-4a03-93a9-be209b1e8fb4
# List of indices # Vector of indices
first_vector[[1, end, 2]] first_vector[[1, end, 2]]
# ╔═╡ 3ea54f0d-2aa5-47a3-bbc3-92023a56b834 # ╔═╡ 3ea54f0d-2aa5-47a3-bbc3-92023a56b834
@ -352,6 +352,13 @@ md"""
### Mutation ### Mutation
""" """
# ╔═╡ 6fdc6add-a478-4707-876b-cf6d660870ba
md"""
From now on, blocks will be used sometimes when manipulating an array to prevent dependency on execution order.
A block starts with `begin` and ends with `end`. Code in the block should be indented.
"""
# ╔═╡ e9e117af-1194-4d64-94a8-3e9fd51498aa # ╔═╡ e9e117af-1194-4d64-94a8-3e9fd51498aa
# Setting the first element to 3 # Setting the first element to 3
begin begin
@ -368,9 +375,11 @@ third_vector = [1, 2, 3]
# third_vector[2] = 42.1 # third_vector[2] = 42.1
# ╔═╡ 65d3ddc2-36ed-4126-9211-e838ffc0d859 # ╔═╡ 65d3ddc2-36ed-4126-9211-e838ffc0d859
# This is because the type of the array is Int64, a Float64 can not be inserted! All elements of an array have to have the same type!
typeof(third_vector) typeof(third_vector)
# ╔═╡ 641e8c05-3e80-47e5-be77-91090a5f799a # ╔═╡ 641e8c05-3e80-47e5-be77-91090a5f799a
# You can mix types during initialization of an array
begin begin
mixed_vector = ["hi", 2.1, 55.7] mixed_vector = ["hi", 2.1, 55.7]
mixed_vector[2] = 1 + 2im mixed_vector[2] = 1 + 2im
@ -378,8 +387,36 @@ begin
end end
# ╔═╡ 2b9a5867-ca82-4a27-a700-bd0bd6c89bbe # ╔═╡ 2b9a5867-ca82-4a27-a700-bd0bd6c89bbe
# But then, the type of the array is Any
typeof(mixed_vector) typeof(mixed_vector)
# ╔═╡ 944e2d37-8280-47b8-b874-97221955d048
md"""
`Any` is the *abstract* type of everything.
Julia does have abstract types which are helpful for multiple dispatch. More about multiple dispatch later 😉
"""
# ╔═╡ 23d4ac67-05ec-4b3d-8368-86256076be62
# The type hierarchy
supertypes(Int64)
# ╔═╡ 866edf5e-a76c-448d-98e8-925eaed5eba5
subtypes(Number)
# ╔═╡ a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
subtypes(Real)
# ╔═╡ 786a96be-16cd-4f1b-9b5f-138e232d3183
subtypes(Integer)
# ╔═╡ dd8dad86-6bc2-4489-8469-7eac80fc41bb
subtypes(Signed)
# ╔═╡ c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# Hierarchy of Float64
supertypes(Float64)
# ╔═╡ 87b43f26-7437-4ee9-9b83-5b21e86dd0c9 # ╔═╡ 87b43f26-7437-4ee9-9b83-5b21e86dd0c9
md""" md"""
⚠️ Try to avoid using mixed arrays (type `Any`)! ⚠️ Try to avoid using mixed arrays (type `Any`)!
@ -761,20 +798,28 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
# ╠═8c47710f-1ed2-40fd-9290-374b498380e3 # ╠═8c47710f-1ed2-40fd-9290-374b498380e3
# ╟─a03b46cc-1b26-44c2-b83d-884e3dbbe4fa # ╟─a03b46cc-1b26-44c2-b83d-884e3dbbe4fa
# ╠═68658408-188e-43f7-ad74-251172dec0a8 # ╠═68658408-188e-43f7-ad74-251172dec0a8
# ╠═7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
# ╠═45b64c7a-850b-402c-b7ce-2a0bf6d77060 # ╠═45b64c7a-850b-402c-b7ce-2a0bf6d77060
# ╠═d1ed1515-cd59-4e10-a15c-b64325bc44c2 # ╠═d1ed1515-cd59-4e10-a15c-b64325bc44c2
# ╠═7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
# ╟─6c80e009-30de-4232-9a1b-ac954242a5a6 # ╟─6c80e009-30de-4232-9a1b-ac954242a5a6
# ╠═b1426df5-a083-4977-a72c-81e03fd7719d # ╠═b1426df5-a083-4977-a72c-81e03fd7719d
# ╠═5b16ca43-1f56-4934-a420-5ffa5ed437ec # ╠═5b16ca43-1f56-4934-a420-5ffa5ed437ec
# ╠═628852dc-16e5-4a03-93a9-be209b1e8fb4 # ╠═628852dc-16e5-4a03-93a9-be209b1e8fb4
# ╟─3ea54f0d-2aa5-47a3-bbc3-92023a56b834 # ╟─3ea54f0d-2aa5-47a3-bbc3-92023a56b834
# ╟─6fdc6add-a478-4707-876b-cf6d660870ba
# ╠═e9e117af-1194-4d64-94a8-3e9fd51498aa # ╠═e9e117af-1194-4d64-94a8-3e9fd51498aa
# ╠═027313d6-c247-43e9-872b-c3f0fe71b733 # ╠═027313d6-c247-43e9-872b-c3f0fe71b733
# ╠═e77e7ceb-31e3-4231-9923-f62b1382a2d1 # ╠═e77e7ceb-31e3-4231-9923-f62b1382a2d1
# ╠═65d3ddc2-36ed-4126-9211-e838ffc0d859 # ╠═65d3ddc2-36ed-4126-9211-e838ffc0d859
# ╠═641e8c05-3e80-47e5-be77-91090a5f799a # ╠═641e8c05-3e80-47e5-be77-91090a5f799a
# ╠═2b9a5867-ca82-4a27-a700-bd0bd6c89bbe # ╠═2b9a5867-ca82-4a27-a700-bd0bd6c89bbe
# ╟─944e2d37-8280-47b8-b874-97221955d048
# ╠═23d4ac67-05ec-4b3d-8368-86256076be62
# ╠═866edf5e-a76c-448d-98e8-925eaed5eba5
# ╠═a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
# ╠═786a96be-16cd-4f1b-9b5f-138e232d3183
# ╠═dd8dad86-6bc2-4489-8469-7eac80fc41bb
# ╠═c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# ╟─87b43f26-7437-4ee9-9b83-5b21e86dd0c9 # ╟─87b43f26-7437-4ee9-9b83-5b21e86dd0c9
# ╠═22f5ebc1-fd5c-4ee7-b169-8144fbd9b570 # ╠═22f5ebc1-fd5c-4ee7-b169-8144fbd9b570
# ╠═7479c420-2e04-4fe7-823c-3fde9efb54ca # ╠═7479c420-2e04-4fe7-823c-3fde9efb54ca