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

Added functionality for strings to be reversed like lists #2994

Closed
p6rt opened this issue Dec 8, 2012 · 6 comments
Closed

Added functionality for strings to be reversed like lists #2994

p6rt opened this issue Dec 8, 2012 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Dec 8, 2012

Migrated from rt.perl.org#116026 (status was 'rejected')

Searchable as RT116026$

@p6rt
Copy link
Author

p6rt commented Dec 8, 2012

From z@chary.me

Currently, Perl6 is lacking the built-in functionality of running

'this is a string'.reverse or reverse 'this is a string'

and getting the desired result. With this patch, I have added the sub for
reversing strings and also the Str method and fixes this problem

## Z. Bornheimer

@p6rt
Copy link
Author

p6rt commented Dec 8, 2012

From z@chary.me

0002-Added-the-functionality-to-reverse-strings-with-a-re.patch
From a159724ab2125ba9f61534ae2d6de3c1b98485ce Mon Sep 17 00:00:00 2001
From: Z. Bornheimer <z@chary.me>
Date: Sat, 8 Dec 2012 11:47:26 -0500
Subject: [PATCH 2/2] Added the functionality to reverse strings with a reverse method or reverse sub

---
 src/core/List.pm |    4 +++-
 src/core/Str.pm  |    5 +++++
 2 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/src/core/List.pm b/src/core/List.pm
index b89b3d2..6f35347 100644
--- a/src/core/List.pm
+++ b/src/core/List.pm
@@ -7,6 +7,7 @@ my class List does Positional {
 
     method new(|) {
         my Mu $args := pir::perl6_current_args_rpa__P();
+        say $args.perl;
         nqp::shift($args);
         nqp::p6list($args, self.WHAT, Mu);
     }
@@ -411,7 +412,8 @@ multi sub unshift(\a, *@elems) { a.unshift: @elems }
 proto sub push(|) {*}
 multi sub push(\a, *@elems) { a.push: @elems }
 
-sub reverse(*@a)            { @a.reverse }
+multi sub reverse(*@a)            { @a.reverse }
+multi sub reverse(*$a)            { return $a.split('').reverse.join('') }
 sub rotate(@a, Int $n = 1)  { @a.rotate($n) }
 sub reduce (&with, *@list)  { @list.reduce(&with) }
 sub categorize(&mapper, *@a){ @a.categorize(&mapper)}
diff --git a/src/core/Str.pm b/src/core/Str.pm
index 279e278..5042008 100644
--- a/src/core/Str.pm
+++ b/src/core/Str.pm
@@ -68,6 +68,11 @@ my class Str does Stringy {
         nqp::p6box_s(pir::chopn__Ssi(nqp::unbox_s(self), 1))
     }
 
+    method reverse(Str:D:) {
+        my $var = self.split('').reverse.join('');
+        $var;
+    }
+
     method substr(Str:D: $start, $length? is copy) {
         my str $sself  = nqp::unbox_s(self);
         my int $istart = nqp::unbox_i(
-- 
1.7.1

@p6rt
Copy link
Author

p6rt commented Dec 9, 2012

From z@chary.me

I'm not sure my patch was received, so here it is in the body of this email​:

================Patch Start============================

From a159724ab2125ba9f61534ae2d6de3c1b98485ce Mon Sep 17 00​:00​:00 2001
From​: Z. Bornheimer <z@​chary.me>
Date​: Sat, 8 Dec 2012 11​:47​:26 -0500
Subject​: [PATCH 2/2] Added the functionality to reverse strings with a
reverse method or reverse sub


src/core/List.pm | 4 +++-
src/core/Str.pm | 5 +++++
2 files changed, 8 insertions(+), 1 deletions(-)

Inline Patch
diff --git a/src/core/List.pm b/src/core/List.pm
index b89b3d2..6f35347 100644
--- a/src/core/List.pm
+++ b/src/core/List.pm
@@ -7,6 +7,7 @@ my class List does Positional {

     method new(|) {
         my Mu $args := pir::perl6_current_args_rpa__P();
+        say $args.perl;
         nqp::shift($args);
         nqp::p6list($args, self.WHAT, Mu);
     }
@@ -411,7 +412,8 @@ multi sub unshift(\a, *@elems) { a.unshift: @elems }
 proto sub push(|) {*}
 multi sub push(\a, *@elems) { a.push: @elems }

-sub reverse(*@a)            { @a.reverse }
+multi sub reverse(*@a)            { @a.reverse }
+multi sub reverse(*$a)            { return $a.split('').reverse.join('') }
 sub rotate(@a, Int $n = 1)  { @a.rotate($n) }
 sub reduce (&with, *@list)  { @list.reduce(&with) }
 sub categorize(&mapper, *@a){ @a.categorize(&mapper)}
diff --git a/src/core/Str.pm b/src/core/Str.pm
index 279e278..5042008 100644
--- a/src/core/Str.pm
+++ b/src/core/Str.pm
@@ -68,6 +68,11 @@ my class Str does Stringy {
         nqp::p6box_s(pir::chopn__Ssi(nqp::unbox_s(self), 1))
     }

+    method reverse(Str:D:) {
+        my $var = self.split('').reverse.join('');
+        $var;
+    }
+
     method substr(Str:D: $start, $length? is copy) {
         my str $sself  = nqp::unbox_s(self);
         my int $istart = nqp::unbox_i(
-- 
1.7.1

================Patch End============================

## Z. Bornheimer

On Sat, Dec 8, 2012 at 12​:32 PM, perl6 via RT
<perl6-bugs-followup@​perl.org>wrote​:

Greetings,

This message has been automatically generated in response to the
creation of a trouble ticket regarding​:
"[PATCH] Added functionality for strings to be reversed like
lists",
a summary of which appears below.

There is no need to reply to this message right now. Your ticket has been
assigned an ID of [perl #​116026].

Please include the string​:

     \[perl #&#8203;116026\]

in the subject line of all future correspondence about this issue. To do
so,
you may reply to this message.

                    Thank you,
                    perl6\-bugs\-followup@&#8203;perl\.org

-------------------------------------------------------------------------
Currently, Perl6 is lacking the built-in functionality of running

'this is a string'.reverse or reverse 'this is a string'

and getting the desired result. With this patch, I have added the sub for
reversing strings and also the Str method and fixes this problem

## Z. Bornheimer

@p6rt
Copy link
Author

p6rt commented Dec 10, 2012

From @moritz

Sorry, this patch is wrong.

Strings are reversed with flip, not with reverse.

The reason is basically that there are too many corner cases with
one-elements and reverse, and that different operations (string and list
reverals) should have different operation/sub/method names.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Dec 10, 2012

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

@p6rt
Copy link
Author

p6rt commented Dec 10, 2012

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

@p6rt p6rt closed this as completed Dec 10, 2012
@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