Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Hash.reverse. #344

Closed
p6rt opened this issue Sep 27, 2008 · 7 comments
Closed

Implementation of Hash.reverse. #344

p6rt opened this issue Sep 27, 2008 · 7 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Sep 27, 2008

Migrated from rt.perl.org#59396 (status was 'resolved')

Searchable as RT59396$

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

From @bacek

Hello.

There is implementation of Hash.reverse

--
Bacek

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

From @bacek

mapping_reverse.patch
diff --git a/languages/perl6/src/classes/Mapping.pir b/languages/perl6/src/classes/Mapping.pir
index ec9824f..7b9aace 100644
--- a/languages/perl6/src/classes/Mapping.pir
+++ b/languages/perl6/src/classes/Mapping.pir
@@ -18,7 +18,7 @@ src/classes/Mapping.pir - Perl 6 hash class and related functions
     mappingproto = p6meta.'new_class'('Mapping', 'parent'=>'Hash Any')
     p6meta.'register'('Hash', 'parent'=>mappingproto, 'protoobject'=>mappingproto)
     $P0 = get_hll_namespace ['Mapping']
-    '!EXPORT'('keys kv values', $P0)
+    '!EXPORT'('keys kv values reverse', $P0)
 .end
 
 
@@ -114,6 +114,24 @@ Returns elements of hash as array of C<Pair(key, value)>
 .end
 
 
+=item reverse
+
+=cut
+
+.sub 'reverse' :method :multi('Hash')
+    .local pmc result, iter
+    result = new 'Perl6Hash'
+    iter = new 'Iterator', self
+  iter_loop:
+    unless iter goto iter_end
+    $S0 = shift iter
+    $S1 = self[$S0]
+    result[$S1] = $S0
+    goto iter_loop
+  iter_end:
+    .return (result)
+.end
+
 =item pairs (method)
 
 Returns elements of hash as array of C<Pairs>

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

From @moritz

Vasily Chekalkin (via RT) wrote​:

There is implementation of Hash.reverse

Thanks, applied as r22430. (But put method in alphabetical order)

Moritz

--
Moritz Lenz
http://perlgeek.de/ | http://perl-6.de/ | http://sudokugarden.de/

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Sep 27, 2008
@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

From @pmichaud

On Sat, Sep 27, 2008 at 12​:31​:08AM -0700, Vasily Chekalkin wrote​:

# New Ticket Created by Vasily Chekalkin
# Please include the string​: [perl #​59396]
# in the subject line of all future correspondence about this issue.
# <URL​: http://rt.perl.org/rt3/Ticket/Display.html?id=59396 >

Hello.

There is implementation of Hash.reverse

--
Bacek

diff --git a/languages/perl6/src/classes/Mapping.pir b/languages/perl6/src/classes/Mapping.pir
index ec9824f..7b9aace 100644
--- a/languages/perl6/src/classes/Mapping.pir
+++ b/languages/perl6/src/classes/Mapping.pir
@​@​ -18,7 +18,7 @​@​ src/classes/Mapping.pir - Perl 6 hash class and related functions
mappingproto = p6meta.'new_class'('Mapping', 'parent'=>'Hash Any')
p6meta.'register'('Hash', 'parent'=>mappingproto, 'protoobject'=>mappingproto)
$P0 = get_hll_namespace ['Mapping']
- '!EXPORT'('keys kv values', $P0)
+ '!EXPORT'('keys kv values reverse', $P0)
.end

It feels a little weird to have 'reverse' as an exported symbol
here. Based on S29, the exports on 'reverse' mean that we get​:

  my $str = 'abc';
  say reverse $str; # cba
  say reverse $str, 5; # 2abc

  my %hash = { a=>1, b=>2, c=>3 };
  say reverse %hash; # 1 a\n2 b\n3 c\n
  say reverse %hash, 5; # 5\na 1\nb 2\nc 3\n
  # note keys and values are not reversed

It just seems a bit odd to me that adding an extra argument causes
the meaning of 'reverse' to change for hashes and strings.

Pm

@p6rt
Copy link
Author

p6rt commented Sep 27, 2008

From @moritz

Patrick R. Michaud via RT wrote​:

It feels a little weird to have 'reverse' as an exported symbol
here. Based on S29, the exports on 'reverse' mean that we get​:

my $str = 'abc';
say reverse $str;              \# cba
say reverse $str, 5;           \# 2abc

5abc

my %hash = \{ a=>1, b=>2, c=>3 \};
say reverse %hash;             \# 1   a\\n2   b\\n3   c\\n
say reverse %hash, 5;          \# 5\\na   1\\nb   2\\nc   3\\n
                               \# note keys and values are not reversed

It just seems a bit odd to me that adding an extra argument causes
the meaning of 'reverse' to change for hashes and strings.

It is weird indeed, but from my grasp of the synopsis it's correct
nonetheless.

But IMHO it feels a bit less weird than the Perl 5 behaviour where the
context decides.

Perl 5​:
say reverse "abc"; # abc\n
say scalar reverse "abc"; # cba\n

Maybe the solution is to avoid name conflicts in this case, maybe name
the methods like this​:

Str.mirror
List.reverse
Hash.inverse (or Hash.flip)

(I've never been good with names, so there might be much better ones).

Moritz

--
Moritz Lenz
http://perlgeek.de/ | http://perl-6.de/ | http://sudokugarden.de/

@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant