News for hackers. Stuff that matters.
Re:Why I'm still using Python by snowpid on Friday December 30, 2022 @02:20PM (#34188824)
List Comprehension is just syntax for functional coding (filter, map) but a very good one!
Re:Why I'm still using Python by von_lohengramm on Friday December 30, 2022 @03:28PM (#34189683)
I may be the outlier here, but I have a very hard time comprehending list comprehensions. They've always seemed like a SQL query compressed into a single line (or two if you're lucky!): readable when it's simple, Lovecraftian when it's not. I've always found the functional pipeline style much more readable and much easier to explain to others, especially with IDE type hints.
Re:Why I'm still using Python by int_19h on Friday December 30, 2022 @03:57PM (#34189987)
SQL queries are sequence comprehensions!

Python comprehensions in particular are unfortunate because the flow of the query, like in SQL, is not linear - the projection part is written first, but happens last. Thus, reading a non-trivial query requires moving back and forth to resolve the references.

Compare that to C# LINQ, which is also a kind of sequence comprehension. That one not only has linear syntax where the data flow in the query is strictly left to right, but it's explicitly defined by mapping it to functional pipeline style. Thus, it ends up being a strict improvement on the latter.

(Another example of "good" sequence comprehensions in this sense is XQuery FLWOR)

Re:Why I'm still using Python by maxbond on Friday December 30, 2022 @04:34PM (#34190383)
IMHO the key to legible comprehensions is to use them only to express composition and to put the logic into verbosely named functions.

Chained pipelines (`foo.filter(...).map(...)`) are fine to me, certainly more verbose then comprehensions for better & worse, but I get lost in pipelines expressed as nested functions (`map(filter(a, ...), ...)`).

Re:Why I'm still using Python by dragonwriter on Friday December 30, 2022 @04:36PM (#34190401)
> they’ve always seemed like a SQL query compressed into a single line (or two if you’re lucky!):

If its more than a very simple expression, with one simple for, and no if or very simple if, I try to use at least (more, with parens, if necessary): one line for the result expression, one line for each for clause, and one line for each if clause.

But, while the boundary is subjective, it doesn’t take much before functional pipeline style is clearer (Python lambdas being more limited and syntacticaly cumbersome, functional pipeline style isn’t as clean in Python as it is in other languages, though.)

Re:Why I'm still using Python by snowpid on Sunday January 01, 2023 @06:55AM (#34205622)
it depends on your problem but in many cases simple is enough imho. There are map and filter in python avalaible. I just didnt need to use them.
Re:Why I'm still using Python by ogogmad on Friday December 30, 2022 @03:51PM (#34189928)
Being pedantic: This isn't true in Haskell, because list comprehensions can use pattern matching, while map+filter can't.

  heads :: [[a]] -> [a]
  heads xs = [y | (y:ys) <- xs]

  -- heads [[1],[],[2,3]] == [1,2]
Doing the above using maps and filters looks very different:

  heads' = map head . filter (not . null)
Python doesn't really support Haskell-like pattern matching, so the above doesn't apply.
Re:Why I'm still using Python by pdonis on Friday December 30, 2022 @04:56PM (#34190580)
> Python doesn't really support Haskell-like pattern matching

It does as of 3.10:

https://peps.python.org/pep-0622/

Not as sophisticated as what Haskell can do, I'm sure, but I think it's a good addition to Python.

Re:Why I'm still using Python by ulucs on Friday December 30, 2022 @05:02PM (#34190616)
The thing you're looking for is easily found with a Hoogle search for type

    (a -> Maybe b) -> [a] -> [b]
Which yields mapMaybe: https://hackage.haskell.org/package/base-4.17.0.0/docs/Data-.... so you end up with

    heads = mapMaybe safeHead
Re:Why I'm still using Python by ogogmad on Saturday December 31, 2022 @06:21AM (#34195436)
That doesn't look like a direct substitute that covers every case.
Re:Why I'm still using Python by pharmakom on Friday December 30, 2022 @04:46PM (#34190494)
Sorry, but Python list comprehensions are not very good compared to other languages.
Re:Why I'm still using Python by snowpid on Sunday January 01, 2023 @06:52AM (#34205605)
why?
Re:Why I'm still using Python by pharmakom on Sunday January 01, 2023 @12:28PM (#34208442)
In F# list comprehensions, for example, you can have full blown expressions such as loops and conditionals. This is really nice for templating.

    [
      if List.isEmpty items then
        p “No items”
      else
        ul
          [
             for item in items do
               li [ item ]
          ]
    ]
Re:Why I'm still using Python by snowpid on Monday January 02, 2023 @06:41AM (#34216883)
so?

if len(items) == 0: return [p("no items")] else: ul( [ li(item) for item in items ] )

I am also not a fan of convoluted list comprehension (in python) so powerful is not necessarly a good thing.

Re:Why I'm still using Python by pharmakom on Monday January 02, 2023 @08:09AM (#34217474)
That only works in a single purpose function.

And consider this example:

    [
      h1 “items”

      if List.isEmpty items then
        p “No items”
      else
        ul
          [
             for item in items do
               li [ item ]
          ]
    ]
Re:Why I'm still using Python by snowpid on Monday January 02, 2023 @09:33AM (#34218246)
h1(items) + rest of my code... I might see what are you using for but I am not sure if concenating implicitly (or with line breaks / spaces) is a good design decision for a language.
Re:Why I'm still using Python by pharmakom on Monday January 02, 2023 @09:55AM (#34218489)
The yield keyword is optional. The result is a List<HtmlElement>

I don’t see how this can be done in Python without using mutation, which makes code scattered across multiple statements

Re:Why I'm still using Python by snowpid on Monday January 02, 2023 @11:26AM (#34219726)
your example has also multiply statements. The question is more important: Which syntax to a more generally better readability. (as this is solvable in most languages)
Re:Why I'm still using Python by pharmakom on Monday January 02, 2023 @11:52AM (#34220133)
No, it is built from pure expressions. This means that our list is defined in one block (everything within the [ ] tokens) and immutability is enforced by the compiler.