-
Notifications
You must be signed in to change notification settings - Fork 1
/
Units.pm6
85 lines (71 loc) · 1.99 KB
/
Units.pm6
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
use Units::Unit;
module Units
{
use Pluggable;
register-units;
sub register-units
{
say "register-units";
for find-plugins() -> $plugin
{
say "registering ", $plugin.^name;
try {
Units::Unit.instance.UNITS.push( $plugin.new );
CATCH { die "Cannot register unit <{ $plugin.^name }>\n$_" }
}
}
say "done registering";
}
sub find-plugins returns UnitArray
{
my $plugins-namespace = '';
my $name-matcher = -> $a { $a ~~ "Units::Unit"|"Units::DerivedUnit"|"Units::SIUnit" ?? Any !! $a };
#my $name-matcher = -> $a { $a !~~ "Units::Lightyear" ?? Any !! $a };
return
plugins( "Units", :$plugins-namespace, :$name-matcher )
.grep({ .HOW.^name ~~ "Perl6::Metamodel::ClassHOW" })
.grep({ .isa(<Units::Unit>) })
.Array;
}
our sub UNITS
is export
returns UnitArray
{
Units::Unit.instance.UNITS;
}
}
sub EXPORT ( *@unit-selection ) returns Hash {
say "EXPORT";
return %(
|unit-operators( @unit-selection, Units::Unit.instance.UNITS )
);
}
sub unit-operators( StrArray $unit-selection, UnitArray $units ) returns Hash
{
my @short-units = @$units.grep( -> $unit { $unit.symbol ∈ @$unit-selection } );
my @long-units = @$units.grep( -> $unit { $unit.singular-name|$unit.plural-name ∈ @$unit-selection } );
return %(
|short-unit-operators(@short-units),
|long-unit-operators(@long-units)
);
}
sub short-unit-operators( UnitArray $units ) returns Hash {
return %( |@$units.map( -> $unit {
|make-unit-postfix( $unit, [$unit.symbol] )
}));
}
sub long-unit-operators( UnitArray $units ) returns Hash {
return %( |@$units.map( -> $unit {
|make-unit-postfix( $unit, [$unit.singular-name, $unit.plural-name] )
}));
}
sub make-unit-postfix( Units::Unit $unit, StrArray $operator) returns Hash {
return %( |@$operator
.map({ "&postfix:<$_>" })
.map({ $_ =>
sub ($value) is looser(&prefix:<->) {
$unit.new( :$value );
}
})
);
}