help with StringReverse[] and nested lists

21 views
Skip to first unread message

Piruzzolo

unread,
May 1, 2016, 4:10:52 PM5/1/16
to mathics-devel
Hi guys, I'm pretty new to this development community but really like to contribute.

I'm implementing StringReverse[] and so far managed to make it work for single strings and single-level list, but can't figure out how to make it work on nested lists.

Here's my code:

class StringReverse(Builtin):
   
   
"""
    <dl>
    <dt>'StringReverse[$string$]'
        <dd>reverses the order of characters in $string$.
    </dl>

    >> StringReverse["
hello world"]
     = "
dlrow olleh"

    StringReverse works on nested list too:
    >> StringReverse[{"
cat", {"mouse", "lion"}}]
     = {"
tac", {"esuom", "noil"}}
    """

   
    messages
= {'strse': 'String or list of strings expected at position `1`'}
   
   
def apply(self, string, evaluation):
       
'StringReverse[string_]'

        expr
= Expression('StringReverse', string)
       
if string.has_form('List', None):
            py_strings
= [stri.get_string_value()[::-1] for stri in string.leaves]
           
if None in py_strings:
               
return evaluation.message(
                   
'StringReverse', 'strse', Integer(1), expr)
       
else:
            py_strings
= string.get_string_value()
           
if py_strings is None:
               
return evaluation.message(
                   
'StringReverse', 'strse', Integer(1), expr)
       
if isinstance(py_strings, list):
           
return Expression(
               
'List', *[String(py_stri) for py_stri in py_strings])
       
else:
           
return String(py_strings)        

Hope you get some hint! Thanks

Angus Griffith

unread,
May 1, 2016, 6:56:25 PM5/1/16
to mathic...@googlegroups.com
Hi, and welcome! 

I just added some feedback to your pull request. To add to that, I think your code style is fine. I try to stick to pep8 but it's not something worth stressing about. It's good to see you're getting familiar with some of the mathics utility functions (like has_form and evaluation messages).

We have a few guides on the github wiki https://github.com/mathics/Mathics/wiki which might be useful too.

Cheers,
Angus

--
You received this message because you are subscribed to the Google Groups "mathics-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathics-deve...@googlegroups.com.
To post to this group, send email to mathic...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathics-devel/72725c39-4a15-47e5-a9b0-a21964a5f871%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages