new chapters 6 and 7

This commit is contained in:
Bogumił Kamiński 2022-02-12 19:16:05 +01:00
parent e73dbaf370
commit e1d5277f8c
2 changed files with 29 additions and 29 deletions

34
ch06.jl
View File

@ -1,8 +1,8 @@
# Bogumił Kamiński, 2022 # Bogumił Kamiński, 2022
# Codes for chapter 4 # Codes for chapter 6
# Code for listing 4.1 # Code for listing 6.1
import Downloads import Downloads
Downloads.download("https://raw.githubusercontent.com/" * Downloads.download("https://raw.githubusercontent.com/" *
@ -37,11 +37,11 @@ Downloads.download("https://raw.githubusercontent.com/\
raw"C:\my_folder\my_file.txt" raw"C:\my_folder\my_file.txt"
# Code for listing 4.2 # Code for listing 6.2
movies = readlines("movies.dat") movies = readlines("movies.dat")
# Code for section 4.2 # Code for section 6.2
movie1 = first(movies) movie1 = first(movies)
@ -50,7 +50,7 @@ movie1_parts = split(movie1, "::")
supertype(String) supertype(String)
supertype(SubString{String}) supertype(SubString{String})
# Code for section 4.3 # Code for section 6.3
movie1_parts[2] movie1_parts[2]
@ -63,7 +63,7 @@ m[2]
parse(Int, m[2]) parse(Int, m[2])
# Code for listing 4.3 # Code for listing 6.3
function parseline(line::String) function parseline(line::String)
parts = split(line, "::") parts = split(line, "::")
@ -78,7 +78,7 @@ end
record1 = parseline(movie1) record1 = parseline(movie1)
# Code for listing 4.4 # Code for listing 6.4
codeunits("a") codeunits("a")
codeunits("ε") codeunits("ε")
@ -104,7 +104,7 @@ isascii("∀ x: x≥0")
word[1] word[1]
word[5] word[5]
# Code for section 4.5 # Code for section 6.5
records = parseline.(movies) records = parseline.(movies)
@ -122,14 +122,14 @@ years = [record.year for record in records]
has_drama = ["Drama" in record.genres for record in records] has_drama = ["Drama" in record.genres for record in records]
drama_prop = proptable(years, has_drama; margins=1) drama_prop = proptable(years, has_drama; margins=1)
# Code for listing 4.5 # Code for listing 6.5
using Plots using Plots
plot(names(drama_prop, 1), drama_prop[:, 2]; legend=false, plot(names(drama_prop, 1), drama_prop[:, 2]; legend=false,
xlabel="year", ylabel="Drama probability") xlabel="year", ylabel="Drama probability")
# Code for section 4.6.1 # Code for section 6.6.1
s1 = Symbol("x") s1 = Symbol("x")
s2 = Symbol("hello world!") s2 = Symbol("hello world!")
@ -147,14 +147,14 @@ Symbol("1")
:hello world :hello world
:1 :1
# Code for section 4.6.2 # Code for section 6.6.2
supertype(Symbol) supertype(Symbol)
:x == :x :x == :x
:x == :y :x == :y
# Code for listing 4.6 # Code for listing 6.6
using BenchmarkTools using BenchmarkTools
str = string.("x", 1:10^6) str = string.("x", 1:10^6)
@ -162,7 +162,7 @@ symb = Symbol.(str)
@benchmark "x" in $str @benchmark "x" in $str
@benchmark :x in $symb @benchmark :x in $symb
# Code for section 4.7 # Code for section 6.7
using InlineStrings using InlineStrings
s1 = InlineString("x") s1 = InlineString("x")
@ -172,7 +172,7 @@ typeof(s2)
sv = inlinestrings(["The", "quick", "brown", "fox", "jumps", sv = inlinestrings(["The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog"]) "over", "the", "lazy", "dog"])
# Code for listing 4.7 # Code for listing 6.7
using Random using Random
using BenchmarkTools using BenchmarkTools
@ -188,7 +188,7 @@ Base.summarysize(s2)
@benchmark sort($s1) @benchmark sort($s1)
@benchmark sort($s2) @benchmark sort($s2)
# Code for listing 4.8 # Code for listing 6.8
open("iris.txt", "w") do io open("iris.txt", "w") do io
for i in 1:10^6 for i in 1:10^6
@ -198,7 +198,7 @@ open("iris.txt", "w") do io
end end
end end
# Code for section 4.8.2 # Code for section 6.8.2
uncompressed = readlines("iris.txt") uncompressed = readlines("iris.txt")
@ -208,7 +208,7 @@ compressed = PooledArray(uncompressed)
Base.summarysize(uncompressed) Base.summarysize(uncompressed)
Base.summarysize(compressed) Base.summarysize(compressed)
# Code for section 4.8.3 # Code for section 6.8.3
compressed.invpool compressed.invpool
compressed.pool compressed.pool

24
ch07.jl
View File

@ -1,8 +1,8 @@
# Bogumił Kamiński, 2022 # 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 HTTP
using JSON3 using JSON3
@ -11,7 +11,7 @@ query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
response = HTTP.get(query) response = HTTP.get(query)
json = JSON3.read(response.body) 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 response.body
@ -31,13 +31,13 @@ only(json.rates).mid
only([]) only([])
only([1, 2]) only([1, 2])
# Code for listing 5.2 # Code for listing 7.2
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" * query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-06/?format=json" "2020-06-06/?format=json"
response = HTTP.get(query) response = HTTP.get(query)
# Code for listing 5.3 # Code for listing 7.3
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" * query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-01/?format=json" "2020-06-01/?format=json"
@ -67,7 +67,7 @@ catch e
end end
end end
# Code for section 5.2 # Code for section 7.2
ismissing(missing) ismissing(missing)
ismissing(1) ismissing(1)
@ -120,7 +120,7 @@ fun2 = passmissing(fun)
fun2(1, 2) fun2(1, 2)
fun2(1, missing) fun2(1, missing)
# Code for section 5.3 # Code for section 7.3
using Dates using Dates
d = Date("2020-06-01") 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)) 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) function get_rate(date::Date)
query = "https://api.nbp.pl/api/exchangerates/rates/" * query = "https://api.nbp.pl/api/exchangerates/rates/" *
@ -172,11 +172,11 @@ end
"https://api.nbp.pl/api/exchangerates/rates/" * "https://api.nbp.pl/api/exchangerates/rates/" *
"a/usd/$dates[1]/?format=json" "a/usd/$dates[1]/?format=json"
# Code for listing 5.7 # Code for listing 7.7
rates = get_rate.(dates) rates = get_rate.(dates)
# Code for section 5.4 # Code for section 7.4
using Statistics using Statistics
mean(rates) mean(rates)
@ -185,7 +185,7 @@ std(rates)
mean(skipmissing(rates)) mean(skipmissing(rates))
std(skipmissing(rates)) std(skipmissing(rates))
# Code for listing 5.8 # Code for listing 7.8
using FreqTables using FreqTables
proptable(dayname.(dates), ismissing.(rates); margins=1) proptable(dayname.(dates), ismissing.(rates); margins=1)
@ -194,7 +194,7 @@ proptable(dayname.(dates), ismissing.(rates); margins=1)
dayname.(dates) .== "Thursday" .&& ismissing.(rates) dayname.(dates) .== "Thursday" .&& ismissing.(rates)
# Code for listing 5.9 # Code for listing 7.9
dates[dayname.(dates) .== "Thursday" .&& ismissing.(rates)] dates[dayname.(dates) .== "Thursday" .&& ismissing.(rates)]