From b27da2a93cc42afcf16ab539dc7aa1a84d12ee8c Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 15 Nov 2020 08:47:32 +0100 Subject: Rename Perl 6 to Raku --- CHANGELOG.md | 2 ++ META6.json | 6 ++-- lib/Hash/Merge.pm6 | 78 ------------------------------------------ lib/Hash/Merge.rakumod | 78 ++++++++++++++++++++++++++++++++++++++++++ lib/Hash/Merge/Augment.pm6 | 33 ------------------ lib/Hash/Merge/Augment.rakumod | 33 ++++++++++++++++++ t/01-thing.rakutest | 56 ++++++++++++++++++++++++++++++ t/01-thing.t | 54 ----------------------------- t/02-empty-source.rakutest | 24 +++++++++++++ t/02-empty-source.t | 22 ------------ t/03-unit.rakutest | 59 ++++++++++++++++++++++++++++++++ t/03-unit.t | 59 -------------------------------- 12 files changed, 255 insertions(+), 249 deletions(-) delete mode 100644 lib/Hash/Merge.pm6 create mode 100644 lib/Hash/Merge.rakumod delete mode 100644 lib/Hash/Merge/Augment.pm6 create mode 100644 lib/Hash/Merge/Augment.rakumod create mode 100644 t/01-thing.rakutest delete mode 100644 t/01-thing.t create mode 100644 t/02-empty-source.rakutest delete mode 100644 t/02-empty-source.t create mode 100644 t/03-unit.rakutest delete mode 100644 t/03-unit.t diff --git a/CHANGELOG.md b/CHANGELOG.md index ced20fd..7c7a113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ Versioning](http://semver.org/spec/v2.0.0.html). - The README has been rewritten in Pod6. +- Perl 6 references have been updated to Raku. + ## [1.0.0] - 2018-03-28 ### Added diff --git a/META6.json b/META6.json index 4fef3d0..08e9fc9 100644 --- a/META6.json +++ b/META6.json @@ -10,10 +10,10 @@ "description": "Raku module to deep merge Hashes", "license": "Artistic-2.0", "name": "Hash::Merge", - "perl": "6.c", + "perl": "6.d", "provides": { - "Hash::Merge": "lib/Hash/Merge.pm6", - "Hash::Merge::Augment": "lib/Hash/Merge/Augment.pm6" + "Hash::Merge": "lib/Hash/Merge.rakumod", + "Hash::Merge::Augment": "lib/Hash/Merge/Augment.rakumod" }, "source-url": "https://github.com/scriptkitties/p6-Hash-Merge.git", "tags": [ diff --git a/lib/Hash/Merge.pm6 b/lib/Hash/Merge.pm6 deleted file mode 100644 index 8f41c07..0000000 --- a/lib/Hash/Merge.pm6 +++ /dev/null @@ -1,78 +0,0 @@ -#! /usr/bin/env false - -use v6.d; - -unit module Hash::Merge; - -#| Merge any number of Hashes together. -sub merge-hashes ( - #| Any number of Hashes to merge together. - *@hashes, - - --> Hash -) is export { - my %result = @hashes.shift; - - # Nothing to do if we only got 1 argument - return %result unless @hashes.elems; - - for ^@hashes.elems { - %result = merge-hash(%result, @hashes.shift); - } - - %result; -} - -#| Merge two hashes together. -sub merge-hash ( - #| The original Hash to merge the second Hash into. - %first, - - #| The second hash, which will be merged into the first Hash. - %second, - - #| Boolean to set whether Associative objects should be merged on their - #| own. When set to False, Associative objects in %second will - #| overwrite those from %first. - Bool:D :$deep = True, - - #| Boolean to set whether Positional objects should be appended. When - #| set to False, Positional objects in %second will overwrite those - #| from %first. - Bool:D :$positional-append = True, - - --> Hash -) is export { - my %result = %first; - - for %second.keys -> $key { - # If the key doesn't exist yet in %first, it can be inserted without worry. - if (%first{$key}:!exists) { - %result{$key} = %second{$key}; - next; - } - - given (%first{$key}) { - # Associative objects need to be merged deeply. - when Associative { - %result{$key} = $deep - ?? merge-hash(%first{$key}, %second{$key}, :$deep, :$positional-append) - !! %second{$key} - } - # Positional objects can be merged or overwritten depending on $append-array. - when Positional { - %result{$key} = $positional-append - ?? (|%first{$key}, |%second{$key}) - !! %second{$key} - } - # Anything else will just overwrite. - default { - %result{$key} = %second{$key}; - } - } - } - - %result; -} - -# vim: ft=perl6 ts=4 sw=4 et diff --git a/lib/Hash/Merge.rakumod b/lib/Hash/Merge.rakumod new file mode 100644 index 0000000..5d496a4 --- /dev/null +++ b/lib/Hash/Merge.rakumod @@ -0,0 +1,78 @@ +#! /usr/bin/env false + +use v6.d; + +unit module Hash::Merge; + +#| Merge any number of Hashes together. +sub merge-hashes ( + #| Any number of Hashes to merge together. + *@hashes, + + --> Hash +) is export { + my %result = @hashes.shift; + + # Nothing to do if we only got 1 argument + return %result unless @hashes.elems; + + for ^@hashes.elems { + %result = merge-hash(%result, @hashes.shift); + } + + %result; +} + +#| Merge two hashes together. +sub merge-hash ( + #| The original Hash to merge the second Hash into. + %first, + + #| The second hash, which will be merged into the first Hash. + %second, + + #| Boolean to set whether Associative objects should be merged on their + #| own. When set to False, Associative objects in %second will + #| overwrite those from %first. + Bool:D :$deep = True, + + #| Boolean to set whether Positional objects should be appended. When + #| set to False, Positional objects in %second will overwrite those + #| from %first. + Bool:D :$positional-append = True, + + --> Hash +) is export { + my %result = %first; + + for %second.keys -> $key { + # If the key doesn't exist yet in %first, it can be inserted without worry. + if (%first{$key}:!exists) { + %result{$key} = %second{$key}; + next; + } + + given (%first{$key}) { + # Associative objects need to be merged deeply. + when Associative { + %result{$key} = $deep + ?? merge-hash(%first{$key}, %second{$key}, :$deep, :$positional-append) + !! %second{$key} + } + # Positional objects can be merged or overwritten depending on $append-array. + when Positional { + %result{$key} = $positional-append + ?? (|%first{$key}, |%second{$key}) + !! %second{$key} + } + # Anything else will just overwrite. + default { + %result{$key} = %second{$key}; + } + } + } + + %result; +} + +# vim: ft=raku ts=4 sw=4 et diff --git a/lib/Hash/Merge/Augment.pm6 b/lib/Hash/Merge/Augment.pm6 deleted file mode 100644 index 7c03a9e..0000000 --- a/lib/Hash/Merge/Augment.pm6 +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/env false - -use v6.c; -use MONKEY-TYPING; - -use Hash::Merge; - -# Don't use precompilation in order to not conflict with other MONKEY-TYPING -# modules. -no precompilation; - -augment class Hash { - method merge ( - Hash:D: - - #| The Hash to merge into this one. - %hash, - - #| Boolean to set whether Associative objects should be merged on their - #| own. When set to False, Associative objects in %second will - #| overwrite those from %first. - Bool:D :$deep = True, - - #| Boolean to set whether Positional objects should be appended. When - #| set to False, Positional objects in %second will overwrite those - #| from %first. - Bool:D :$positional-append = True, - ) { - self = merge-hash(self, %hash, :$deep, :$positional-append); - } -} - -# vim: ft=perl6 ts=4 sw=4 et diff --git a/lib/Hash/Merge/Augment.rakumod b/lib/Hash/Merge/Augment.rakumod new file mode 100644 index 0000000..e719dc1 --- /dev/null +++ b/lib/Hash/Merge/Augment.rakumod @@ -0,0 +1,33 @@ +#! /usr/bin/env false + +use v6.d; +use MONKEY-TYPING; + +use Hash::Merge; + +# Don't use precompilation in order to not conflict with other MONKEY-TYPING +# modules. +no precompilation; + +augment class Hash { + method merge ( + Hash:D: + + #| The Hash to merge into this one. + %hash, + + #| Boolean to set whether Associative objects should be merged on their + #| own. When set to False, Associative objects in %second will + #| overwrite those from %first. + Bool:D :$deep = True, + + #| Boolean to set whether Positional objects should be appended. When + #| set to False, Positional objects in %second will overwrite those + #| from %first. + Bool:D :$positional-append = True, + ) { + self = merge-hash(self, %hash, :$deep, :$positional-append); + } +} + +# vim: ft=raku ts=4 sw=4 et diff --git a/t/01-thing.rakutest b/t/01-thing.rakutest new file mode 100644 index 0000000..46c8055 --- /dev/null +++ b/t/01-thing.rakutest @@ -0,0 +1,56 @@ +#! /usr/bin/env raku + +use v6; +use lib 'lib'; +use Test; + +use Hash::Merge::Augment; + +my %a; +my %b; + +%a = 1; +%b = 2; +%a = 2; +%b = 1; + +my %b-orig = %b; +my %a-orig = %a; + +%a.merge(%b); +is-deeply %b, %b-orig; +is-deeply %a, {:a(2), :b(1), :y(${:a(1), :z(2)})}; + +%a = %a-orig; +%b = %b-orig; +%a = "orig"; +%b = "new"; +%a.merge(%b); + +is-deeply %a, {Z => 'new', a => 2, b => 1, y => {a => 1, z => 2}}; + +{ + my (%z, %y); + + %z

= (1,2,3,4); + %y

= (5,4,6,7); + + %z.merge(%y); + + is %z, {y => {p => [1, 2, 3, 4, 5, 4, 6, 7]}}, "appends arrays"; +} + +{ + my (%z, %y); + + %z

= (1,2,3,4); + %y

= (5,4,6,7); + + %z.merge(%y, :!positional-append); + + is-deeply %z, ${:y(${:p($(5, 4, 6, 7))})}, ":!positional-append makes lists overwrite"; +} + +done-testing; + +# vim: ft=raku ts=4 sw=4 et diff --git a/t/01-thing.t b/t/01-thing.t deleted file mode 100644 index 44d7495..0000000 --- a/t/01-thing.t +++ /dev/null @@ -1,54 +0,0 @@ -#! /usr/bin/env perl6 - -use v6; -use lib 'lib'; -use Test; - -use Hash::Merge::Augment; - -my %a; -my %b; - -%a = 1; -%b = 2; -%a = 2; -%b = 1; - -my %b-orig = %b; -my %a-orig = %a; - -%a.merge(%b); -is-deeply %b, %b-orig; -is-deeply %a, {:a(2), :b(1), :y(${:a(1), :z(2)})}; - -%a = %a-orig; -%b = %b-orig; -%a = "orig"; -%b = "new"; -%a.merge(%b); - -is-deeply %a, {Z => 'new', a => 2, b => 1, y => {a => 1, z => 2}}; - -{ - my (%z, %y); - - %z

