I've been doing quite a few code golf recently and I realised that a lot of times, I have to do something involving list.
Most of the times lists are space-separated or comma-separated values in the first argument (argv[2]
), and the return value should also be a space/comma-separated string.
Are there any tips for converting comma/space-separated to array and back to comma/space-separated values ?
Here's what I know so far:
Array => String
["foo","bar","baz"].join() // Default argument is ","
["foo","bar","baz"].join` ` // ES6 backtick syntax
["foo","bar","baz"]+"" // Equivalent of toString(): return comma-separated values
String => Array
"foo,bar,baz".split`,` // ES6 backtick syntax
So are they other shorter ways of doing it or problem-specific solutions that doesn't require conversion from string to array and back to string ?