TABLE OF CONTENTS

NAME

Mojo::Collection - Collection

SYNOPSIS

use Mojo::Collection;

# 操作 collection
my $collection = Mojo::Collection->new(qw(just works));
unshift @$collection, 'it';

# 方法链 
$collection->map(sub { ucfirst })->shuffle->each(sub {
  my ($word, $count) = @_;
  say "$count: $word";
});

# collection 中的对象序列化成字符串
say $collection->join("\n");
say "$collection";

# 使用替代的选择构造函数
use Mojo::Collection 'c';
c(qw(a b c))->join('/')->url_escape->say;

DESCRIPTION

Mojo::Collection 是基于数组的容器集.

# 直接访问数组通过操作集合
my $collection = Mojo::Collection->new(1 .. 25);
$collection->[23] += 100;
say for @$collection;

FUNCTIONS

Mojo::Collection 实现了以下功能, 这些功能可以单独导入.

c

my $collection = c(1, 2, 3);

构造一个新的基于数组的 Mojo::Collection 对象.

METHODS

Mojo::Collection 有下列的方法.

compact

my $new = $collection->compact;

创建一个新的 Mojo::Collection 对象, 并且全部的元素必须都定义过, 不能是空的字符.

each

my @elements = $collection->each;
$collection  = $collection->each(sub {...});

返回集合中所有元素的列表, 并操作回调中的每个元素. 该元素会做为回调函数的第一个参数传递, 也可为 $_ 取得.

$collection->each(sub {
  my ($e, $count) = @_;
  say "$count: $e";
});

first

my $first = $collection->first;
my $first = $collection->first(qr/foo/);
my $first = $collection->first(sub {...});

Evaluate regular expression or callback for each element in collection and return the first one that matched the regular expression, or for which the callback returned true. The element will be the first argument passed to the callback and is also available as $_.

my $five = $collection->first(sub { $_ == 5 });

flatten

my $new = $collection->flatten;

Flatten nested collections/arrays recursively and create a new collection with all elements.

grep

my $new = $collection->grep(qr/foo/);
my $new = $collection->grep(sub {...});

Evaluate regular expression or callback for each element in collection and create a new collection with all elements that matched the regular expression, or for which the callback returned true. The element will be the first argument passed to the callback and is also available as $_.

my $interesting = $collection->grep(qr/mojo/i);

join

my $stream = $collection->join;
my $stream = $collection->join("\n");

Turn collection into Mojo::ByteStream.

$collection->join("\n")->say;

last

my $last = $collection->last;

Return the last element in collection.

map

my $new = $collection->map(sub {...});

Evaluate callback for each element in collection and create a new collection from the results. The element will be the first argument passed to the callback and is also available as $_.

my $doubled = $collection->map(sub { $_ * 2 });

new

my $collection = Mojo::Collection->new(1, 2, 3);

Construct a new array-based Mojo::Collection object.

pluck

my $new = $collection->pluck($method);
my $new = $collection->pluck($method, @args);

Call method on each element in collection and create a new collection from the results.

# Equal to but more convenient than
my $new = $collection->map(sub { $_->$method(@args) });

reverse

my $new = $collection->reverse;

创建一个新的 collection 的对象, 这个中包含反转过的顺序的元素.

slice

my $new = $collection->slice(4 .. 7);

创建一个新的 collection 的对象, 这个中包含你切片选择的元素.

shuffle

my $new = $collection->shuffle;

创建一个新的 collection 的对象, 这个中的元素的顺序是完全打乱的.

size

my $size = $collection->size;

返回对象中元素的数量.

sort

my $new = $collection->sort;
my $new = $collection->sort(sub {...});

从结果中返回一个新的 collection 对象, 这个结果是基于给的回调返回的值的排序.

# Sort values case insensitive
my $insensitive = $collection->sort(sub { uc($a) cmp uc($b) });

tap

$collection = $collection->tap(sub {...});

"tap" in Mojo::Base 的别名.

uniq

my $new = $collection->uniq;

创建一个新的没有重复元素的 collection 对象.

AUTOLOAD

In addition to the "METHODS" above, you can also call methods provided by all elements in the collection directly and create a new collection from the results, similar to "pluck".

# "<h2>Test1</h2><h2>Test2</h2>"
 my $collection = Mojo::Collection->new(
   Mojo::DOM->new("<h1>1</h1>"), Mojo::DOM->new("<h1>2</h1>"));
 $collection->at('h1')->type('h2')->prepend_content('Test')->join;

OPERATORS

Mojo::Collection overloads the following operators.

bool

my $bool = !!$collection;

True or false, depending on if the collection is empty.

stringify

my $str = "$collection";

Stringify elements in collection and "join" them with newlines.

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.