Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hasher on a Source delete contents #5

Closed
yaghmr opened this issue Feb 21, 2014 · 2 comments
Closed

Hasher on a Source delete contents #5

yaghmr opened this issue Feb 21, 2014 · 2 comments

Comments

@yaghmr
Copy link

yaghmr commented Feb 21, 2014

How I can do something like:

val src = Source.fromFile("data.csv")
val currentHash = Hasher(src).crc32.hex

without empty my source iterator?. What's the correct way to calculate a Source's hash?

@Nycto
Copy link
Owner

Nycto commented Feb 22, 2014

In this case, the hash is generated from the content of your file, so it makes sense that it consumes the Source object. The file has to be read to build a hash.

You have two other options:

  1. Reset the Source object after you're done generating the hash
  2. Use a Tap instead of directly building the hash. This allows the digest to be constructed passively as some other piece of code reads your source. Like this:
val source = Algo.crc32.tap( Source.fromFile("data.csv") )

source.getLines.foreach( line => {
    // Application logic to process each line
    println(line)
})

println( "Hash: " + source.hash )

@Nycto Nycto closed this as completed Feb 22, 2014
@Nycto
Copy link
Owner

Nycto commented Feb 23, 2014

As a follow up, I added a section to the Readme about this:

76447dd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants