sync code from and code for

This commit is contained in:
Bogumił Kamiński 2022-04-19 13:51:42 +02:00
parent d945f3126b
commit 160247f5ad
4 changed files with 19 additions and 19 deletions

View File

@ -2,7 +2,7 @@
# Codes for chapter 1
# Code from section 1.2.1
# Code for section 1.2.1
function sum_n(n)
s = 0
@ -14,7 +14,7 @@ end
@time sum_n(1_000_000_000)
# Code from section 1.4
# Code for section 1.4
@time using Plots
@time plot(1:10)

18
ch02.jl
View File

@ -160,13 +160,13 @@ x = 9.0
y = x > 0 ? sqrt(x) : sqrt(-x)
y
# Code from listing 2.4
# Code for listing 2.4
for i in [1, 2, 3]
println(i, " is ", isodd(i) ? "odd" : "even")
end
# Code from listing 2.5
# Code for listing 2.5
i = 1
while i < 4
@ -184,7 +184,7 @@ while true
println(i, " is even")
end
# Code from listing 2.6
# Code for listing 2.6
x = -7
x < 0 && begin
@ -195,7 +195,7 @@ x < 0 && begin
end
x > 0 ? (println(x); x) : (x += 1; println(x); x)
# Code from section 2.3.4
# Code for section 2.3.4
x = [8, 3, 1, 5, 7]
k = 1
@ -215,14 +215,14 @@ end
s
s / length(y)
# Code from listing 2.7
# Code for listing 2.7
function times_two(x)
return 2 * x
end
times_two(10)
# Code from listing 2.8
# Code for listing 2.8
function compose(x, y=10; a, b=10)
return x, y, a, b
@ -233,7 +233,7 @@ compose(1; a=3)
compose(1)
compose(; a=3)
# Code from listing 2.9
# Code for listing 2.9
times_two(x) = 2 * x
compose(x, y=10; a, b=10) = x, y, a, b
@ -242,7 +242,7 @@ compose(x, y=10; a, b=10) = x, y, a, b
map(times_two, [1, 2, 3])
# Code from listing 2.10
# Code for listing 2.10
map(x -> 2 * x, [1, 2, 3])
@ -281,7 +281,7 @@ function winsorized_mean(x, k)
end
winsorized_mean([8, 3, 1, 5, 7], 1)
# Code from section 2.5
# Code for section 2.5
function fun1()
x = 1

View File

@ -2,7 +2,7 @@
# Codes for chapter 3
# Code from section 3.1
# Code for section 3.1
methods(cd)
@ -36,7 +36,7 @@ AbstractVector
typejoin(typeof([1.0, 2.0, 3.0]), typeof(1:3))
# Code from section 3.2
# Code for section 3.2
fun(x) = println("unsupported type")
fun(x::Number) = println("a number was passed")
@ -78,7 +78,7 @@ winsorized_mean(10, 1)
winsorized_mean(1:10, -1)
winsorized_mean(1:10, 5)
# Code from section 3.3
# Code for section 3.3
module ExampleModule
@ -117,7 +117,7 @@ using StatsBase
?winsor
mean(winsor([8, 3, 1, 5, 7], count=1))
# Code from section 3.4
# Code for section 3.4
@time 1 + 2

View File

@ -16,7 +16,7 @@ x = [1.5]
x[1] = 1
x
# Code from section 11.1.1
# Code for section 11.1.1
Matrix(walk)
Matrix{Any}(walk)
@ -27,7 +27,7 @@ plot(walk)
plot(Matrix(walk); labels=["x" "y"] , legend=:topleft)
# Code from section 11.1.2
# Code for section 11.1.2
Tables.columntable(walk)
@ -67,7 +67,7 @@ unique(df)
tab = Tables.columntable(df)
unique(tab)
# Code from section 11.1.3
# Code for section 11.1.3
Tables.rowtable(walk)
@ -88,7 +88,7 @@ identity.(eachcol(walk))
df = DataFrame(x=1:2, b=["a", "b"])
identity.(eachcol(df))
# Code from section 11.2
# Code for section 11.2
using CSV
raw_data = """