Skip to content

Commit

Permalink
Hash.invert, List.end, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Jul 11, 2011
1 parent b901bc0 commit 1bb2883
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions LHF.markdown
Expand Up @@ -34,3 +34,7 @@ my Int $b = 3; $b = Nil;
Often builtins are only added as methods, even when the corresponding function
should exist. Go through src/core/ and write functions that re-dispatch to the
methods where necessary

## Hash.push

Implement Hash.push
6 changes: 6 additions & 0 deletions src/core/Any.pm
Expand Up @@ -9,6 +9,7 @@ my class Any {

method eager() { nqp::p6list(nqp::list(self), List, Bool::True).eager }
method elems() { self.list.elems }
method end() { self.list.end }
method infinite() { Mu }
method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method hash() { my %h = self }
Expand Down Expand Up @@ -189,3 +190,8 @@ multi pairs($x) { $x.pairs }
proto kv(|$) { * }
multi kv($x) { $x.kv }

proto elems(|$) { * }
multi elems($a) { $a.elems }

proto end(|$) { * }
multi end($a) { $a.end }
11 changes: 11 additions & 0 deletions src/core/EnumMap.pm
Expand Up @@ -33,6 +33,17 @@ my class EnumMap does Associative {
Nil
}
}
method invert() {
gather {
my Mu $iter := nqp::iterator($!storage);
my Mu $pair;
while $iter {
$pair := nqp::shift($iter);
take Pair.new(:key($pair.value), :value($pair.key));
}
Nil
}
}

method at_key($key is copy) is rw {
$key = $key.Str;
Expand Down
1 change: 1 addition & 0 deletions src/core/List.pm
Expand Up @@ -16,6 +16,7 @@ class List does Positional {
method Bool() { self.gimme(1).Bool }
method Int() { self.elems }
method Numeric() { self.elems }
method end() { self.elems - 1 }
multi method Str(List:D:) { self.join(' ') }
method fmt($format = '%s', $separator = ' ') {
self.map({ .fmt($format) }).join($separator);
Expand Down
4 changes: 2 additions & 2 deletions t/spectest.data
Expand Up @@ -472,7 +472,7 @@ S32-array/create.t
S32-array/exists.t
# S32-array/keys_values.t # err: Method 'keys' not found for invocant of class 'Array'
# S32-array/kv.t # err: Method 'kv' not found for invocant of class 'Array'
# S32-array/pairs.t #err: Could not find sub &pairs
# S23-array/pairs.t #err: Method 'pairs' not found for invocant of class 'Array'
S32-array/pop.t
# S32-array/push.t # err: No applicable candidates found to dispatch to for 'ACCEPTS'
# S32-array/rotate.t # err: Method 'rotate' not found for invocant of class 'Array'
Expand All @@ -495,7 +495,7 @@ S32-hash/exists.t
# S32-io/IO-Socket-INET.t # err: ResizablePMCArray: Can't shift from an empty array!
# S32-list/classify.t # err: Method 'classify' not found for invocant of class 'Array'
S32-list/create.t
# S32-list/end.t # err: Method 'end' not found for invocant of class 'Array'
S32-list/end.t # err: Method 'end' not found for invocant of class 'Array'
# S32-list/first.t # err: too many positional arguments: 3 passed, 1 expected
S32-list/grep.t
S32-list/join.t
Expand Down

0 comments on commit 1bb2883

Please sign in to comment.