perl: Abolish _assoc_BANG.
authorBen Harris <bjh21@bjh21.me.uk>
Fri, 26 Jul 2019 21:48:46 +0000 (22:48 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Fri, 26 Jul 2019 22:54:09 +0000 (23:54 +0100)
_hash_map and assoc both get simpler by not using it.

perl/core.pm
perl/types.pm

index 2ed1d85..dd5a320 100644 (file)
@@ -1,13 +1,14 @@
 package core;
 use strict;
 use warnings FATAL => qw(all);
+use List::Util qw(pairmap);
 use Time::HiRes qw(time);
 
 use readline;
 use types qw(_sequential_Q _equal_Q _clone $nil $true $false
              _nil_Q _true_Q _false_Q
              _number_Q _symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q _sub_Q _function_Q
-             _hash_map _hash_map_Q _assoc_BANG _atom_Q);
+             _hash_map _hash_map_Q _atom_Q);
 use reader qw(read_str);
 use printer qw(_pr_str);
 
@@ -49,8 +50,7 @@ sub slurp {
 
 sub assoc {
     my $src_hsh = shift;
-    my $new_hsh = { %$src_hsh };
-    return _assoc_BANG($new_hsh, @_);
+    return HashMap->new( { %$src_hsh, pairmap { $$a => $b } @_ } );
 }
 
 sub dissoc {
index b029717..e33f2b5 100644 (file)
@@ -5,8 +5,8 @@ use Exporter 'import';
 our @EXPORT_OK = qw(_sequential_Q _equal_Q _clone
                     $nil $true $false _nil_Q _true_Q _false_Q
                     _number_Q _symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q _sub_Q _function_Q
-                    _hash_map _hash_map_Q _assoc_BANG _atom_Q);
-use List::Util qw(pairs);
+                    _hash_map _hash_map_Q _atom_Q);
+use List::Util qw(pairs pairmap);
 
 use Data::Dumper;
 
@@ -164,19 +164,7 @@ sub _vector_Q { $_[0]->isa('Vector') }
     sub get { no overloading '%{}'; $_[0]->{val}->{$_[1]}; }
 }
 
-sub _hash_map {
-    my $hsh = {};
-    return _assoc_BANG($hsh, @_);
-}
-
-sub _assoc_BANG {
-    my $hsh = shift;
-    foreach my $pair (pairs @_) {
-       my ($k, $v) = @$pair;
-        $hsh->{$$k} = $v;
-    }
-    return HashMap->new($hsh);
-}
+sub _hash_map { HashMap->new( { pairmap { $$a => $b } @_ } ) }
 
 sub _hash_map_Q { $_[0]->isa('HashMap') }