Skip to content

Commit

Permalink
Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
FCO committed Jun 13, 2018
1 parent 67444e4 commit 71df189
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
39 changes: 37 additions & 2 deletions README.md
@@ -1,12 +1,47 @@
NAME
====

OrderedHash - blah blah blah
OrderedHash - A hash that you specify the possible keys and theirs order

SYNOPSIS
========

use OrderedHash;
```perl6
use OrderedHash;

my %oh1 does OrderedHash = b => 2, c => 3, a => 1;

say %oh1.keys; # (a b c)
say %oh1.values; # (1 2 3)
say %oh1.kv; # (a 1 b 2 c 3)
say %oh1.pairs; # (a => 1 b => 2 c => 3)

my %oh2 does OrderedHash[Int] = b => 2, a => 1;

say %oh2.keys; # (a b)
say %oh2.values; # (1 2)
say %oh2.kv; # (a 1 b 2)
say %oh2.pairs; # (a => 1 b => 2)

%oh2<c> = 3;
say %oh2; # {a => 1, b => 2, c => 3}

# %oh2<d> = "error"; # dies

my %oh3 does OrderedHash[:keys<c b a>] = b => 2, c => 1, a => 3;

say %oh3.keys; # (c b a)
say %oh3.values; # (1 2 3)
say %oh3.kv; # (c 1 b 2 a 3)
say %oh3.pairs; # (c => 1 b => 2 a => 3)

my %oh4 does OrderedHash[Str, :keys<c b a>] = b => "b", c => "a", a => "c";

say %oh4.keys; # (c b a)
say %oh4.values; # (a b c)
say %oh4.kv; # (c a b b a c)
say %oh4.pairs; # (c => a b => b a => c)
```

DESCRIPTION
===========
Expand Down
4 changes: 2 additions & 2 deletions lib/OrderedHash.pm6
@@ -1,7 +1,7 @@
unit role OrderedHash[\Type = Any, :@keys = flat("0".."9", "a".."z")] does Associative;
unit role OrderedHash[::T \Type = Any, :@keys = flat("0".."9", "A".."Z", "a".."z")] does Associative;
has Mu:U $!of = Type;
has Str @!keys = @keys;
has @!values is default(Type);
has T @!values is default(Type);
has UInt %!map = @keys.kv.reverse;

method STORE(*@pairs, :$initialize --> OrderedHash:D) {
Expand Down
4 changes: 4 additions & 0 deletions t/02-ordered-hash.t
Expand Up @@ -42,4 +42,8 @@ is %oh2.kv, <2 1 3 2 1 3>;

is %oh2.pairs, (2 => 1, 3 => 2, 1 => 3);

my %oh3 does OrderedHash[Int];

dies-ok {%oh3<error> = "string"};

done-testing

0 comments on commit 71df189

Please sign in to comment.