+sub compare_numbers {
+ $main::lxdebug->enter_sub();
+
+ my ($a, $a_unit, $b, $b_unit) = @_;
+ require SL::AM;
+ my $units = AM->retrieve_all_units;
+
+ if (!$units->{$a_unit} || !$units->{$b_unit} || ($units->{$a_unit}->{base_unit} ne $units->{$b_unit}->{base_unit})) {
+ $main::lxdebug->leave_sub();
+ return undef;
+ }
+
+ $a *= $units->{$a_unit}->{factor};
+ $b *= $units->{$b_unit}->{factor};
+
+ $main::lxdebug->leave_sub();
+
+ return $a <=> $b;
+}
+
+sub cross(&\@\@) {
+ my $op = shift;
+ use vars qw/@A @B/;
+ local (*A, *B) = @_; # syms for caller's input arrays
+
+ # Localise $a, $b
+ my ($caller_a, $caller_b) = do {
+ my $pkg = caller();
+ no strict 'refs';
+ \*{$pkg.'::a'}, \*{$pkg.'::b'};
+ };
+
+ local(*$caller_a, *$caller_b);
+
+ # This map expression is also the return value.
+ map { my $a_index = $_;
+ map { my $b_index = $_;
+ # assign to $a, $b as refs to caller's array elements
+ (*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]);
+ $op->(); # perform the transformation
+ } 0 .. $#B;
+ } 0 .. $#A;
+}
+
+sub _ary_calc_union_intersect {
+ my ($a, $b) = @_;
+
+ my %count = ();
+
+ foreach my $e (@$a, @$b) { $count{$e}++ }
+
+ my @union = ();
+ my @isect = ();
+ foreach my $e (keys %count) {
+ push @union, $e;
+ push @isect, $e if $count{$e} == 2;
+ }
+
+ return (\@union, \@isect);
+}
+
+sub ary_union {
+ return @{ (_ary_calc_union_intersect @_)[0] };
+}
+
+sub ary_intersect {
+ return @{ (_ary_calc_union_intersect @_)[1] };
+}
+
+sub ary_diff {
+ my ($a, $b) = @_;
+ my %in_b = map { $_ => 1 } @$b;
+ return grep { !$in_b{$_} } @$a;
+}
+
+sub listify {
+ my @ary = scalar @_ > 1 ? @_ : ref $_[0] eq 'ARRAY' ? @{ $_[0] } : (@_);
+ return wantarray ? @ary : scalar @ary;
+}
+
+sub ary_to_hash {
+ my $idx_key = shift;
+ my $value_key = shift;
+
+ return map { ($_, 1) } @_ if !defined($idx_key);
+
+ my @indexes = map { ref $_ eq 'HASH' ? $_->{ $idx_key } : $_->$idx_key(); } @_;
+ my @values = map {
+ !defined($value_key) ? $_
+ : ref $_ eq 'HASH' ? $_->{ $value_key }
+ : $_->$value_key()
+ } @_;
+
+ return zip(@indexes, @values);
+}
+
+sub uri_encode {
+ my ($str) = @_;
+
+ $str = Encode::encode('utf-8-strict', $str);
+ $str =~ s/([^a-zA-Z0-9_.:-])/sprintf("%%%02x", ord($1))/ge;
+
+ return $str;
+}
+
+sub uri_decode {
+ my $str = $_[0] // '';
+
+ $str =~ tr/+/ /;
+ $str =~ s/\\$//;
+
+ $str =~ s/%([0-9a-fA-Z]{2})/pack("C",hex($1))/eg;
+ $str = Encode::decode('utf-8-strict', $str);
+
+ return $str;
+}
+