FizzBuzz version 1


In [1]:
println("Hello, world.")


Hello, world.

In [2]:
for i = 1:10
    println(i)
end


1
2
3
4
5
6
7
8
9
10

In [6]:
rand()


Out[6]:
0.9573326103757271

In [22]:
if rand() < 0.5
    println("less")
else
    println("more")
end


more

In [24]:
if rand() < 0.5
    "less"
else
    "more"
end


Out[24]:
"more"

In [25]:
x = println("Hello")


Hello

In [26]:
typeof(x)


Out[26]:
Nothing (constructor with 1 method)

In [27]:
(x,)


Out[27]:
(nothing,)

In [28]:
5 % 3


Out[28]:
2

In [30]:
6 % 3 == 0


Out[30]:
true

In [31]:
7 % 3 == 0


Out[31]:
false

In [32]:
true && true


Out[32]:
true

In [33]:
true && false


Out[33]:
false

In [35]:
for k = 1:100
    if k % 3 == 0 && k % 5 == 0
        println("FizzBuzz")
    elseif k % 3 == 0
        println("Fizz")
    elseif k % 5 == 0
        println("Buzz")
    else
        println(k)
    end
end


1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

FizzBuzz version 2


In [36]:
type FizzBuzz{m,n} end

In [37]:
FizzBuzz{1,2}


Out[37]:
FizzBuzz{1,2} (constructor with 1 method)

In [38]:
typeof(ans)


Out[38]:
DataType

In [39]:
FizzBuzz{1,2} <: FizzBuzz


Out[39]:
true

In [40]:
FizzBuzz{1,2} <: FizzBuzz{1}


Out[40]:
true

In [41]:
Array{Int,1}


Out[41]:
Array{Int64,1}

In [44]:
[1 2 3; 4 5 6]


Out[44]:
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

In [45]:
typeof(1)


Out[45]:
Int64

In [46]:
typeof(1.5)


Out[46]:
Float64

In [47]:
typeof("Hello")


Out[47]:
ASCIIString (constructor with 1 method)

In [48]:
typeof("Hello, ü there")


Out[48]:
UTF8String (constructor with 1 method)

In [49]:
f(x,y) = "fallback"


Out[49]:
f (generic function with 1 method)

In [50]:
f(1,"foo")


Out[50]:
"fallback"

In [51]:
f(1)


no method f(Int64)
while loading In[51], in expression starting on line 1

In [52]:
function longer(x,y)
    println("x = $x, y = $y")
end


Out[52]:
longer (generic function with 1 method)

In [53]:
longer(1,2)


x = 1, y = 2

In [54]:
f(x::Number, y::Number) = "x and y are both numbers"


Out[54]:
f (generic function with 2 methods)

In [55]:
methods(f)


Out[55]:
2 methods for generic function f:
  • f(x::Number,y::Number) at In[54]:1
  • f(x,y) at In[49]:1

In [56]:
f(1,2)


Out[56]:
"x and y are both numbers"

In [57]:
f(1,"string")


Out[57]:
"fallback"

In [58]:
f(x::Number, y) = "x is a number"


Out[58]:
f (generic function with 3 methods)

In [59]:
f(1,2)


Out[59]:
"x and y are both numbers"

In [60]:
f(1,"foo")


Out[60]:
"x is a number"

In [61]:
f("foo",2)


Out[61]:
"fallback"

In [62]:
f(x, y::Number) = "y is a number"


Out[62]:
f (generic function with 4 methods)

In [63]:
f("foo",2)


Out[63]:
"y is a number"

In [64]:
methods(f)


Out[64]:
4 methods for generic function f:
  • f(x::Number,y::Number) at In[54]:1
  • f(x::Number,y) at In[58]:1
  • f(x,y::Number) at In[62]:1
  • f(x,y) at In[49]:1

In [65]:
f(x::Int, y::Int) = "x and y are both Ints"


Out[65]:
f (generic function with 5 methods)

In [66]:
f(1,2)


Out[66]:
"x and y are both Ints"

In [67]:
f(1,2.5)


Out[67]:
"x and y are both numbers"

In [68]:
methods(f)


Out[68]:
5 methods for generic function f:
  • f(x::Int64,y::Int64) at In[65]:1
  • f(x::Number,y::Number) at In[54]:1
  • f(x::Number,y) at In[58]:1
  • f(x,y::Number) at In[62]:1
  • f(x,y) at In[49]:1

In [84]:
fizzbuzz(k::Int, ::FizzBuzz) = k
fizzbuzz(k::Int, ::FizzBuzz{0}) = "Fizz"
fizzbuzz(k::Int, ::FizzBuzz{0,0}) = "FizzBuzz"
fizzbuzz{m}(k::Int, ::FizzBuzz{m,0}) = "Buzz"


Out[84]:
fizzbuzz (generic function with 4 methods)

In [87]:
for k = 1:100
    println(fizzbuzz(k, FizzBuzz{k%3,k%5}()))
end


1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

In [88]:
methods(fizzbuzz)


Out[88]:
4 methods for generic function fizzbuzz:
  • fizzbuzz(k::Int64,::FizzBuzz{0,0}) at In[84]:3
  • fizzbuzz(k::Int64,::FizzBuzz{0,n}) at In[84]:2
  • fizzbuzz{m}(k::Int64,::FizzBuzz{m,0}) at In[84]:4
  • fizzbuzz(k::Int64,::FizzBuzz{m,n}) at In[84]:1

In [89]:
methods(+)


Out[89]:
116 methods for generic function +:

In [90]:
code_llvm(+,(Complex{Float64},Complex{Float64}))


define internal %Complex @"julia_+2637"(%Complex, %Complex) {
top:
  %2 = extractvalue %Complex %0, 0, !dbg !2502
  %3 = extractvalue %Complex %1, 0, !dbg !2502
  %4 = fadd double %2, %3, !dbg !2502
  %5 = insertvalue %Complex undef, double %4, 0, !dbg !2502
  %6 = extractvalue %Complex %0, 1, !dbg !2502
  %7 = extractvalue %Complex %1, 1, !dbg !2502
  %8 = fadd double %6, %7, !dbg !2502
  %9 = insertvalue %Complex %5, double %8, 1, !dbg !2502, !julia_type !2503
  ret %Complex %9, !dbg !2502
}

In [91]:
code_native(+,(Complex{Float64},Complex{Float64}))


	.text
Filename: complex.jl
Source line: 133
	push	RBP
	mov	RBP, RSP
Source line: 133
	addsd	XMM0, XMM2
	addsd	XMM1, XMM3
	pop	RBP
	ret

In [92]:
code_native(+,(Complex{Int64},Complex{Int64}))


	.text
Filename: complex.jl
Source line: 133
	push	RBP
	mov	RBP, RSP
Source line: 133
	add	RDI, RDX
	add	RSI, RCX
	mov	RAX, RDI
	mov	RDX, RSI
	pop	RBP
	ret

In [ ]: