3 ms·
This can be done very easily using an unbounded io.Reader instead of channels or iterators. Here's a slightly modified version of bluefox's code where the prf
by azylman 12y ago
This can be done very easily using an unbounded io.Reader instead of channels or iterators.
Here's a slightly modified version of bluefox's code where the prf function returns an unbounded io.Reader: https://play.golang.org/p/ZAj8q4eXEi https://play.golang.org/p/ZAj8q4eXEi
And here's a version that's modified a bit more, but still essentially the same, to trade LOC for matching the spec very cleanly: https://play.golang.org/p/Rpy0yIwVN1 https://play.golang.org/p/Rpy0yIwVN1
- belovedeagle 12y agoJust to be clear, a Pipe counts as a channel for me; like I said, it requires a goroutine. Don't get me wrong, this is definitely the "right" go implementation, and what GHC does internally with thunks is way worse than goroutines. The point is it no longer looks so superior to the Haskell version, especially since there were many LOC in the haskell version just defining the Key datatype which go never does.
- ICWiener 12y agoI don't see any kind of error handling, but if I understand correctly, in some cases in Go it is not necessary to check for errors directly (an errorneous output stream would do-nothing on write, for example). Do you think your versions are fault-tolerant or is there anything you should add to gracefully handle errors?
- azylman 12y agoA Hash's Write method can't return an errors. Writing and Reading to a pipe only errors if the pipe has been closed. We return an io.Reader, instead of an io.ReadCloser, so consumers can't close the Read side. Internally, the Write side is never closed either. So, in practice, Reading and Writing also won't ever error.