File: Util.pm

package info (click to toggle)
libcassandra-client-perl 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: perl: 3,898; ansic: 1,767; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 1,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package Cassandra::Client::Util;
our $AUTHORITY = 'cpan:TVDW';
$Cassandra::Client::Util::VERSION = '0.21';
use 5.010;
use strict;
use warnings;

use Exporter 'import';
our @EXPORT= ('series', 'parallel', 'whilst');

use feature 'current_sub';

sub series {
    my $list= shift;
    my $final= shift;

    (shift @$list)->(sub {
        my $next= shift @$list;
        if ($next && !$_[0]) {
            splice @_, 0, 1, __SUB__;
            goto &$next;
        }

        goto &$final;
    });

    return;
}

sub parallel {
    my ($list, $final)= @_;

    if (!@$list) {
        return $final->();
    }

    my $remaining= 0+@$list;
    my @result;
    for my $i (0..$#$list) {
        $list->[$i]->(sub {
            my ($error, $result)= @_;
            if ($error) {
                if ($remaining > 0) {
                    $remaining= 0;
                    $final->($error);
                }
                return;
            }

            $result[$i]= $result;

            $remaining--;
            if ($remaining == 0) {
                $final->(undef, @result);
            }
        });
    }

    return;
}

sub whilst {
    my ($condition, $iteratee, $callback)= @_;

    (sub {
        if (defined $_[0] || !($condition->())) {
            goto &$callback;
        }
        splice @_, 0, 1, __SUB__;
        goto &$iteratee;
    })->();
}

1;

__END__

=pod

=head1 NAME

Cassandra::Client::Util

=head1 VERSION

version 0.21

=head1 AUTHOR

Tom van der Woerdt <tvdw@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Tom van der Woerdt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut