Skip to content Skip to sidebar Skip to footer

Is There A Python Equivalent Of The Haskell 'let'

Is there a Python equivalent of the Haskell 'let' expression that would allow me to write something like: list2 = [let (name,size)=lookup(productId) in (barcode(productId),metric(s

Solution 1:

You could use a temporary list comprehension

[(barcode(productId), metric(size)) for name, size in [lookup(productId)]][0]

or, equivalently, a generator expression

next((barcode(productId), metric(size)) for name, size in [lookup(productId)])

but both of those are pretty horrible.

Another (horrible) method is via a temporary lambda, which you call immediately

(lambda (name, size): (barcode(productId), metric(size)))(lookup(productId))

I think the recommended "Pythonic" way would just be to define a function, like

def barcode_metric(productId):
   name, size = lookup(productId)
   return barcode(productId), metric(size)
list2 = [barcode_metric(productId) for productId in list]

Solution 2:

Recent python versions allows multiple for clauses in a generator expression, so you can now do something like:

list2 = [ barcode(productID), metric(size)
          for productID in list
          for (name,size) in (lookup(productID),) ]

which is similar to what Haskell provides too:

list2 = [ (barcode productID, metric size)
        | productID <- list
        , let (name,size) = lookup productID ]

and denotationally equivalent to

list2 = [ (barcode productID, metric size) 
        | productID <- list
        , (name,size) <- [lookup productID] ]

Solution 3:

The multiple for clauses in b0fh's answer is the style I have personally been using for a while now, as I believe it provides more clarity and doesn't clutter the namespace with temporary functions. However, if speed is an issue, it is important to remember that temporarily constructing a one element list takes notably longer than constructing a one-tuple.

Comparing the speed of the various solutions in this thread, I found that the ugly lambda hack is slowest, followed by the nested generators and then the solution by b0fh. However, these were all surpassed by the one-tuple winner:

list2 = [ barcode(productID), metric(size)
          for productID in list
          for (_, size) in (lookup(productID),) ]

This may not be so relevant to the OP's question, but there are other cases where clarity can be greatly enhanced and speed gained in cases where one might wish to use a list comprehension, by using one-tuples instead of lists for dummy iterators.


Solution 4:

Only guessing at what Haskell does, here's the alternative. It uses what's known in Python as "list comprehension".

[barcode(productId), metric(size)
    for (productId, (name, size)) in [
        (productId, lookup(productId)) for productId in list_]
]

You could include the use of lambda:, as others have suggested.


Solution 5:

Since you asked for best readability you could consider the lambda-option but with a small twist: initialise the arguments. Here are various options I use myself, starting with the first I tried and ending with the one I use most now.

Suppose we have a function (not shown) which gets data_structure as argument, and you need to get x from it repeatedly.

First try (as per 2012 answer from huon):

(lambda x:
    x * x + 42 * x)
  (data_structure['a']['b'])

With multiple symbols this becomes less readable, so next I tried:

(lambda x, y:
    x * x + 42 * x + y)
  (x = data_structure['a']['b'],
   y = 16)

That is still not very readable as it repeats the symbolic names. So then I tried:

(lambda x = data_structure['a']['b'],
        y = 16:
  x * x + 42 * x + y)()

This almost reads as an 'let' expression. The positioning and formatting of the assignments is yours of course.

This idiom is easily recognised by the starting '(' and the ending '()'.

In functional expressions (also in Python), many parenthesis tend to pile up at the end. The odd one out '(' is easily spotted.


Post a Comment for "Is There A Python Equivalent Of The Haskell 'let'"