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

18
ch02.jl
View File

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

View File

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

View File

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