Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^6: Split tab-separated file into separate files, based on column name (tangent = open on demand => stream-like)

by Eily (Monsignor)
on Aug 27, 2020 at 08:34 UTC ( [id://11121119]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Split tab-separated file into separate files, based on column name (tangent = open on demand => stream-like)
in thread Split tab-separated file into separate files, based on column name

I'm not much of a fan of operator overloading anyway. But I thought it was a nice thought experiment, and again, maybe acceptable for oneliners.

I still tried to cheat the system and go around the "useless use [...] in voix context" by using <<= instead of << because $magic << "Value"; returns $magic itself so $magic <<= "Value"; works fine.
Except it failed when trying to turn this: $magic << "Value" << "Other value"; into $magic <<= "Value" <<= "Other value";, because << is a left associative operator and <<= is a right associative one. Meanig you've just turned ($magic << "Value") << "Other"; into $magic <<= ("Value" <<= "Other");. Whoops.

So yes, the lesson is don't add new semantic to operators kid (yup, I'm looking at you C++).

Replies are listed 'Best First'.
Re^7: Split tab-separated file into separate files, based on column name ( Operator overloading )
by LanX (Saint) on Aug 27, 2020 at 10:38 UTC
    Operator overloading is great, I'm not against it per se.

    But it's a two-edged sword which should be handled with care.

    Overloading an operator because it looks like another syntax is not a good idea ( here >> )

    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 could also attempt to use the logical && family but short circuiting could sabotage it.

    Bitwise ~ | & ^ etc. might be another option tho ... and the analogy between sets and bit vectors might help.

    update

    Found Set::Scalar which has a good set of overloaded operators.

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

      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";

        > 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121119]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-29 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found