NAME
Venus::Set - Set Class
ABSTRACT
Set Class for Perl 5
SYNOPSIS
package main;
use Venus::Set;
my $set = Venus::Set->new([1,1,2,2,3,3,4,4,5..9]);
# $set->count;
# 4
DESCRIPTION
This package provides a representation of a collection of ordered unique values and methods for validating and manipulating it.
ATTRIBUTES
This package has the following attributes:
accept
accept(string $data) (string)
The accept attribute is read-write, accepts (string)
values, and is optional.
Since 4.11
- accept example 1
-
# given: synopsis package main; my $set_accept = $set->accept("number"); # "number"
- accept example 2
-
# given: synopsis # given: example-1 accept package main; my $get_accept = $set->accept; # "number"
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
all
all(coderef $code) (boolean)
The all method returns true if the callback returns true for all of the elements.
Since 4.11
- all example 2
-
# given: synopsis; my $all = $set->all(sub { my ($key, $value) = @_; $value > 0; }); # 1
any
any(coderef $code) (boolean)
The any method returns true if the callback returns true for any of the elements.
Since 4.11
attest
attest() (any)
The attest method validates the values using the Venus::Assert expression in the "accept" attribute and returns the result.
Since 4.11
- attest example 2
-
# given: synopsis package main; $set->accept('number | object'); my $attest = $set->attest; # [1..9]
- attest example 3
-
# given: synopsis package main; $set->accept('string'); my $attest = $set->attest; # Exception! (isa Venus::Check::Error)
- attest example 4
-
# given: synopsis package main; $set->accept('Venus::Number'); my $attest = $set->attest; # [1..9]
call
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.
Since 4.11
contains
contains(any $value) (boolean)
The contains method returns true if the value provided already exists in the set, otherwise it returns false.
Since 4.11
count
count() (number)
The count method returns the number of elements within the set.
Since 4.11
default
default() (arrayref)
The default method returns the default value, i.e. []
.
Since 4.11
delete
delete(number $index) (any)
The delete method returns the value of the element at the index specified after removing it from the array.
Since 4.11
difference
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.
Since 4.11
- difference example 1
-
# given: synopsis package main; my $difference = $set->difference([9, 10, 11]); # bless(..., "Venus::Set") # $difference->list; # [10, 11]
- difference example 2
-
# given: synopsis package main; my $difference = $set->difference(Venus::Set->new([9, 10, 11])); # bless(..., "Venus::Set") # $difference->list; # [10, 11]
- difference example 3
-
# 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
different(arrayref | Venus::Array | Venus::Set $data) (boolean)
The different method returns true if the values provided don't exist in the source.
Since 4.11
- different example 1
-
# given: synopsis package main; my $different = $set->different([1..10]); # true
- different example 2
-
# given: synopsis package main; my $different = $set->different([1..9]); # false
each
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.
Since 4.11
- each example 1
-
# given: synopsis; my $each = $set->each(sub { [$_] }); # [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
- each example 2
-
# 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
empty() (Venus::Array)
The empty method drops all elements from the set.
Since 4.11
exists
exists(number $index) (boolean)
The exists method returns true if the element at the index specified exists, otherwise it returns false.
Since 4.11
first
first() (any)
The first method returns the value of the first element.
Since 4.11
get
get(number $index) (any)
The get method returns the value at the position specified.
Since 4.11
grep
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.
Since 4.11
- grep example 2
-
# given: synopsis; my $grep = $set->grep(sub { my ($key, $value) = @_; $value > 3 }); # [4..9]
intersect
intersect(arrayref | Venus::Array | Venus::Set $data) (boolean)
The intersect method returns true if the values provided already exist in the source.
Since 4.11
- intersect example 1
-
# given: synopsis package main; my $intersect = $set->intersect([9, 10]); # true
- intersect example 2
-
# given: synopsis package main; my $intersect = $set->intersect([10, 11]); # false
intersection
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.
Since 4.11
- intersection example 1
-
# given: synopsis package main; $set->push(10); my $intersection = $set->intersection([9, 10, 11]); # bless(..., "Venus::Set") # $intersection->list; # [9, 10]
- intersection example 2
-
# given: synopsis package main; $set->push(10); my $intersection = $set->intersection(Venus::Set->new([9, 10, 11])); # bless(..., "Venus::Set") # $intersection->list; # [9, 10]
- intersection example 3
-
# 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
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.
Since 4.11
- iterator example 1
-
# given: synopsis; my $iterator = $set->iterator; # sub { ... } # while (my $value = $iterator->()) { # say $value; # 1 # }
- iterator example 2
-
# given: synopsis; my $iterator = $set->iterator; # sub { ... } # while (grep defined, my ($key, $value) = $iterator->()) { # say $value; # 1 # }
join
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.
Since 4.11
keyed
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.
Since 4.11
- keyed example 1
-
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
keys() (arrayref)
The keys method returns an array reference consisting of the indicies of the array.
Since 4.11
last
last() (any)
The last method returns the value of the last element in the array.
Since 4.11
length
length() (number)
The length method returns the number of elements within the array, and is an alias for the "count" method.
Since 4.11
list
list() (any)
The list method returns a shallow copy of the underlying array reference as an array reference.
Since 4.11
map
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.
Since 4.11
- map example 1
-
# given: synopsis; my $map = $set->map(sub { $_ * 2 }); # [2, 4, 6, 8, 10, 12, 14, 16, 18]
- map example 2
-
# 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
merge(any @data) (Venus::Set)
The merge method merges the arguments provided with the existing set.
Since 4.11
- merge example 1
-
# given: synopsis; my $merge = $set->merge(6..9); # bless(..., "Venus::Set") # $set->list; # [1..9]
- merge example 2
-
# given: synopsis; my $merge = $set->merge(8, 10); # bless(..., "Venus::Set") # $set->list; # [1..10]
none
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.
Since 4.11
- none example 2
-
# given: synopsis; my $none = $set->none(sub { my ($key, $value) = @_; $value < 1 }); # 1
one
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.
Since 4.11
- one example 2
-
# given: synopsis; my $one = $set->one(sub { my ($key, $value) = @_; $value == 1 }); # 1
order
order(number @indices) (Venus::Array)
The order method reorders the array items based on the indices provided and returns the invocant.
Since 4.11
- order example 1
-
# given: synopsis; my $order = $set->order; # bless(..., "Venus::Set") # $set->list; # [1..9]
- order example 2
-
# given: synopsis; my $order = $set->order(8,7,6); # bless(..., "Venus::Set") # $set->list; # [9,8,7,1,2,3,4,5,6]
- order example 3
-
# given: synopsis; my $order = $set->order(0,2,1); # bless(..., "Venus::Set") # $set->list; # [1,3,2,4,5,6,7,8,9]
pairs
pairs() (arrayref)
The pairs method is an alias to the pairs_array method. This method can return a list of values in list-context.
Since 4.11
- pairs example 1
-
# 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
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.
Since 4.11
- part example 2
-
# given: synopsis; my $part = $set->part(sub { my ($key, $value) = @_; $value < 5 }); # [[1..4], [5..9]]
pop
pop() (any)
The pop method returns the last element of the array shortening it by one. Note, this method modifies the array.
Since 4.11
push
push(any @data) (arrayref)
The push method appends the array by pushing the agruments onto it and returns itself.
Since 4.11
random
random() (any)
The random method returns a random element from the array.
Since 4.11
range
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.
Since 4.11
reverse
reverse() (arrayref)
The reverse method returns an array reference containing the elements in the array in reverse order.
Since 4.11
rotate
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.
Since 4.11
rsort
rsort() (arrayref)
The rsort method returns an array reference containing the values in the array sorted alphanumerically in reverse.
Since 4.11
set
set(any $value) (any)
The set method inserts a new value into the set if it doesn't exist.
Since 4.11
shift
shift() (any)
The shift method returns the first element of the array shortening it by one.
Since 4.11
shuffle
shuffle() (arrayref)
The shuffle method returns an array with the items in a randomized order.
Since 4.11
- shuffle example 1
-
# given: synopsis package main; my $shuffle = $set->shuffle; # [4, 5, 8, 7, 2, 9, 6, 3, 1]
slice
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.
Since 4.11
sort
sort() (arrayref)
The sort method returns an array reference containing the values in the array sorted alphanumerically.
Since 4.11
- sort example 1
-
package main; use Venus::Set; my $set = Venus::Set->new(['d','c','b','a']); my $sort = $set->sort; # ["a".."d"]
subset
subset(arrayref | Venus::Array | Venus::Set $data) (boolean)
The subset method returns true if all the values provided already exist in the source.
Since 4.11
superset
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.
Since 4.11
unique
unique() (arrayref)
The unique method returns an array reference consisting of the unique elements in the array.
Since 4.11
- unique example 1
-
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
unshift(any @data) (arrayref)
The unshift method prepends the array by pushing the agruments onto it and returns itself.
Since 4.11
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
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.