Sidef

 view release on metacpan or  search on metacpan

lib/Sidef/Types/Array/Array.pod  view on Meta::CPAN


    say [1,2].tuples_with_repetition(2)     #=> [[1,1], [1,2], [2,1], [2,2]]

Aliases: I<variations_with_repetition>

=cut

=head2 uniq

    arr.uniq
    arr.uniq { ... }

Returns a new array with duplicate elements removed. When a block is given, uniqueness is determined by the result of the block.

    say [1,2,2,3,3,3].uniq              #=> [1, 2, 3]
    say [1,-1,2,-2,3].uniq { .abs }     #=> [1, 2, 3]

Aliases: I<unique>, I<distinct>

=cut

=head2 uniq_by

    arr.uniq_by { ... }

Returns a new array with duplicate elements removed, where uniqueness is determined by mapping each element to the given block.

    say %w(foo bar baz qux).uniq_by { .len }       #=> ["foo", "qux"]

Aliases: I<unique_by>

=cut

=head2 uniq_permutations

    arr.uniq_permutations
    arr.uniq_permutations { ... }

It uses the C<next_permutation> method to create all the unique permutations of the self-array.

    say [1,1,2].unique_permutations         #=> [[1, 1, 2], [1, 2, 1], [2, 1, 1]]

Equivalent with C<arr.permutations.uniq>, but more efficient, as it creates the permutations without duplicates.

The method also accepts a callback block as an optional argument:

    [1,1,2].unique_permutations {|*perm|
        say perm
    }

Output:

    [1, 1, 2]
    [1, 2, 1]
    [2, 1, 1]

Aliases: I<unique_permutations>

=cut

=head2 uniq_prefs

    arr.uniq_prefs { ... }

Returns an array of unique prefixes based on the given block. Iterates through the array and includes an element only if the block result hasn't been seen before.

    say [1,2,2,3,3,3].uniq_prefs { _ }          #=> [1, 2, 3]
    say [1,-1,2,-2,3].uniq_prefs { .abs }       #=> [1, 2, 3]

Aliases: I<unique_prefixes>

=cut

=head2 unroll_op

    arr.unroll_op(operator, arg)

Applies the given operator between consecutive elements of the array, optionally with an initial argument.

    say [1,2,3,4].unroll_op('+')        #=> [1, 3, 6, 10]  (cumulative sum)
    say [1,2,3,4].unroll_op('*', 1)     #=> [1, 2, 6, 24]  (factorial progression)

Aliases: I<unroll_operator>

=cut

=head2 unzip_by

    arr.unzip_by { ... }

Unzips an array by applying the given block to each element, which should return an array. Returns an array where each element is an array of the corresponding positions from all block results.

    say [[1,2],[3,4],[5,6]].unzip_by { _ }      #=> [[1, 3, 5], [2, 4, 6]]

=cut

=head2 weighted_shuffle_by

    arr.weighted_shuffle_by { ... }

Returns a randomly shuffled copy of the array, where the probability of each element appearing earlier is weighted by the result of the given block.

    say [1,2,3,4].weighted_shuffle_by { _ }     # higher numbers more likely to appear first

=cut

=head2 wise_op

    arr1.wise_op(operator, arr2)

Applies the given operator element-wise between two arrays.

    say [1,2,3].wise_op('+', [4,5,6])       #=> [5, 7, 9]
    say [10,20,30].wise_op('*', [2,3,4])    #=> [20, 60, 120]

Aliases: I<wise_operator>

=cut

=head2 zip

    arr.zip(*arrays)

Zips the array with one or more other arrays, returning an array of arrays where each sub-array contains the corresponding elements from all arrays.

    say [1,2,3].zip([4,5,6])                #=> [[1, 4], [2, 5], [3, 6]]
    say [1,2,3].zip([4,5,6], [7,8,9])       #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]



( run in 1.391 second using v1.01-cache-2.11-cpan-98e64b0badf )