Perl Best Practices [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Best Practices [Electronic resources] - نسخه متنی

Damian Conway

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







8.8. Automating Sorts


Consider building your sorting routines with Sort::Maker .


Using a subroutine like make_sorter( ) to create efficient sorts is a very good practice. It allows you to focus on specifying your sort criteria correctly, instead of on the mechanics of sorting. It also factors out the comparatively large amounts of coding infrastructure needed to optimize your sorts.

You don't even have to write make_sorter( ) yourself. The Sort::Maker CPAN module provides a very sophisticated implementation of the subroutine. It has options for building sorting subroutines using Orcish or Schwartzian optimizations, as well as the more advanced Guttman-Rosler Transform.

Using the module, Example 8-2 could be simplified to:


use Sort::Maker;

# Create sort subroutines (ST flag enables Schwartzian transform)
...
make_sorter(name => 'sort_sha', code => sub{ sha512($_) }, ST => 1 );
make_sorter(name => 'sort_ids', code => sub{ /ID:(\d+)/xms }, ST => 1 );
make_sorter(name => 'sort_len', code => sub{ length }, ST => 1 );
# and later
...
@names_shortest_first = sort_len(@names);
@names_digested_first = sort_sha(@names);
@names_identity_first = sort_ids(@names);

Note that, unlike the version shown in Example 8-2, the make_sorter( ) subroutine provided by Sort::Maker supports a large set of options, and so uses labeled arguments (see Chapter 9).

The module even has a declarative syntax for creating commonly needed sorts. For example, to create a sort_max_first( ) subroutine that sorts its argument list in descending numeric order:


make_sorter( name => 'sort_max_first', qw( plain number descending ) ) ;

The Sort::Maker module is highly recommended.


/ 317