new chapters 6 and 7
This commit is contained in:
parent
e73dbaf370
commit
e1d5277f8c
34
ch06.jl
34
ch06.jl
@ -1,8 +1,8 @@
|
||||
# Bogumił Kamiński, 2022
|
||||
|
||||
# Codes for chapter 4
|
||||
# Codes for chapter 6
|
||||
|
||||
# Code for listing 4.1
|
||||
# Code for listing 6.1
|
||||
|
||||
import Downloads
|
||||
Downloads.download("https://raw.githubusercontent.com/" *
|
||||
@ -37,11 +37,11 @@ Downloads.download("https://raw.githubusercontent.com/\
|
||||
|
||||
raw"C:\my_folder\my_file.txt"
|
||||
|
||||
# Code for listing 4.2
|
||||
# Code for listing 6.2
|
||||
|
||||
movies = readlines("movies.dat")
|
||||
|
||||
# Code for section 4.2
|
||||
# Code for section 6.2
|
||||
|
||||
movie1 = first(movies)
|
||||
|
||||
@ -50,7 +50,7 @@ movie1_parts = split(movie1, "::")
|
||||
supertype(String)
|
||||
supertype(SubString{String})
|
||||
|
||||
# Code for section 4.3
|
||||
# Code for section 6.3
|
||||
|
||||
movie1_parts[2]
|
||||
|
||||
@ -63,7 +63,7 @@ m[2]
|
||||
|
||||
parse(Int, m[2])
|
||||
|
||||
# Code for listing 4.3
|
||||
# Code for listing 6.3
|
||||
|
||||
function parseline(line::String)
|
||||
parts = split(line, "::")
|
||||
@ -78,7 +78,7 @@ end
|
||||
|
||||
record1 = parseline(movie1)
|
||||
|
||||
# Code for listing 4.4
|
||||
# Code for listing 6.4
|
||||
|
||||
codeunits("a")
|
||||
codeunits("ε")
|
||||
@ -104,7 +104,7 @@ isascii("∀ x: x≥0")
|
||||
word[1]
|
||||
word[5]
|
||||
|
||||
# Code for section 4.5
|
||||
# Code for section 6.5
|
||||
|
||||
records = parseline.(movies)
|
||||
|
||||
@ -122,14 +122,14 @@ years = [record.year for record in records]
|
||||
has_drama = ["Drama" in record.genres for record in records]
|
||||
drama_prop = proptable(years, has_drama; margins=1)
|
||||
|
||||
# Code for listing 4.5
|
||||
# Code for listing 6.5
|
||||
|
||||
using Plots
|
||||
|
||||
plot(names(drama_prop, 1), drama_prop[:, 2]; legend=false,
|
||||
xlabel="year", ylabel="Drama probability")
|
||||
|
||||
# Code for section 4.6.1
|
||||
# Code for section 6.6.1
|
||||
|
||||
s1 = Symbol("x")
|
||||
s2 = Symbol("hello world!")
|
||||
@ -147,14 +147,14 @@ Symbol("1")
|
||||
:hello world
|
||||
:1
|
||||
|
||||
# Code for section 4.6.2
|
||||
# Code for section 6.6.2
|
||||
|
||||
supertype(Symbol)
|
||||
|
||||
:x == :x
|
||||
:x == :y
|
||||
|
||||
# Code for listing 4.6
|
||||
# Code for listing 6.6
|
||||
|
||||
using BenchmarkTools
|
||||
str = string.("x", 1:10^6)
|
||||
@ -162,7 +162,7 @@ symb = Symbol.(str)
|
||||
@benchmark "x" in $str
|
||||
@benchmark :x in $symb
|
||||
|
||||
# Code for section 4.7
|
||||
# Code for section 6.7
|
||||
|
||||
using InlineStrings
|
||||
s1 = InlineString("x")
|
||||
@ -172,7 +172,7 @@ typeof(s2)
|
||||
sv = inlinestrings(["The", "quick", "brown", "fox", "jumps",
|
||||
"over", "the", "lazy", "dog"])
|
||||
|
||||
# Code for listing 4.7
|
||||
# Code for listing 6.7
|
||||
|
||||
using Random
|
||||
using BenchmarkTools
|
||||
@ -188,7 +188,7 @@ Base.summarysize(s2)
|
||||
@benchmark sort($s1)
|
||||
@benchmark sort($s2)
|
||||
|
||||
# Code for listing 4.8
|
||||
# Code for listing 6.8
|
||||
|
||||
open("iris.txt", "w") do io
|
||||
for i in 1:10^6
|
||||
@ -198,7 +198,7 @@ open("iris.txt", "w") do io
|
||||
end
|
||||
end
|
||||
|
||||
# Code for section 4.8.2
|
||||
# Code for section 6.8.2
|
||||
|
||||
uncompressed = readlines("iris.txt")
|
||||
|
||||
@ -208,7 +208,7 @@ compressed = PooledArray(uncompressed)
|
||||
Base.summarysize(uncompressed)
|
||||
Base.summarysize(compressed)
|
||||
|
||||
# Code for section 4.8.3
|
||||
# Code for section 6.8.3
|
||||
|
||||
compressed.invpool
|
||||
compressed.pool
|
||||
|
24
ch07.jl
24
ch07.jl
@ -1,8 +1,8 @@
|
||||
# Bogumił Kamiński, 2022
|
||||
|
||||
# Codes for chapter 5
|
||||
# Codes for chapter 7
|
||||
|
||||
# Code for listing 5.1
|
||||
# Code for listing 7.1
|
||||
|
||||
using HTTP
|
||||
using JSON3
|
||||
@ -11,7 +11,7 @@ query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
|
||||
response = HTTP.get(query)
|
||||
json = JSON3.read(response.body)
|
||||
|
||||
# Code for the remainder of section 5.1.2
|
||||
# Code for the remainder of section 7.1.2
|
||||
|
||||
response.body
|
||||
|
||||
@ -31,13 +31,13 @@ only(json.rates).mid
|
||||
only([])
|
||||
only([1, 2])
|
||||
|
||||
# Code for listing 5.2
|
||||
# Code for listing 7.2
|
||||
|
||||
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
|
||||
"2020-06-06/?format=json"
|
||||
response = HTTP.get(query)
|
||||
|
||||
# Code for listing 5.3
|
||||
# Code for listing 7.3
|
||||
|
||||
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
|
||||
"2020-06-01/?format=json"
|
||||
@ -67,7 +67,7 @@ catch e
|
||||
end
|
||||
end
|
||||
|
||||
# Code for section 5.2
|
||||
# Code for section 7.2
|
||||
|
||||
ismissing(missing)
|
||||
ismissing(1)
|
||||
@ -120,7 +120,7 @@ fun2 = passmissing(fun)
|
||||
fun2(1, 2)
|
||||
fun2(1, missing)
|
||||
|
||||
# Code for section 5.3
|
||||
# Code for section 7.3
|
||||
|
||||
using Dates
|
||||
d = Date("2020-06-01")
|
||||
@ -146,7 +146,7 @@ Date(2020, 5, 20):Day(1):Date(2020, 7, 5)
|
||||
|
||||
collect(Date(2020, 5, 20):Day(1):Date(2020, 7, 5))
|
||||
|
||||
# Code for listing 5.6
|
||||
# Code for listing 7.6
|
||||
|
||||
function get_rate(date::Date)
|
||||
query = "https://api.nbp.pl/api/exchangerates/rates/" *
|
||||
@ -172,11 +172,11 @@ end
|
||||
"https://api.nbp.pl/api/exchangerates/rates/" *
|
||||
"a/usd/$dates[1]/?format=json"
|
||||
|
||||
# Code for listing 5.7
|
||||
# Code for listing 7.7
|
||||
|
||||
rates = get_rate.(dates)
|
||||
|
||||
# Code for section 5.4
|
||||
# Code for section 7.4
|
||||
|
||||
using Statistics
|
||||
mean(rates)
|
||||
@ -185,7 +185,7 @@ std(rates)
|
||||
mean(skipmissing(rates))
|
||||
std(skipmissing(rates))
|
||||
|
||||
# Code for listing 5.8
|
||||
# Code for listing 7.8
|
||||
|
||||
using FreqTables
|
||||
proptable(dayname.(dates), ismissing.(rates); margins=1)
|
||||
@ -194,7 +194,7 @@ proptable(dayname.(dates), ismissing.(rates); margins=1)
|
||||
|
||||
dayname.(dates) .== "Thursday" .&& ismissing.(rates)
|
||||
|
||||
# Code for listing 5.9
|
||||
# Code for listing 7.9
|
||||
|
||||
dates[dayname.(dates) .== "Thursday" .&& ismissing.(rates)]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user