Venus::Set - Set Class
Set Class for Perl 5
package main; use Venus::Set; my $set = Venus::Set->new([1,1,2,2,3,3,4,4,5..9]); # $set->count; # 4
This package provides a representation of a collection of ordered unique values and methods for validating and manipulating it.
This package has the following attributes:
accept(string $data) (string)
The accept attribute is read-write, accepts (string) values, and is optional.
(string)
Since 4.11
4.11
# given: synopsis package main; my $set_accept = $set->accept("number"); # "number"
# given: synopsis # given: example-1 accept package main; my $get_accept = $set->accept; # "number"
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Mappable
This package provides the following methods:
all(coderef $code) (boolean)
The all method returns true if the callback returns true for all of the elements.
# given: synopsis; my $all = $set->all(sub { $_ > 0; }); # 1
# given: synopsis; my $all = $set->all(sub { my ($key, $value) = @_; $value > 0; }); # 1
any(coderef $code) (boolean)
The any method returns true if the callback returns true for any of the elements.
# given: synopsis; my $any = $set->any(sub { $_ > 4; });
# given: synopsis; my $any = $set->any(sub { my ($key, $value) = @_; $value > 4; });
attest() (any)
The attest method validates the values using the Venus::Assert expression in the "accept" attribute and returns the result.
# given: synopsis package main; my $attest = $set->attest; # [1..9]
# given: synopsis package main; $set->accept('number | object'); my $attest = $set->attest; # [1..9]
# given: synopsis package main; $set->accept('string'); my $attest = $set->attest; # Exception! (isa Venus::Check::Error)
# given: synopsis package main; $set->accept('Venus::Number'); my $attest = $set->attest; # [1..9]
call(string $iterable, string $method) (any)
The call method executes the given method (named using the first argument) which performs an iteration (i.e. takes a callback) and calls the method (named using the second argument) on the object (or value) and returns the result of the iterable method.
# given: synopsis package main; my $call = $set->call('map', 'incr'); # [2..10]
# given: synopsis package main; my $call = $set->call('grep', 'gt', 4); # [4..9]
contains(any $value) (boolean)
The contains method returns true if the value provided already exists in the set, otherwise it returns false.
# given: synopsis; my $contains = $set->contains(1); # true
# given: synopsis; my $contains = $set->contains(0); # false
count() (number)
The count method returns the number of elements within the set.
# given: synopsis; my $count = $set->count; # 9
default() (arrayref)
The default method returns the default value, i.e. [].
[]
# given: synopsis; my $default = $set->default; # []
delete(number $index) (any)
The delete method returns the value of the element at the index specified after removing it from the array.
# given: synopsis; my $delete = $set->delete(2); # 3
difference(arrayref | Venus::Array | Venus::Set $data) (Venus::Set)
The difference method returns a new set containing only the values that don't exist in the source.
# given: synopsis package main; my $difference = $set->difference([9, 10, 11]); # bless(..., "Venus::Set") # $difference->list; # [10, 11]
# given: synopsis package main; my $difference = $set->difference(Venus::Set->new([9, 10, 11])); # bless(..., "Venus::Set") # $difference->list; # [10, 11]
# given: synopsis package main; use Venus::Array; my $difference = $set->difference(Venus::Array->new([9, 10, 11])); # bless(..., "Venus::Set") # $difference->list; # [10, 11]
different(arrayref | Venus::Array | Venus::Set $data) (boolean)
The different method returns true if the values provided don't exist in the source.
# given: synopsis package main; my $different = $set->different([1..10]); # true
# given: synopsis package main; my $different = $set->different([1..9]); # false
each(coderef $code) (arrayref)
The each method executes a callback for each element in the array passing the index and value as arguments. This method can return a list of values in list-context.
# given: synopsis; my $each = $set->each(sub { [$_] }); # [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
# given: synopsis; my $each = $set->each(sub { my ($key, $value) = @_; [$key, $value] }); # [ # [0, 1], # [1, 2], # [2, 3], # [3, 4], # [4, 5], # [5, 6], # [6, 7], # [7, 8], # [8, 9], # ]
empty() (Venus::Array)
The empty method drops all elements from the set.
# given: synopsis; my $empty = $set->empty; # bless({}, "Venus::Set")
exists(number $index) (boolean)
The exists method returns true if the element at the index specified exists, otherwise it returns false.
# given: synopsis; my $exists = $set->exists(0); # true
first() (any)
The first method returns the value of the first element.
# given: synopsis; my $first = $set->first; # 1
get(number $index) (any)
The get method returns the value at the position specified.
# given: synopsis package main; my $get = $set->get(0); # 1
# given: synopsis package main; my $get = $set->get(3); # 4
grep(coderef $code) (arrayref)
The grep method executes a callback for each element in the array passing the value as an argument, returning a new array reference containing the elements for which the returned true. This method can return a list of values in list-context.
# given: synopsis; my $grep = $set->grep(sub { $_ > 3 }); # [4..9]
# given: synopsis; my $grep = $set->grep(sub { my ($key, $value) = @_; $value > 3 }); # [4..9]
intersect(arrayref | Venus::Array | Venus::Set $data) (boolean)
The intersect method returns true if the values provided already exist in the source.
# given: synopsis package main; my $intersect = $set->intersect([9, 10]); # true
# given: synopsis package main; my $intersect = $set->intersect([10, 11]); # false
intersection(arrayref | Venus::Array | Venus::Set $data) (Venus::Set)
The intersection method returns a new set containing only the values that already exist in the source.
# given: synopsis package main; $set->push(10); my $intersection = $set->intersection([9, 10, 11]); # bless(..., "Venus::Set") # $intersection->list; # [9, 10]
# given: synopsis package main; $set->push(10); my $intersection = $set->intersection(Venus::Set->new([9, 10, 11])); # bless(..., "Venus::Set") # $intersection->list; # [9, 10]
# given: synopsis package main; use Venus::Array; $set->push(10); my $intersection = $set->intersection(Venus::Array->new([9, 10, 11])); # bless(..., "Venus::Set") # $intersection->list; # [9, 10]
iterator() (coderef)
The iterator method returns a code reference which can be used to iterate over the array. Each time the iterator is executed it will return the next element in the array until all elements have been seen, at which point the iterator will return an undefined value. This method can return a tuple with the key and value in list-context.
# given: synopsis; my $iterator = $set->iterator; # sub { ... } # while (my $value = $iterator->()) { # say $value; # 1 # }
# given: synopsis; my $iterator = $set->iterator; # sub { ... } # while (grep defined, my ($key, $value) = $iterator->()) { # say $value; # 1 # }
join(string $seperator) (string)
The join method returns a string consisting of all the elements in the array joined by the join-string specified by the argument. Note: If the argument is omitted, an empty string will be used as the join-string.
# given: synopsis; my $join = $set->join; # 123456789
# given: synopsis; my $join = $set->join(', '); # "1, 2, 3, 4, 5, 6, 7, 8, 9"
keyed(string @keys) (hashref)
The keyed method returns a hash reference where the arguments become the keys, and the elements of the array become the values.
package main; use Venus::Array; my $set = Venus::Array->new([1..4]); my $keyed = $set->keyed('a'..'d'); # { a => 1, b => 2, c => 3, d => 4 }
keys() (arrayref)
The keys method returns an array reference consisting of the indicies of the array.
# given: synopsis; my $keys = $set->keys; # [0..8]
last() (any)
The last method returns the value of the last element in the array.
# given: synopsis; my $last = $set->last; # 9
length() (number)
The length method returns the number of elements within the array, and is an alias for the "count" method.
# given: synopsis; my $length = $set->length; # 9
list() (any)
The list method returns a shallow copy of the underlying array reference as an array reference.
# given: synopsis; my $list = $set->list; # 9
# given: synopsis; my @list = $set->list; # (1..9)
map(coderef $code) (arrayref)
The map method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new array reference containing the elements for which the argument returns a value or non-empty list. This method can return a list of values in list-context.
# given: synopsis; my $map = $set->map(sub { $_ * 2 }); # [2, 4, 6, 8, 10, 12, 14, 16, 18]
# given: synopsis; my $map = $set->map(sub { my ($key, $value) = @_; [$key, ($value * 2)] }); # [ # [0, 2], # [1, 4], # [2, 6], # [3, 8], # [4, 10], # [5, 12], # [6, 14], # [7, 16], # [8, 18], # ]
merge(any @data) (Venus::Set)
The merge method merges the arguments provided with the existing set.
# given: synopsis; my $merge = $set->merge(6..9); # bless(..., "Venus::Set") # $set->list; # [1..9]
# given: synopsis; my $merge = $set->merge(8, 10); # bless(..., "Venus::Set") # $set->list; # [1..10]
none(coderef $code) (boolean)
The none method returns true if none of the elements in the array meet the criteria set by the operand and rvalue.
# given: synopsis; my $none = $set->none(sub { $_ < 1 }); # 1
# given: synopsis; my $none = $set->none(sub { my ($key, $value) = @_; $value < 1 }); # 1
one(coderef $code) (boolean)
The one method returns true if only one of the elements in the array meet the criteria set by the operand and rvalue.
# given: synopsis; my $one = $set->one(sub { $_ == 1 }); # 1
# given: synopsis; my $one = $set->one(sub { my ($key, $value) = @_; $value == 1 }); # 1
order(number @indices) (Venus::Array)
The order method reorders the array items based on the indices provided and returns the invocant.
# given: synopsis; my $order = $set->order; # bless(..., "Venus::Set") # $set->list; # [1..9]
# given: synopsis; my $order = $set->order(8,7,6); # bless(..., "Venus::Set") # $set->list; # [9,8,7,1,2,3,4,5,6]
# given: synopsis; my $order = $set->order(0,2,1); # bless(..., "Venus::Set") # $set->list; # [1,3,2,4,5,6,7,8,9]
pairs() (arrayref)
The pairs method is an alias to the pairs_array method. This method can return a list of values in list-context.
# given: synopsis; my $pairs = $set->pairs; # [ # [0, 1], # [1, 2], # [2, 3], # [3, 4], # [4, 5], # [5, 6], # [6, 7], # [7, 8], # [8, 9], # ]
part(coderef $code) (tuple[arrayref, arrayref])
The part method iterates over each element in the array, executing the code reference supplied in the argument, using the result of the code reference to partition to array into two distinct array references. This method can return a list of values in list-context.
# given: synopsis; my $part = $set->part(sub { $_ > 5 }); # [[6..9], [1..5]]
# given: synopsis; my $part = $set->part(sub { my ($key, $value) = @_; $value < 5 }); # [[1..4], [5..9]]
pop() (any)
The pop method returns the last element of the array shortening it by one. Note, this method modifies the array.
# given: synopsis; my $pop = $set->pop; # 9
push(any @data) (arrayref)
The push method appends the array by pushing the agruments onto it and returns itself.
# given: synopsis; my $push = $set->push(10); # [1..10]
random() (any)
The random method returns a random element from the array.
# given: synopsis; my $random = $set->random; # 2 # my $random = $set->random; # 1
range(number | string @args) (arrayref)
The range method accepts a "range expression" and returns the result of calling the "slice" method with the computed range.
# given: synopsis package main; my $range = $set->range; # []
# given: synopsis package main; my $range = $set->range(0); # [1]
# given: synopsis package main; my $range = $set->range('0:'); # [1..9]
# given: synopsis package main; my $range = $set->range(':4'); # [1..5]
# given: synopsis package main; my $range = $set->range('8:'); # [9]
# given: synopsis package main; my $range = $set->range('4:'); # [5..9]
# given: synopsis package main; my $range = $set->range('0:2'); # [1..3]
# given: synopsis package main; my $range = $set->range('2:4'); # [3..5]
# given: synopsis package main; my $range = $set->range(0..3); # [1..4]
# given: synopsis package main; my $range = $set->range('-1:8'); # [9,1..9]
# given: synopsis package main; my $range = $set->range('0:8'); # [1..9]
# given: synopsis package main; my $range = $set->range('0:-2'); # [1..7]
# given: synopsis package main; my $range = $set->range('-2:-2'); # [8]
# given: synopsis package main; my $range = $set->range('0:-20'); # []
# given: synopsis package main; my $range = $set->range('-2:-20'); # []
# given: synopsis package main; my $range = $set->range('-2:-6'); # []
# given: synopsis package main; my $range = $set->range('-2:-8'); # []
# given: synopsis package main; my $range = $set->range('-2:-9'); # []
# given: synopsis package main; my $range = $set->range('-5:-1'); # [5..9]
reverse() (arrayref)
The reverse method returns an array reference containing the elements in the array in reverse order.
# given: synopsis; my $reverse = $set->reverse; # [9, 8, 7, 6, 5, 4, 3, 2, 1]
rotate() (arrayref)
The rotate method rotates the elements in the array such that first elements becomes the last element and the second element becomes the first element each time this method is called.
# given: synopsis; my $rotate = $set->rotate; # [2..9, 1]
rsort() (arrayref)
The rsort method returns an array reference containing the values in the array sorted alphanumerically in reverse.
# given: synopsis; my $rsort = $set->rsort; # [9, 8, 7, 6, 5, 4, 3, 2, 1]
set(any $value) (any)
The set method inserts a new value into the set if it doesn't exist.
# given: synopsis package main; $set = $set->set(10); # 10
# given: synopsis package main; $set = $set->set(0); # 0
shift() (any)
The shift method returns the first element of the array shortening it by one.
# given: synopsis; my $shift = $set->shift; # 1
shuffle() (arrayref)
The shuffle method returns an array with the items in a randomized order.
# given: synopsis package main; my $shuffle = $set->shuffle; # [4, 5, 8, 7, 2, 9, 6, 3, 1]
slice(string @keys) (arrayref)
The slice method returns a hash reference containing the elements in the array at the index(es) specified in the arguments.
# given: synopsis; my $slice = $set->slice(2, 4); # [3, 5]
sort() (arrayref)
The sort method returns an array reference containing the values in the array sorted alphanumerically.
package main; use Venus::Set; my $set = Venus::Set->new(['d','c','b','a']); my $sort = $set->sort; # ["a".."d"]
subset(arrayref | Venus::Array | Venus::Set $data) (boolean)
The subset method returns true if all the values provided already exist in the source.
# given: synopsis package main; my $subset = $set->subset([1..4]); # true
# given: synopsis package main; my $subset = $set->subset([1..10]); # false
# given: synopsis package main; my $subset = $set->subset([1..9]); # true
superset(arrayref | Venus::Array | Venus::Set $data) (boolean)
The superset method returns true if all the values in the source exists in the values provided.
# given: synopsis package main; my $superset = $set->superset([1..10]); # true
# given: synopsis package main; my $superset = $set->superset([1..9]); # false
# given: synopsis package main; my $superset = $set->superset([0..9]); # true
unique() (arrayref)
The unique method returns an array reference consisting of the unique elements in the array.
package main; use Venus::Set; my $set = Venus::Set->new([1,1,1,1,2,3,1]); my $unique = $set->unique; # [1, 2, 3]
unshift(any @data) (arrayref)
The unshift method prepends the array by pushing the agruments onto it and returns itself.
# given: synopsis; my $unshift = $set->unshift(-2,-1,0); # [-2..9]
Awncorp, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2022, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.