News for hackers. Stuff that matters.
Re:Toolz: A functional standard library for Python by 0x2c8 on Thursday January 21, 2021 @02:26PM (#25862994)
Can you elaborate on this? What is wrong with Python's `lambda`?
Re:Toolz: A functional standard library for Python by rustyminnow on Thursday January 21, 2021 @02:44PM (#25863220)
Like others have said:

- Can only have one line

- Can only use expressions, not statements. E.g. `print`s, loops, conditionals are out.

- Overall just kinda clunky

Here's an SO post about lambdas where the answer is "Use def instead." https://stackoverflow.com/questions/14843777/how-to-write-py...

Re:Toolz: A functional standard library for Python by seertaak on Thursday January 21, 2021 @02:56PM (#25863390)
Exactly. Kind of an own-goal too, from none other than ex-BDFL. While not totally obvious, it's not impossible to widen the syntax to allow multi-line lambdas, you just need to ditch the stack-based lexer-integrated whitespace sensitivity behaviour.
Re:Toolz: A functional standard library for Python by diarrhea on Thursday January 21, 2021 @04:35PM (#25864599)
This works for conditionals:

    In [4]: f = lambda x: "Yes" if x else "No"

    In [5]: f(True)
    Out[5]: 'Yes'

    In [6]: f(False)
    Out[6]: 'No'
It's Python's version of ternary operators, so not sure if that counts as a "true" conditional; but it is one.

Loops don't work, but list comprehensions do, and they are definitely the way to go here. Multi-line loops deserve a `def`.

Re:Toolz: A functional standard library for Python by dragonwriter on Thursday January 21, 2021 @06:21PM (#25866037)
> Can only use expressions, not statements. E.g. `print`s, loops, conditionals are out.

print is a function (and thus can be used in expressions)

python has conditional expressions (<true-val> if <cond> else <false-val>)

loops are a limitation, though comprehensions, map(), functools.reduce(), and the itertools module can allow lots of looping functionality in an expression.

Re:Toolz: A functional standard library for Python by heavyset_go on Thursday January 21, 2021 @02:50PM (#25863298)
print() is a function now and you can use it with lambdas.
Re:Toolz: A functional standard library for Python by pmiller2 on Thursday January 21, 2021 @09:08PM (#25867376)
Even in Python 2, you can do something like this:

    println = lambda s: sys.stdout.write(s + '\n')
Not that it really makes things much better, but, at least it shows you can do it.
Re:Toolz: A functional standard library for Python by bobbylarrybobby on Thursday January 21, 2021 @02:46PM (#25863236)
The lambda calculus is Turing complete, so in theory, Python’s lambdas should suffice...
Re:Toolz: A functional standard library for Python by jolux on Thursday January 21, 2021 @03:01PM (#25863459)
Turing completeness is completely unrelated to whether or not anonymous functions are easy to use in Python.
Re:Toolz: A functional standard library for Python by zelphirkalt on Thursday January 21, 2021 @02:59PM (#25863421)
Which of course says nothing about usability and readability.
Re:Toolz: A functional standard library for Python by rustyminnow on Thursday January 21, 2021 @02:59PM (#25863415)
I mean... You're right. They suffice. They're just less friendly (i.e. useful) than lambdas in other languages.
Re:Toolz: A functional standard library for Python by pansa2 on Thursday January 21, 2021 @02:36PM (#25863126)
Python’s `lambda` can only contain a single expression.

There’s no good way to add support for full anonymous functions to Python’s grammar. One of the rules that makes significant-whitespace work elegantly is that statements can contain expressions, but never vice-versa.

Re:Toolz: A functional standard library for Python by omaranto on Friday January 22, 2021 @02:31AM (#25869357)
Plenty of languages with significant-whitespace have multiline anonymous functions, like Haskell, Standard ML, Ocaml, etc. Maybe it's no possible in Python for a student reason, but the reason is not that the syntax is bad on indentation.
Re:Toolz: A functional standard library for Python by pansa2 on Friday January 22, 2021 @03:16AM (#25869569)
The syntaxes of those languages are fundamentally different from Python’s. They don’t have an “expressions may not contain statements” rule - they don’t even have statements.
Re:Toolz: A functional standard library for Python by omaranto on Friday January 22, 2021 @05:54AM (#25870524)
It sounds like you agree with me: the reason Python does not have multiline lambdas is not that it has significant-whitespace, but other decisions including the “expressions may not contain statements” rule.
Re:Toolz: A functional standard library for Python by c-cube on Saturday January 23, 2021 @11:13AM (#25883304)
Nitpick : neither ocaml nor SML have significant whitespace.
Re:Toolz: A functional standard library for Python by omaranto on Saturday January 23, 2021 @12:12PM (#25884065)
You're right! It's been too long since I used them. I think I was probably remembering F#'s light syntax.
Re:Toolz: A functional standard library for Python by kissgyorgy on Thursday January 21, 2021 @02:32PM (#25863070)
It can only have one line for example.
Re:Toolz: A functional standard library for Python by Iwan-Zotow on Friday January 22, 2021 @12:13AM (#25868555)
no, not correct
Re:Toolz: A functional standard library for Python by ausjke on Thursday January 21, 2021 @02:39PM (#25863163)
since lambda is for simple and short anonymous functions most of the time why do I need type the whole word each time? can they also do what javascript does(or similar):

    x => x * 2
instead of

    lambda x : x * 2
Re:Toolz: A functional standard library for Python by pansa2 on Thursday January 21, 2021 @02:51PM (#25863305)
Until very recently, Python’s grammar was strictly LL(1), so the parser couldn’t handle, for example, `(x, y) => x * y`.

Perhaps with the move to a PEG parser, this syntax could now be supported?

Re:Toolz: A functional standard library for Python by cabalamat on Thursday January 21, 2021 @03:02PM (#25863478)
I like:

    λx: x*2
Re:Toolz: A functional standard library for Python by VWWHFSfQ on Thursday January 21, 2021 @03:52PM (#25864088)
The use of lambda is becoming somewhat of a smell in Python in general. PSF's own black code formatter will complain about using it and pretty much always says to just use a def instead