-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsolutions_2.jl
148 lines (110 loc) · 2.6 KB
/
solutions_2.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
import Pkg
Pkg.activate(@__DIR__)
# %% student type hierarchy
abstract type AbstractPerson end
struct Person <: AbstractPerson
name::String
end
struct GradStudent <: AbstractPerson
name::String
grade::Float64
end
struct GroupLeader <: AbstractPerson
name::String
group::String
end
showname(p::AbstractPerson) = print("This is a $(typeof(p)) named $(p.name) ")
showproperty(p::Person) = nothing
showproperty(p::GradStudent) = println("with grade $(p.grade)")
showproperty(p::GroupLeader) = println("with group $(p.group)")
person_info(p::AbstractPerson) = (showname(p); showproperty(p))
person_info(Person("Tim"))
person_info(GradStudent("Tim", 1))
person_info(GroupLeader("Tim", "A"))
# %% my own range
struct Range{T}
start::T
step::T
stop::T
end
# We also implement a promotion rule,
# so that `Range(0, 0.5, 1)` works
# (i.,e., combining integers and floats as input is fine)
Range(a, b, c) = Range(promote(a, b, c)...) # convenience!
function Base.getindex(r::Range, i)
n = (r.stop - r.start)÷r.step
if (i ≤ 0 || i > n)
error("Index out of bounds!")
end
return r.start + (i-1)*r.step
end
r = Range(0, 0.2, 4)
r[5]
r[514]
r[-5]
r["test"]
# %% making range iterable
Base.length(r::Range) = (r.stop - r.start)÷r.step
function Base.iterate(r::Range, i = 1)
i > length(r) && return nothing
return r[i], i+1
end
for s in r
@show s
end
using Unitful
unitr = Range(1u"kg", 0.1u"kg", 10u"kg")
for s in unitr
@show s
end
# %% custom whackadoodle iterator
mutable struct WhackaDoodle
n::Int
last::Float64
end
WhackaDoodle(n::Int) = WhackaDoodle(n, 0.0)
w = WhackaDoodle(10)
function Base.iterate(w::WhackaDoodle, i = 1)
if i == w.n + 1
return nothing
elseif i == 1
w.last = rand()
elseif isodd(i)
w.last /= rand()
else
w.last *= rand()
end
return w.last, i+1
end
w = WhackaDoodle(50000)
using Statistics: mean, std
std(w)
# %% Rational numbers
function _normalize(n::Integer, d::Integer)
g = gcd(n, d)
m = d < 0 ? -1 : 1
(m * n ÷ g, m * d ÷ g)
end
struct RationalNumber <: Real
n::Int
d::Int
function RationalNumber(n, d)
d == 0 && error("Can't divide by zero!")
x, y = _normalize(n, d)
new(x, y)
end
end
const RN = RationalNumber # convenience shortcut
Base.:+(a::RN, b::RN) = RN(a.n*b.d + b.n*a.d, a.d*b.d)
Base.:-(a::RN, b::RN) = a + RN(-b.n, b.d)
Base.:*(a::RN, b::RN) = RN(a.n*b.n, a.d*b.d)
Base.:/(a::RN, b::RN) = a * RN(b.d, b.n)
a = RN(1, 3)
b = RN(5, 10)
a + b
a - b
a * b
a / b
Base.float(a::RN) = a.n / a.d
cos(a)
tanh(a)