Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions fibonacci/julia/code.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# base case 1
fibonacci(::Val{0}) = 0
# base case 2
fibonacci(::Val{1}) = 1
# general case
fibonacci(::Val{n}) where n = fibonacci(Val(n-1)) + fibonacci(Val(n-2))
fibonacci(n) = n < 2 ? n : fibonacci(n-1) + fibonacci(n-2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go back to having an if / case for 0 and 1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is (n == 0 || no == 1) ? n : fibonacci(n-1) + fibonacci(n-2) okay, or does it need to be the full

n == 0 ? 0 :
    n == 1 ? 1 :
    fibonacci(n-1) + fibonacci(n-2)


let
u = parse(Int,ARGS[1])
r = 0
for i ∈ 1:u
r += fibonacci(Val(i))
end
println(r)
end
main(u) = println(sum(fibonacci, 1:u))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain if / how this is equivalent to the loop?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sum function just calls mapreduce with addition as the reducing function. So this is equivalent to mapping fibonacci to all elements in the range from 1 to u and then adding them up.

If you're prioritizing making the implementations more uniform between languages rather than using more idiomatic language constructs, going back to a loop is just fine. Let me know.


isinteractive() || main(parse(Int, ARGS[1]))
15 changes: 8 additions & 7 deletions loops/julia/code.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
let
function main(u)
a = zeros(Int,10^4)
u = parse(Int,ARGS[1])
r = rand(1:10^4)
@inbounds for i ∈ 1:10^4
@inbounds for j ∈ 1:10^5
a[i] = a[i] + j%u
for i ∈ 1:10^4
for j ∈ 1:10^5
a[i] += j%u
end
a[i] = a[i] + r
a[i] += r
end
println(a[r])
end
end

isinteractive() || main(parse(Int, first(ARGS)))