Skip to content

Commit 3a00ad1

Browse files
committed
initial commit
0 parents  commit 3a00ad1

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# p5-Dispatch-Fu
2+
3+
This is currently the proof of concept of an idea I had to solve
4+
generally the given/when or so-called "smart match' problem by offering
5+
a syntax (thanks again to Perl's prototype coercions) that semantically
6+
allow one to deduce any set of values into a static key according to
7+
a custom reduction operation they create; then use that key to do an O(1)
8+
dispatch.
9+
10+
The solution is tiny and uses the accumlator nature of prototypes. All
11+
complexity is offloaded to the custom implemention of the `fu` block.
12+
Assuming the `fu` block is fast, the entire dispatch will be fast.
13+
14+
```
15+
use strict;
16+
use warnings;
17+
use Dispatch::Fu; # exports 'fu' and 'on'
18+
19+
my $bar = [qw/1 2 3 4 5/];
20+
21+
fu {
22+
# here, give a reference $bar of any kind,
23+
# you compute a static string that is added
24+
# via the 'on' keyword; result will be
25+
# 'bucket' + some number in in 0-5
26+
27+
my $baz = shift;
28+
return ( scalar @$baz > 5 )
29+
? q{bucket5}
30+
: sprintf qq{bucket%d}, scalar @$baz;
31+
}
32+
$bar,
33+
on bucket0 => sub { print qq{bucket 0\n} },
34+
on bucket1 => sub { print qq{bucket 1\n} },
35+
on bucket2 => sub { print qq{bucket 2\n} },
36+
on bucket3 => sub { print qq{bucket 3\n} },
37+
on bucket4 => sub { print qq{bucket 4\n} },
38+
on bucket5 => sub { print qq{bucket 5\n} };
39+
```

dist.ini

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name = Dispatch::Fu
2+
author = oodler577 <oodler@cpan.org>
3+
license = Perl_5
4+
copyright_holder = oodler577
5+
copyright_year = 2023
6+
[@Basic]
7+
[VersionFromModule]
8+
[MetaJSON]
9+
[GithubMeta]
10+
homesite = https://github.com/oodler577/p5-Dispatch-Fu.git
11+
[KwaliteeTests]
12+
[CheckChangeLog]
13+
[Prereqs / TestRequires]
14+
Test::More = 0
15+
[PruneCruft]
16+
[PruneFiles]
17+
filename = releases/

lib/Dispatch/Fu.pm

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use strict;
2+
use warnings;
3+
4+
package Dispatch::Fu;
5+
6+
our $VERSION = q{0.8};
7+
8+
use Exporter qw/import/;
9+
our @EXPORT = qw(fu on);
10+
our @EXPORT_OK = qw(fu on);
11+
12+
sub fu (&@) {
13+
my $code_ref = shift;
14+
my $match_ref = shift;
15+
my %dispatch = @_;
16+
my $key = $code_ref->($match_ref);
17+
$dispatch{$key}->();
18+
}
19+
20+
sub on (@) {
21+
return @_;
22+
}
23+
24+
1;

t/00-example.t

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use strict;
2+
use warnings;
3+
use Dispatch::Fu; # exports 'fu' and 'on'
4+
5+
my $bar = [qw/1 2 3 4 5/];
6+
7+
fu {
8+
# here, give a reference $H of any kind,
9+
# you compute a static string that is added
10+
# via the 'on' keyword; result will be
11+
# 'bucket' + some number in in 0-5
12+
13+
my $baz = shift;
14+
return ( scalar @$baz > 5 )
15+
? q{bucket5}
16+
: sprintf qq{bucket%d}, scalar @$baz;
17+
}
18+
$bar,
19+
on bucket0 => sub { print qq{bucket 0\n} },
20+
on bucket1 => sub { print qq{bucket 1\n} },
21+
on bucket2 => sub { print qq{bucket 2\n} },
22+
on bucket3 => sub { print qq{bucket 3\n} },
23+
on bucket4 => sub { print qq{bucket 4\n} },
24+
on bucket5 => sub { print qq{bucket 5\n} };

0 commit comments

Comments
 (0)