= (1,2,3,4); - %y

= (5,4,6,7); - - %z.merge(%y); - - is %z, {y => {p => [1, 2, 3, 4, 5, 4, 6, 7]}}, "appends arrays"; -} - -{ - my (%z, %y); - - %z

= (1,2,3,4); - %y

= (5,4,6,7); - - %z.merge(%y, :!positional-append); - - is-deeply %z, ${:y(${:p($(5, 4, 6, 7))})}, ":!positional-append makes lists overwrite"; -} - -done-testing; diff --git a/t/02-empty-source.rakutest b/t/02-empty-source.rakutest new file mode 100644 index 0000000..2e6b609 --- /dev/null +++ b/t/02-empty-source.rakutest @@ -0,0 +1,24 @@ +#! /usr/bin/env raku + +use v6.d; + +use Test; + +use Hash::Merge::Augment; + +plan 1; + +my %hash = + a => "a", + b => { + c => "c" + }, +; + +my %empty; + +%empty.merge(%hash); + +is-deeply %empty, %hash, "Merge into empty hash"; + +# vim: ft=raku ts=4 sw=4 et diff --git a/t/02-empty-source.t b/t/02-empty-source.t deleted file mode 100644 index 55403e1..0000000 --- a/t/02-empty-source.t +++ /dev/null @@ -1,22 +0,0 @@ -#! /usr/bin/env perl6 - -use v6.d; - -use Test; - -use Hash::Merge::Augment; - -plan 1; - -my %hash = - a => "a", - b => { - c => "c" - }, -; - -my %empty; - -%empty.merge(%hash); - -is-deeply %empty, %hash, "Merge into empty hash"; diff --git a/t/03-unit.rakutest b/t/03-unit.rakutest new file mode 100644 index 0000000..75d763f --- /dev/null +++ b/t/03-unit.rakutest @@ -0,0 +1,59 @@ +#! /usr/bin/env raku + +use v6.c; + +use Hash::Merge; +use Test; + +plan 2; + +subtest "merge-hash" => { + plan 2; + + my %original = + a => "a", + b => { + c => "c" + } + ; + + my %result = + a => "a", + b => { + c => "c", + d => "d", + }, + ; + + is-deeply merge-hash(%original, %(b => %(d => "d"))), %result, "Hash merges correctly"; + is-deeply merge-hash(%original, %()), %original, "Empty Hash doesn't affect original"; +} + +subtest "merge-hashes" => { + plan 4; + + my %original = + a => "a", + b => { + c => "c" + } + ; + + my %result = + a => "a", + b => { + c => "c", + d => "d", + }, + ; + + is-deeply merge-hashes(%original), %original, "Single argument returns original"; + is-deeply merge-hashes(%original, %(b => %(d => "d"))), %result, "Hash merges correctly"; + is-deeply merge-hashes(%original, %()), %original, "Empty Hash doesn't affect original"; + + %result = "e"; + + is-deeply merge-hashes(%original, %(b => %(d => "d")), %(b => %(e => "e"))), %result, "Hash merges correctly"; +} + +# vim: ft=raku ts=4 sw=4 et diff --git a/t/03-unit.t b/t/03-unit.t deleted file mode 100644 index 5230398..0000000 --- a/t/03-unit.t +++ /dev/null @@ -1,59 +0,0 @@ -#! /usr/bin/env perl6 - -use v6.c; - -use Hash::Merge; -use Test; - -plan 2; - -subtest "merge-hash" => { - plan 2; - - my %original = - a => "a", - b => { - c => "c" - } - ; - - my %result = - a => "a", - b => { - c => "c", - d => "d", - }, - ; - - is-deeply merge-hash(%original, %(b => %(d => "d"))), %result, "Hash merges correctly"; - is-deeply merge-hash(%original, %()), %original, "Empty Hash doesn't affect original"; -} - -subtest "merge-hashes" => { - plan 4; - - my %original = - a => "a", - b => { - c => "c" - } - ; - - my %result = - a => "a", - b => { - c => "c", - d => "d", - }, - ; - - is-deeply merge-hashes(%original), %original, "Single argument returns original"; - is-deeply merge-hashes(%original, %(b => %(d => "d"))), %result, "Hash merges correctly"; - is-deeply merge-hashes(%original, %()), %original, "Empty Hash doesn't affect original"; - - %result = "e"; - - is-deeply merge-hashes(%original, %(b => %(d => "d")), %(b => %(e => "e"))), %result, "Hash merges correctly"; -} - -# vim: ft=perl6 ts=4 sw=4 et -- cgit v1.1