-
Notifications
You must be signed in to change notification settings - Fork 49
/
ch06.jl
227 lines (151 loc) · 3.77 KB
/
ch06.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Bogumił Kamiński, 2022
# Codes for chapter 6
# Code for listing 6.1
import Downloads
Downloads.download("https://raw.githubusercontent.com/" *
"sidooms/MovieTweetings/" *
"44c525d0c766944910686c60697203cda39305d6/" *
"snapshots/10K/movies.dat",
"movies.dat")
# Code for string interpolation examples
x = 10
"I have $x apples"
"I have $(2 * x) apples"
"I have \$100."
"I have $100."
# Code for multiline strings
Downloads.download("https://raw.githubusercontent.com/\
sidooms/MovieTweetings/\
44c525d0c766944910686c60697203cda39305d6/\
snapshots/10K/movies.dat",
"movies.dat")
"a\
b\
c"
# Code for raw strings
"C:\my_folder\my_file.txt"
raw"C:\my_folder\my_file.txt"
print(raw"C:\my_folder\my_file.txt")
# Code for listing 6.2
movies = readlines("movies.dat")
# Code for section 6.2
movie1 = first(movies)
movie1_parts = split(movie1, "::")
supertype(String)
supertype(SubString{String})
# Code for section 6.3
movie1_parts[2]
rx = r"(.+) \((\d{4})\)$"
m = match(rx, movie1_parts[2])
m[1]
m[2]
parse(Int, m[2])
# Code for listing 6.3
function parseline(line::AbstractString)
parts = split(line, "::")
m = match(r"(.+) \((\d{4})\)", parts[2])
return (id=parts[1],
name=m[1],
year=parse(Int, m[2]),
genres=split(parts[3], "|"))
end
# Code for parsing one line of movies data
record1 = parseline(movie1)
# Code for listing 6.4
codeunits("a")
codeunits("ε")
codeunits("∀")
# Codes for different patterns of string subsetting
word = first(record1.name, 8)
record1.name[1:8]
for i in eachindex(word)
println(i, ": ", word[i])
end
codeunits("ô")
codeunits("Fantômas")
isascii("Hello world!")
isascii("∀ x: x≥0")
word[1]
word[5]
# Code for section 6.5
records = parseline.(movies)
genres = String[]
for record in records
append!(genres, record.genres)
end
genres
using FreqTables
table = freqtable(genres)
sort!(table)
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 6.5
using Plots
plot(names(drama_prop, 1), drama_prop[:, 2]; legend=false,
xlabel="year", ylabel="Drama probability")
# Code for section 6.6.1
s1 = Symbol("x")
s2 = Symbol("hello world!")
s3 = Symbol("x", 1)
typeof(s1)
typeof(s2)
typeof(s3)
Symbol("1")
:x
:x1
:hello world
:1
# Code for section 6.6.2
supertype(Symbol)
:x == :x
:x == :y
# Code for listing 6.6
using BenchmarkTools
str = string.("x", 1:10^6)
symb = Symbol.(str)
@btime "x" in $str;
@btime :x in $symb;
# Code for section 6.7
using InlineStrings
s1 = InlineString("x")
typeof(s1)
s2 = InlineString("∀")
typeof(s2)
sv = inlinestrings(["The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog"])
# Code for listing 6.7
using Random
using BenchmarkTools
Random.seed!(1234);
s1 = [randstring(3) for i in 1:10^6]
s2 = inlinestrings(s1)
# Code for analyzing properties of InlineStrings.jl
Base.summarysize(s1)
Base.summarysize(s2)
@btime sort($s1);
@btime sort($s2);
# Code for listing 6.8
open("iris.txt", "w") do io
for i in 1:10^6
println(io, "Iris setosa")
println(io, "Iris virginica")
println(io, "Iris versicolor")
end
end
# Code for section 6.8.2
uncompressed = readlines("iris.txt")
using PooledArrays
compressed = PooledArray(uncompressed)
Base.summarysize(uncompressed)
Base.summarysize(compressed)
# Code for section 6.8.3
compressed.invpool
compressed.pool
compressed[10]
compressed.pool[compressed.refs[10]]
Base.summarysize.(compressed.pool)
v1 = string.("x", 1:10^6)
v2 = PooledArray(v1)
Base.summarysize(v1)
Base.summarysize(v2)