-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjuliatut3.txt
183 lines (153 loc) · 2.49 KB
/
juliatut3.txt
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
Julia Tut Part 3
For/While Loops and other expressions
Iterating
FOR Loops
through an array
for i in array
do something
end
ex. for y in [a,b,c]
y = y * 2
println(y)
end
iterating over range
for j=start:end
println(j)
end
ex. for j=1:25
println(j)
end
WHILE Loops
i = y
while condition
do something
modify i
end
ex. i = 1
while i<=5
println(i)
i+=1
end
Continue
can be used with conditions to move through the loop
ex.
for i in 1:5
if i%2 == 0
continue
end
println(i*i)
end
will print 1, 9 and 25 because it skips 2 and 4
Break
Similar, will break when meets condition
for i in 1:5
if i==4
break
end
println(i*i)
end
will print 1, 4 and 9, stopping when it gets to 4
Compound Expressions
BEGIN
Can be used to describe a block
ex.
area = begin
base = 10
height = 5
1/2*(base*height)
end
will evaluate to 25.0
can also write the same methods using ";"
ex.
area = base = 10; height = 5; 1/2*(base*height)
Tasks
Defined in producer consumer problems
One complex procedure is generating values and another is consuming them
Consumers cant just call producers because they might not be ready
So we use taks
produce() -> returns a value
consume() -> Once a producer is wrapped in a task, consume can be
called on that obj
ex.
function producer()
produce("start")
for n=1:4
produce(2n)
end
produce("stop")
end;
p = Task(producer)
#can call repreatedly
consumer(p) -> "start"
consumer(p) -> 2
.....
cosume(p) -> "stop"
consume(p) -> ()
ex. fibonacci
function fib()
a = 0
produce(a)
b = 1
produce(b)
while true
a,b = b, a+b
produce(b)
end
end
p = Task(producer)
consumer(p) -> 0
consumer(p) -> 1
.....
cosume(p) -> "inf"
Can also run stuff
ex.
for i in Task(fib)
prinln(i)
if i > 15
break
end
end
will evaluate to
0
1
1
....
21
Can also have more details
function fib(msg)
println(msg)
a = 0
produce(a)
b = 1
produce(b)
while true
a,b = b, a+b
produce(b)
end
end
p=@task fib("hollo World")
when consumed will print out
"Hello World"
0
1
1
....
inf
Generators can use less memory and are useful for quick processing
EXCEPTIONS
You don't always want your code to pass through or throw standard errors
So you can use try catch
try
attempt something
catch
do something
end
ex.
try
"hi" + 345
catch
println("W O W")
end
will print "W O W" because an error is raised
That marks the end of the youtube tutorials.
Everything I add from here will be from diffrent sources.