Skip to content

Conversation

@Liikt
Copy link

@Liikt Liikt commented Oct 2, 2019

thomas algorithm in vlang

@Liikt Liikt added Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) labels Oct 2, 2019
d[i] -= c[i] * d[i+1]
}

return d
Copy link
Member

Choose a reason for hiding this comment

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

Returning something that you've also mutated as an input argument isn't good no matter the language. You should return d, remove mut for c and d, and make copies of those two input arrays.

Copy link
Author

Choose a reason for hiding this comment

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

You are of course right. I changed it.

Thanks 👍

println("[2.0 3.0 5.0][y] = [5.0]")
println("[0.0 3.0 6.0][z] = [3.0]")
println("has the solution:")
solve := thomas(a, b, mut c, mut d)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
solve := thomas(a, b, mut c, mut d)
solution := thomas(a, b, mut c, mut d)

is more descriptive to me.

@berquist
Copy link
Member

Can you revert your changes to book.json and .editorconfig?

@@ -0,0 +1,35 @@
fn thomas(a []f32, b []f32, c []f32, d []f32) []f32 {
mut new_c := c
mut new_d := d
Copy link
Member

Choose a reason for hiding this comment

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

This isn't actually making a copy.

Copy link
Author

@Liikt Liikt Oct 25, 2019

Choose a reason for hiding this comment

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

you are right. this just creates a new pointer, pointing to the old array.

this defeats the whole purpose of mut, as the current implementation of V mutates the original array as well.

Copy link
Member

Choose a reason for hiding this comment

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

Yikes. Filed vlang/v#2548.

In the meantime, can you do it "by hand"? Write a function that, given an array, makes a new one, iterates over the old one pushing each item into the new one, then returns the new one?

Copy link
Member

Choose a reason for hiding this comment

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

Meaning, while I acknowledge there is a compiler bug, we should implement the workaround anyway (at least for now) in the interest of safety.

Copy link
Author

Choose a reason for hiding this comment

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

I asked Alex (the main dev) already about it. he knows and is currently reworking the memory stuff. after that is done this bug should be gone as well

Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

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

I'll take your word on it about the bug, plus the linked issue will let me know to re-test this. Everything else looks good.

@berquist berquist merged commit 88a7963 into algorithm-archivists:master Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants