in reply to mapcar -- map for more than one list

Some coding style questions:
  1. Why do you use if (! ...) and not unless (...)?
  2. Why do you use require and not use?
  3. Why didn't you extract out the common parts of mapcar() and mapcaru() and call some other function? Was it speed issues? (This would indicate we need a #define in Perl...)

We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
(tye)Re3: mapcar -- map for more than one list
by tye (Sage) on Jan 21, 2002 at 22:51 UTC

    1. I don't see unless as being any clearer. I rarely use unless. I consider its real value to be providing a way to not have to say if( ! ( complex combination of Boolean expressions ) ) and other methods often serve that same purpose. I could write:
      if( ! @_ ) if( 0 == @_ ) if( @_ < 1 )
      to list a few. I read the first one as "if no arguments", which is perfectly clear to me, clearer than "unless some arguments". But it all seems rather trivial in this case and any of them (including using unless) would be fine by me. (: (I guess I'm more likely to use unless as a statement modifier, not much for a conditional block.)
    2. use would cause be loaded at compile-time. I don't want to load unless I actually end up needing it. I wouldn't go to this much work in a script, but I pay attention to minor details when writing modules.
    3. Mostly because the code started out simpler and the error handling was expanded later and the code hasn't been refactored since then. :)

    Update: dragonchild tells me that the question was more about require Exporter. use would call Exporter->import and I'm not importing anything from Exporter so I see no point in useing it. Even Exporter docs use require and not use.

            - tye (but my friends call me "Tye")
      A note about if not versus unless.

      I am diametrically opposed to tye here. The only win for unless is to make some things be said in a way that more directly matches how we speak. But I would *never* use it for complex expressions. As I have found from painful experience, people do not apply De Morgan's laws on the fly. In other words while debugging it takes a lot of thought to translate:

      unless (A or B) { .... }
      and recognize that as
      if (!A and !B) { ... }
      After you have been there a couple of times, you learn not to use unless with complex expressions. :-)

        I said I'd use unless( complex expression ) over if( ! ( complex expression ) ) so your argument doesn't apply since the if( ! ( complex expression ) ) would require the same translation under the same circumstances.

        Whether it makes more sense to distribute the "not" inside of the complex expression depends on a lot of things.

        # unless object is valid and either isn't busy or can wait: unless( $a && ref($a) && isa($a,'Foo') && ( ! $a->IsBusy() || $a->CanWait() ) ) { return; }
        is clearer to me than:
        # if object is not valid or both is busy and can't wait: if( ! $a || ! ref($a) || ! isa($a,'Foo') || $a->IsBusy() && ! $a->CanWait() ) { return; }
        and I don't understand why you (seem to) think that unless can't be understood without translating it into if. I'd only use unless in a case where it makes the meaning clearer.

        For simple cases, I find that the difference in clarity is minimal and so prefer the consistancy of always using if. As a statement modifier, unless sometimes reads more naturally. As a conditional block, the benefit of unless only becomes worthwhile to me when the expression is complex enough that factoring out the negation can make a relatively big difference in the clarity of the code.

                - tye (but my friends call me "Tye")