How to parse this line of code manually

Discussion in 'Python' started by Davy, Aug 28, 2007.

  1. Davy

    Davy Guest

    Hi all,

    It is well known that Python is appreciated for its merit of concise.
    However, I found the over concise code is too hard to understand for
    me.

    Consider, for instance,
    def known_edits2(word):
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
    NWORDS)

    Shall I understand the code in set() as
    for e2 in edits1(e1) {
    if e2 in NWORDS {
    for e1 in edits1(word) {
    e2
    }
    }
    }

    And a general question is: Is there any tip available to understand
    the code in one line, or what's the parsing priority (left to right,
    right to left, or other possibilities)

    Any suggestions are welcome!

    The code is a simple spell checker from
    http://www.norvig.com/spell-correct.html

    Best regards,
    Davy
     
    Davy, Aug 28, 2007
    #1
    1. Advertising

  2. Davy

    Davy Guest

    On Aug 28, 11:00 am, Davy <> wrote:
    > Hi all,
    >
    > It is well known that Python is appreciated for its merit of concise.
    > However, I found the over concise code is too hard to understand for
    > me.
    >
    > Consider, for instance,
    > def known_edits2(word):
    > return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
    > NWORDS)
    >
    > Shall I understand the code in set() as
    > for e2 in edits1(e1) {
    > if e2 in NWORDS {
    > for e1 in edits1(word) {
    > e2
    > }
    > }
    >
    > }
    >

    [SNIP]
    Hi all, I figured it myself. It is left to righ parse, right?
    So the above one is like
    for e1 in edits1(word) {
    for e2 in edits1(e1) {
    if e2 in NWORDS {
    push e2 to set
    }
    }
    }
    > And a general question is: Is there any tip available to understand
    > the code in one line, or what's the parsing priority (left to right,
    > right to left, or other possibilities)
    >
    > Any suggestions are welcome!
    >
    > The code is a simple spell checker fromhttp://www.norvig.com/spell-correct.html
    >
    > Best regards,
    > Davy
     
    Davy, Aug 28, 2007
    #2
    1. Advertising

  3. Davy

    A.T.Hofkamp Guest

    On 2007-08-28, Davy <> wrote:
    > On Aug 28, 11:00 am, Davy <> wrote:
    >> Hi all,
    >>
    >> It is well known that Python is appreciated for its merit of concise.
    >> However, I found the over concise code is too hard to understand for
    >> me.
    >>
    >> Consider, for instance,
    >> def known_edits2(word):
    >> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
    >> NWORDS)
    >>
    >> Shall I understand the code in set() as
    >> for e2 in edits1(e1) {
    >> if e2 in NWORDS {
    >> for e1 in edits1(word) {
    >> e2
    >> }
    >> }
    >>
    >> }
    >>

    > [SNIP]
    > Hi all, I figured it myself. It is left to righ parse, right?
    > So the above one is like
    > for e1 in edits1(word) {
    > for e2 in edits1(e1) {
    > if e2 in NWORDS {
    > push e2 to set
    > }
    > }
    > }


    This is correct, although I am not sure what language you are using here, it
    looks like a strange mix of Python and C to me.

    >> Any suggestions are welcome!


    The idea is known as List comprehension (for lists, obviously), and comes from
    functional programming, Bird & Wadler used it in their book.



    The notation is very close to mathematics:

    { e2 | e1: edits(word), e2: edits(e1) in NWORDS }

    or in LaTeX:

    $\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
    \forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$

    :)

    (which in words is something like: collect values e2, where e1 comes from
    'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)


    Sincerely,
    Albert
     
    A.T.Hofkamp, Aug 28, 2007
    #3
  4. Davy

    Dustan Guest

    On Aug 28, 2:59 am, "A.T.Hofkamp" <> wrote:
    > On 2007-08-28, Davy <> wrote:
    >
    >
    >
    > > On Aug 28, 11:00 am, Davy <> wrote:
    > >> Hi all,

    >
    > >> It is well known that Python is appreciated for its merit of concise.
    > >> However, I found the over concise code is too hard to understand for
    > >> me.

    >
    > >> Consider, for instance,
    > >> def known_edits2(word):
    > >> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
    > >> NWORDS)

    >
    > >> Shall I understand the code in set() as
    > >> for e2 in edits1(e1) {
    > >> if e2 in NWORDS {
    > >> for e1 in edits1(word) {
    > >> e2
    > >> }
    > >> }

    >
    > >> }

    >
    > > [SNIP]
    > > Hi all, I figured it myself. It is left to righ parse, right?
    > > So the above one is like
    > > for e1 in edits1(word) {
    > > for e2 in edits1(e1) {
    > > if e2 in NWORDS {
    > > push e2 to set
    > > }
    > > }
    > > }

    >
    > This is correct, although I am not sure what language you are using here, it
    > looks like a strange mix of Python and C to me.
    >
    > >> Any suggestions are welcome!

    >
    > The idea is known as List comprehension (for lists, obviously), and comes from
    > functional programming, Bird & Wadler used it in their book.
    >
    > The notation is very close to mathematics:
    >
    > { e2 | e1: edits(word), e2: edits(e1) in NWORDS }
    >
    > or in LaTeX:
    >
    > $\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
    > \forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$
    >
    > :)
    >
    > (which in words is something like: collect values e2, where e1 comes from
    > 'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)
    >


    For more examples:
    http://docs.python.org/tut/node7.html#SECTION007140000000000000000

    A 'list comprehension' with parentheses instead of square-brackets
    creates a generator instead of a list, which can be more memory-
    efficient and allows for lazy evaluation.
     
    Dustan, Aug 28, 2007
    #4
  5. Davy

    Davy Guest

    On Aug 28, 7:28 pm, Dustan <> wrote:
    > On Aug 28, 2:59 am, "A.T.Hofkamp" <> wrote:
    >
    >
    >
    >
    >
    > > On 2007-08-28, Davy <> wrote:

    >
    > > > On Aug 28, 11:00 am, Davy <> wrote:
    > > >> Hi all,

    >
    > > >> It is well known that Python is appreciated for its merit of concise.
    > > >> However, I found the over concise code is too hard to understand for
    > > >> me.

    >
    > > >> Consider, for instance,
    > > >> def known_edits2(word):
    > > >> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
    > > >> NWORDS)

    >
    > > >> Shall I understand the code in set() as
    > > >> for e2 in edits1(e1) {
    > > >> if e2 in NWORDS {
    > > >> for e1 in edits1(word) {
    > > >> e2
    > > >> }
    > > >> }

    >
    > > >> }

    >
    > > > [SNIP]
    > > > Hi all, I figured it myself. It is left to righ parse, right?
    > > > So the above one is like
    > > > for e1 in edits1(word) {
    > > > for e2 in edits1(e1) {
    > > > if e2 in NWORDS {
    > > > push e2 to set
    > > > }
    > > > }
    > > > }

    >
    > > This is correct, although I am not sure what language you are using here, it
    > > looks like a strange mix of Python and C to me.

    >
    > > >> Any suggestions are welcome!

    >
    > > The idea is known as List comprehension (for lists, obviously), and comes from
    > > functional programming, Bird & Wadler used it in their book.

    >
    > > The notation is very close to mathematics:

    >
    > > { e2 | e1: edits(word), e2: edits(e1) in NWORDS }

    >
    > > or in LaTeX:

    >
    > > $\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
    > > \forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$

    >
    > > :)

    >
    > > (which in words is something like: collect values e2, where e1 comes from
    > > 'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)

    >
    > For more examples:http://docs.python.org/tut/node7.html#SECTION007140000000000000000

    [SNIP]
    Hi Hofkamp and Dustan,

    Thank you for your help :)

    Davy
    >
    > A 'list comprehension' with parentheses instead of square-brackets
    > creates a generator instead of a list, which can be more memory-
    > efficient and allows for lazy evaluation.- Hide quoted text -
    >
    > - Show quoted text -
     
    Davy, Aug 29, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marty
    Replies:
    2
    Views:
    5,170
    microsoft.public.dotnet.languages.csharp
    Feb 19, 2004
  2. Replies:
    19
    Views:
    1,132
    Daniel Vallstrom
    Mar 15, 2005
  3. Cirene

    code to login manually

    Cirene, Jun 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    332
    Cirene
    Jun 5, 2008
  4. potchi
    Replies:
    0
    Views:
    432
    potchi
    May 18, 2011
  5. Chris Conrad

    Must manually attach to ASPNET_WP process to debug ASP.NET code

    Chris Conrad, Mar 16, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    194
    Chris Conrad
    Mar 16, 2005
Loading...

Share This Page