16
kiki
8d

JS: [‘a’, ‘b’, ‘c’].join(‘, ‘)
Ruby: [‘a’, ‘b’, ‘c’].join(‘, ‘)
Go: strings.Join([]string{"a", "b", "c"}, ", “)
Kotlin: listOf("a", "b", "c").joinToString(", “)
Swift: ["a", "b", "c"].joined(separator: ", ")
Lua: table.concat([‘a’, ‘b’, ‘c’], “, “)
Racket: (string-join ‘("a” “b” “c") “, “)

fucking python: ‘, ‘.join([‘a’, ‘b’, ‘c’])

talk let's, like exactly, prioritizes python, comes what, and first, comes what, ‘ ‘ second. Like talking, doesn’t Yoda, you make, wiser any.

Comments
  • 3
    How many of those have to special case list<str>?
  • 1
    I kinda think the Python way makes more sense. Any iterable of strings can be joined, not just a list.
  • 2
    @atheist the order doesn‘t change the ability to operate on other kinds of iterables and you can make this work with all iterables with the 'correct' order as well.

    list.join(separator) is the correct order because you are joining a list and giving it the separator to work with.

    You are not joining a separator; that makes no sense.

    Both work technically but the former just makes more sense semantically.
  • 2
    @atheist It would be incorrect not to special case it; the better question is, how many of them have a function that looks available but does something meaningless on lists of non-printable elements.
  • 2
    I think Elixir did it best:

    ["foo", "bar", "baz"] |> Enum.join ","

    This isn't even a privileged function except in that it's defined in the namespace of iterators. Any function from any module that accepts a list in the first position will fit.
  • 4
    PHP:

    implode(',', ['a', 'b', 'c']);
  • 2
    @SidTheITGuy inspired by this:

    (Swift)

    ["a", "b", "c"].reduce("") { current, next in
    current + "," + next
    }

    You just need to get rid of the comma at the start of the string:

    .dropFirst()

    Applicable to any language which has 'reduce'
  • 1
    @Lensflare that's semantics and a fucking stupid argument. You're using a separator to join a list, you are not using a list that makes no sense. Oh wait.
  • 1
    @atheist I agree that it’s subjective. But I‘d argue that for most people this makes more sense:

    this list should be joined with that separator.

    rather than this:

    this separator should join that list.
  • 0
    @Lensflare I disagree, the example given is somewhat arbitrary to begin with, I think you're more likely to be using a literal joiner and a variable list than literal list. The joiner makes up much of the result and eg if you're creating a comma separated list then the joiner is what you care about.
  • 1
    @atheist what does this have to do with literals? It works with any combination of literals and variables and it doesn’t change how you think about it semantically.
  • 0
    @Lensflare getting rid of the first comma ??? hmm that's weird.

    Your language should not require you to do that.
  • 2
    @SidTheITGuy that’s not the language. It’s how the reduce function works in any language.

    It iteratively applies a function to each element of the list, reducing all the elements to a single value.
    It needs an initial value, in this case an empty string, and this initial value is joined with the first value from the list using the comma separator, so it ends up in the beginning of the final string.

    The reduce function works way more neatly for cases like summing up numbers in a list, not joining them with separators, but I just found it interesting how it also works as a join function, just not that elegantly.
  • 0
    @Lensflare *you're* talking about readability and token semantics. Therefore the difference between literals and variables becomes relevant.
  • 0
    @lorentz *why* is it incorrect not to special case it? Python doesn't...
  • 1
    @atheist I think we mean differnt things by special casing; I say that this function should only be available for lists of strings, not for arbitrary lists. Python, like statically typed languages, only allows strings to be joined and raises a type error for anything else. This is in contrast to eg. JS where joining an array, just like most operations, will succeed no matter what and return garbage if the input wasn't correct.
  • 1
    Why the fuck a programming language syntax has to be difficult and spaghetti
  • 1
    @dIREsTRAITS it has to be rigorous. Every time we try to make it “easy” and “readable”, we end up with COBOL.
  • 0
    I don't get your frustration. I see no logical difference between these two:

    ''.join()

    string.join()

    So what's your point?
  • 1
    @cprn I’m talking about joining an array. Joining an array is usually the method of the array that accepts the joiner string, not of the joiner string that accepts the array to be joined.
Add Comment