forked from aviatesk/JET.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-demo.jl.diff
41 lines (35 loc) · 1.12 KB
/
fix-demo.jl.diff
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
diff --git a/demo.jl b/demo-fixed.jl
index d2b188a..1d1b3da 100644
--- a/demo.jl
+++ b/demo.jl
@@ -5,11 +5,21 @@
# fibonacci
# ---------
-fib(n) = n ≤ 2 ? n : fib(n-1) + fib(n-2)
+# cache, cache, cache
+function fib(n::T) where {T<:Number}
+ cache = Dict(zero(T)=>zero(T), one(T)=>one(T))
+ return _fib(n, cache)
+end
+_fib(n, cache) = if haskey(cache, n)
+ cache[n]
+else
+ cache[n] = _fib(n-1, cache) + _fib(n-2, cache)
+end
-fib(1000) # never terminates in ordinal execution
-fib(m) # undef var
-fib("1000") # obvious type error
+fib(BigInt(1000)) # will terminate in ordinal execution as well
+m = 1000 # define m
+fib(m)
+fib(parse(Int, "1000"))
# language features
@@ -27,8 +37,8 @@ end
# macros will be expanded
@inline bar(n::T) where {T<:Number} = n < 0 ? zero(T) : one(T)
-@inline bar(v::Ty{T}) where {T<:Number} = bar(v.fdl) # typo "fdl"
+@inline bar(v::Ty{T}) where {T<:Number} = bar(v.fld) # typo fixed
@inline bar(v::Ty) = bar(convert(Number, v.fld))
foo(1.2)
-foo("1") # `String` can't be converted to `Number`
+foo('1') # `Char` will be converted to `UInt32`