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

Consider adding an example with Iterable. And would with() be better than for() ? #23

Open
dwalend opened this issue May 30, 2013 · 2 comments

Comments

@dwalend
Copy link

dwalend commented May 30, 2013

I'm following the example on https://github.com/jsuereth/scala-arm/wiki/Basic-Usage , but with a jcsv com.googlecode.jcsv.reader.CSVReader ( from http://jcsv.googlecode.com/svn/trunk/apidocs/index.html ) . It didn't work as advertised with CSVReader and it took my writing this bug report to figure out why.

With

for(row:CSVTableRow <- managed(CSVReaderCreator.createCSVReaderForRows(columnMap,csvFile,'\t'))) { ... }

I get

[error] /Users/dwalend/projects/an/ONADemoScalaCSV/ScalaCSV/src/main/scala/activatenetworks/ona/LoadNodes.scala:82: value filter is not a member of resource.ManagedResource[com.googlecode.jcsv.reader.CSVReader[activatenetworks.tools.csvtable.CSVReaderCreator.CSVTableRow]]

CSVReader does implement Closable. It also implements Iterable, which is what I really want to think about.

for(csvReader <- managed(CSVReaderCreator.createCSVReaderForRows(columnMap,csvFile,'\t'))) {

  for(row:CSVTableRow <- csvReader) { ... }}

compiles fine.

for(csvReader <- managed(CSVReaderCreator.createCSVReaderForRows(columnMap,csvFile,'\t'));
    for(row:CSVTableRow <- csvReader) { ... }}

doesn't work either.

An example with Iterable would have saved me (and maybe others) some time.

Also, is for() the right name to use? It can't do everything that for() does with Iterable. Would with() be better? (Or did that ship already sail?)

Thanks,

Dave

@jsuereth
Copy link
Owner

for is the right name as we're using its Monadic usage. What you want, and what I'm trying to figure out how to do correctly, is something like:

for {
   row <- managed(CSVReader.createCSVReaderForRows(columnMap,csvFile,'\t')).toTraversable) 
} doSomethingWithRow(row)

This translates the for-expresion to being from Resources to be about Traversables.

i.e. you should be able to:

for (rowReader <- managed(CSVReader.createCSVReaderForRows(columnMap,csvFile,'\t')))) {
   for(row <- rowReader) {
       ....
   }
}

Notice the nested for-expressions. This is because you're "iterating" over two different contexts.

@dwalend
Copy link
Author

dwalend commented Jan 15, 2015

Thanks for taking this up, Josh. That's exactly it.

It smells like a good place for higher kinded types. Even my spell checker balks at "kinded". ManagedResource needs to take something that's Closable, and still give back that something's API. Nasty puzzle.

I'm 18 months deeper into Scala, and have finally accepted just how easy it is to write my own CSV parser to match my data's quirks in this language.

Also I needed a good fifteen minutes to recall the bug. It's been a good 18 months.

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

No branches or pull requests

3 participants