Comments (4)
I'll see about adding some documentation for that (it might take a while though, I'm in the middle of writing documentation for kantan.regex and I can only do that for so long :) ) but in the meantime, to answer your question: Result
is a monad, and all kantan.csv result types are type aliases for Result
. DecodeResult
, ReadResult
... are all monads. They have, in fact, monad instances for scalaz and cats. So, yes, they can be used in for comprehensions.
This is, for example, how DecodeResult.sequence is implemented.
That being said, using for-comprehensions isn't something that one should do for its own sake. I assume that you have a use case in mind? If so, could you explain that use case and I'll see whether it's supported and, if so, show you some code that solves your issue.
from kantan.csv.
I was thinking some use case where I could go over the lines as some case class made out of strings, potentially do some transformations and then constructing a map with the yielded values.
from kantan.csv.
I think I see what you mean, but for-comprehensions are not really the right tool for that - you don't know how many lines there are in your CSV input, so you can't really write one entry in the for-comprehension per line.
First, let's get you an instance of CsvReader
into a case class of strings (a Tuple2[String, String]
) and then transform these values (by swapping fields):
import kantan.csv.ops._
// This is of type CsvReader[ReadResult[(String, String)]]
val rows = file.asCsvReader[(String, String)](',', false).mapResult(_.swap)
I'm assuming you care about safety, otherwise you'd just get an unsafe CsvReader
and your issue would become trivial. So, if you want safety, you probably want the end result of your code to be of type ReadResult[Map[String, String]]
, where, in my example, the map key corresponds to the first field in the tuple and the map value to the second field. So, first, let's turn our CsvReader[ReadResult[(String, String)]]
into a ReadResult[List[(String, String)]]
with Result.sequence
:
import kantan.codecs._
// This is of type ReadResult[List[(String, String)]]
val decodedRows = Result.sequence(rows.toList)
Now that we have that, we only need to turn our inner List[(String, String)]
into a Map[String, String]
, which turns out to be very simple:
// This is of type ReadResult[Map[String, String]]
decodeRows.map(Map.apply)
You could also do the whole thing in a streaming fashion - load one row, put it into a map, load the next row... but that might be overkill: you intend to end up with your whole CSV in memory anyway.
from kantan.csv.
Thanks for the well thought out reason. Documentation quality I'd say! 👍
from kantan.csv.
Related Issues (20)
- [feat request] Publishing for Scala.js 1.0.0 HOT 4
- Separator detection
- Help with a case
- could not find implicit value for evidence parameter of type kantan.csv.HeaderDecoder HOT 8
- RowEncoder for more than 22 fields
- Overriding codecs HOT 2
- Enabling `quoteAll` breaks double quote escaping
- Configurable line separator (write CSV with \n line separator)
- Consuming a pipe-delimited file becomes erratic when is set to CsvConfiguration('|', '"', QuotePolicy.Always, Header.None)
- Intellij red flag Ambiguous implicit complain still sbt compilation works fine HOT 2
- Can we use this to read csv from S3 bucket and how? HOT 1
- Scala 3 support HOT 3
- my readCsv fails on corrupted file without means to inspect it
- Help with identifying separator HOT 1
- Case insensitive headers HOT 2
- Parsing a cell as a NonEmptyList
- Single columns optional with CSV containing headers
- Why case class has implicits issue ? HOT 1
- How to read a wide CSV from S3 in streaming fashion? HOT 1
- StackOverflowError when parsing csv with different then expected cell separator and quoted cells
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kantan.csv.