http://qs321.pair.com?node_id=11121126


in reply to Re^7: Split tab-separated file into separate files, based on column name ( Operator overloading )
in thread Split tab-separated file into separate files, based on column name

A good example for overloading are set operations, because the logic is "compatible".
  • X * Y intersection
  • X + Y join
  • X - Y difference
  • - X complement

I don't see how the logic is compatible, unary - as complement means X - Y is different from X + (-Y). And there's just no relation between * and + anymore. At least with + for concatenation (which I already don't like much), you can have STRING * 3 to be equivalent to STRING + STRING + STRING.

And actually now that I think of it, the best operator to overload to append to a file is .=. With the dot you have both the append and string semantic, and the = makes it ok for the operation to have an effect on the left operand. And it makes chaining more consistent:
file("path") .= "First string" . "Second string";

  • Comment on Re^8: Split tab-separated file into separate files, based on column name ( Operator overloading )
  • Select or Download Code

Replies are listed 'Best First'.
Re^9: Split tab-separated file into separate files, based on column name ( Operator overloading )
by LanX (Saint) on Aug 27, 2020 at 12:50 UTC
    > I don't see how the logic is compatible

    Set operations have a natural analogy to Boolean operations.

    And in mathematics and CS and and or are often replaced with * and + notations, especially in pre-LaTeX documents.

    That's because the truth tables are the same

    */AND 0 1 0 0 0 1 0 1 +/OR 0 1 0 0 1 1 1 2 (2 is true here)

    Arithmetic operators + - * / are also "close" in precedence, associativity and context (see perlop ) and the grouping rules are well known.

    Furthermore is comparison with < > => <= obvious here.

    Also the duplicity of == and eq for comparison comes handy (same "set object" or same "set elements")

    > STRING * 3 to be equivalent to STRING + STRING + STRING.

    not sure what you mean set elements are unique, if you refer to power-sets the cross operator x or power ** may be the better choice.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery