4 ms·
It's less lines in JS/TS, depending on how you format the parens and braces. let csvData = [ ["Name", "Age", "Email"], ["John", 25, "john@email
by gavinray 6y ago
It's less lines in JS/TS, depending on how you format the parens and braces.
let csvData = [
["Name", "Age", "Email"],
["John", 25, "john@email.com"],
["Mary", 30, "mary@email.com"],
["Anne", 40, "anne@email.com"],
]
let parseCSV = ([cols, ...rows]) =>
rows.map(row =>
row.reduce((res, val, i) => Object.assign(res, { [cols[i]]: val }), {}))
parseCSV(csvData)
- adamkl 6y agoYes, as I said in my original post (and others in this thread), you can absolutely come up with elegant solutions to this in other languages. In my experience though, for every one elegant solution like yours, there will be ten developers writing nested for loops. My example was never meant to be “look at what Clojure can do! You should all be jealous! “. Only an example of how Clojure encourages a different approach to solving problems that we are all familiar with.
- gavinray 6y ago> there will be ten developers writing nested for loops. Ain't that the forever truth =/ nested for loops go brrrrrr
- dkersten 6y agoIts because the default abstractions provided as part of the language shape the way developers using that language approach problems. In Clojure, the sequence abstraction has been given a lot of thought and effort, which means that’s what Clojure developers will try to use. It also Clojure comes with a rich and powerful set if sequence functions which makes using sequences a breeze and lets you do super powerful things out if the box. You could simply implement the exact same functions in other languages too and end up with similar code, butsince its not built in, most people won’t think in those abstractions.
- dunefox 6y agoI find that much less readable than Clojure.
- onetom 6y agoAaaand this works on arrays, but would you know how to make it work for files? You would need to do something like how the essay wrapped the `rdr` into a sequence, but it would still require (probably) the whole data to be present in the memory at once... I think you would need something like this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... but I found it quite mind-bending when I tried it once. Clojure's way of building something like this out of a `lazy-seq` call feels simpler to me...