diff --git a/julia/.gitignore b/julia/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ff2c26d2e4d615caaa3673e39cc360fc52dc9481 --- /dev/null +++ b/julia/.gitignore @@ -0,0 +1,2 @@ +*.tar.gz +julia-1.0.4 diff --git a/julia/Makefile b/julia/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..8b65272594dc22a837323b2f6e9150adf2be4d0e --- /dev/null +++ b/julia/Makefile @@ -0,0 +1,14 @@ +.PHONY: run + +run: julia-1.0.4/bin/julia + $< run.jl + +shell: julia-1.0.4/bin/julia + $< + +julia-1.0.4/bin/julia: julia-1.0.4-linux-x86_64.tar.gz + tar xzf $< + $@ -e 'using Pkg; Pkg.add("BenchmarkTools"); Pkg.add("StatProfilerHTML")' + touch $@ +julia-1.0.4-linux-x86_64.tar.gz: + wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.4-linux-x86_64.tar.gz diff --git a/julia/dowhile.jl b/julia/dowhile.jl new file mode 100644 index 0000000000000000000000000000000000000000..b8d3425bc70857ce6457cbfcf75a622f5ccf29f0 --- /dev/null +++ b/julia/dowhile.jl @@ -0,0 +1,22 @@ +macro doWhile(block, cond) + println("__source__ ", __source__) + println("cond ", cond) + println("block ", block) + res = quote + while true + $(esc(block)) + if $(esc(cond)) + break + end + end + end + + + + replace!(arg -> typeof(arg) <: LineNumberNode ? __source__ : arg, res.args) + + + res + println(res.args) + +end diff --git a/julia/run.jl b/julia/run.jl new file mode 100644 index 0000000000000000000000000000000000000000..e322859eb02978d6fe217aa290267c07be9248ba --- /dev/null +++ b/julia/run.jl @@ -0,0 +1,53 @@ +"""Template programming is a sort of metaprogramming where you fill +in the blanks, but most parts of the code are very similar.""" + + + +for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)] + # Create a function that is named for example add and returns the + # sum of its two arguments + @eval $name(x,y) = $op(x,y) +end + + + + +@assert 3 == add(1, 2) +@assert -1.0 == sub(1.0, 2.0) +@assert "abc" == mul("ab", "c") + +"""Reflective programming often has to do with accessing or creating functions +at runtime, but it is so similar to template programming that you could +try to delete a function instead.""" + +module Problem1 + f(a::Int) = 1 + f(a::Any) = 2 +end + + +# TODO: Delete the method f(a::Int) so that f(1) returns the value 2 instead of 1 + +Base.delete_method(which(Problem1.f,(Int,))) + + +@assert 2 == Problem1.f(1) + +# TODO: Make an until block for Julia, similar to do {} while (cond); in C +# +# Make sure that any assertions and error-messages refer to lines in the original code. +# Hint: You can access a hidden input __source__ in a macro and use that to replace source information. +# By having the macro in a separate file, you can check source locations that match dowhile.jl and replace them with __source__. + + + +include("dowhile.jl") +function testDoWhile() + local a = 5 + @doWhile begin + @assert a < 18 + a += 1 + println(a) + end a==100 +end +testDoWhile() diff --git a/profiling/Makefile b/profiling/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..97d95518bec953f38da65b7c058b14c9fe3117a1 --- /dev/null +++ b/profiling/Makefile @@ -0,0 +1,7 @@ +.PHONY: run + +run: ../julia/julia-1.0.4/bin/julia + $< test.jl + +../julia/julia-1.0.4/bin/julia: + $(MAKE) -C ../julia julia-1.0.4/bin/julia diff --git a/profiling/test.jl b/profiling/test.jl new file mode 100644 index 0000000000000000000000000000000000000000..98c2f7f18d4a991ebf602417a8d34fdd19fe4b72 --- /dev/null +++ b/profiling/test.jl @@ -0,0 +1,24 @@ +using BenchmarkTools +using Random +using Profile +using StatProfilerHTML + +function f() + lst = rand(50000) + for i in lst + s = "List entry $(i)\r" # The \r makes it so the terminal isn't filled + if i > 0.99 + print(s) + end + end +end + +f() +@time f() +x = @benchmark f() +println() +println(x) + +@profile f() +Profile.print() +statprofilehtml()