5 ms·
List comprehensions aren't very functional. A functional language would use higher-level functions like map and filter which JS absolutely has. One thing I alw
by globular-toast 3y ago
List comprehensions aren't very functional. A functional language would use higher-level functions like map and filter which JS absolutely has.
One thing I always miss in languages like Python and JS is the identity function. I don't how people are writing all this functional code without it.
- rebeccaskinner 3y agoI don't really see how list comprehensions aren't functional- it's just a different notation for dealing with maps, filters, and cartesian products. Python borrowed list comprehensions from Haskell in the first place.
- pama 3y ago(not GP and I agree with you, and wanted to add some additional thoughts that may help others.) The comprehensions are great and have a functional flavor, sure. But when you hit the corner cases in Python you feel that the addition they provide to Python are not taking the language close to Haskell. Lambda functions within the Python comprehensions have limited power, reductions don’t work as nicely with comprehensions as map or filter; more generally, when you compose comprehensions things feel messy without the ability to pipe previous results to a next function, so you are using lots of intermediates and have to think of useful names which is hard. The end result can be good and closer to pseudo code, which is fantastic. However Python does not satisfy the need of many people used to lisp and often takes much longer to get right in those complex corner cases where Haskell or lisp would make validation and testing much easier.
- corethree 3y ago> Lambda functions within the Python comprehensions have limited power, lambda functions in python have as much expressive power as any function. >comprehensions things feel messy without the ability to pipe previous results to a next function compose :: (a -> b) -> (b -> c) -> (a -> c) is definable in python Along with type checking. This is basically your pipe operator. Python has very much the capability to come rather close, closer than javascript to the flavor of functional programming found in ML languages like haskell. The benefit of using python is that you can cheat a lot too which is more challenging to do in language as strict as haskell. Here: from typing import Callable, TypeVar, List, Any from numbers import Number A = TypeVar("A") B = TypeVar("C") C = TypeVar("C") def compose(f: Callable[[A], B], g: Callable[[B], C]) -> Callable[[A], C]: return lambda x: g(f(x)) def curry(f: Callable[[Any, ...], Any], x: Any) -> Callable[[Any, ...], Any]: return lambda *args: f(x, *args) def compose_many(fs: tuple[Callable[[Any], Any], ...]) -> Callable[[Any], Any]: return (lambda x: x) if len(fs) == 0 else fs[0] if len(fs) == 1 else compose(compose(fs[0], fs[1]), compose_many(*fs[2:])) MultAllType = Callable[[List[Number], Number], List[Number]] multAll: MultAllType = lambda x, y: [i * y for i in x] SubTractAllType = Callable[[List[Number], Number], List[Number]] subtractAll: SubTractAllType = lambda x, y: [i - y for i in x] quadrupleAndSubtractTwoFromAll = compose_many( curry(multAll, 2), curry(multAll, 2), curry(subtractAll, 2)) as literals: quadrupleAndSubtractTwoFromAll = compose_many( lambda x: [i * 2 for i in x], lambda x: [i * 2 for i in x], lambda x: [i - 2 for i in x]) Python gets a bad rep for being noob language but it's capable of functional programming with very very good type checking. You can cheat too and go no types like lisp as I've shown in the example above. Overall I would say it's superior to typescript in terms of functional programming due to exhaustive pattern matching (which I did not demonstrate above.) In terms of FP in python versus FP in lisp it's mostly just opinionated differences. Python and lisp sort of even out because python has optional type checking and lisp has macros. I would say though that python leans more towards the ML flavor of FP which makes it less comparable to lisp. Apples and Oranges.
- pama 3y agoLambda functions in Python cannot contain statements (you couldn’t do a try/except block, or a while loop); they cannot have assignments, or multiple expressions. They don’t feel syntactically as expressive as other functions in Python. Your composition and curry are OK, and related tools exist in standard libraries. The lack of a first class support is a minor annoyance but not a showstopper, and the solutions are not as natural as genuine pipe notation (you can’t simply write left to right and generate text autoregressively). The lambda limit was more real in my mind and I hope Python eventually corrects it.
- corethree 3y agoNo function in functional programming allows more than one statement. That is literally the definition of FP. That the definition of a functions be a singular statement as opposed to a procedural function is a function made up of several statements where each statement has the potential to assign or reassign values. If you write a function with procedures you are not doing functional programming. That's why python lambdas are defined that way. That being said python doesn't support the let...in expression which is common to do in fp, but neither does JavaScript. JavaScript just supports anonymous functions that aren't functional. If you make the procedures const then you get something similar to let in which is arguably a trick to make it work. You can do tricks in python to achieve the same thing with lambdas. In JavaScript you do tricks to make anonymous functions behave like lambdas with let-in, in python you can also do a trick to get let-in behavior in a lambda. f = lambda x:( y := 1, w := 3, y + w + x)[-1] Arguably though you shouldn't do this. The pythonic philosophy is that Any function past a certain level of complexity you should just use def rather then inline it. This philosophy still applies in FP. Ultimately these are minor syntactic and stylistic differences involving trivial sugar. I don't think python will "correct it" because from their perspective there is nothing to correct. Make your function a def. That's the pythonic way. The first class support for for pipe operators and what not are not present in JavaScript or python. It's not showstoppers. But in python it is possible to get composition working with magic method operating overloading. It's just not first class. Fp in js never involved the algebraic operator style promoted by haskell. In this sense it's more lisp style and python is similar to js in this respect.
- corethree 3y agoNo. A functional language is a language that primarily uses functions as defined in mathematics for it's programming language. Functions defined in mathematics are single lined expressions where no mutation takes place. In traditional languages programs are a list of procedures that change and mutate state. A list comprehension is functional in every way as it is a single expression. It does not change state. It is functionally equivalent to map and filter and even flatmap. If you need an official source you just need to know that list comprehensions are borrowed from haskell. That's where the term came from and that's where the feature was borrowed from.
- esafak 3y agoGuido van Rossum dislikes map and filter: > I value readability and usefulness for real code. There are some places where map() and filter() make sense, and for other places Python has list comprehensions. I ended up hating reduce() because it was almost exclusively used (a) to implement sum(), or (b) to write unreadable code. https://developers.slashdot.org/story/13/08/25/2115204/interviews-guido-van-rossum-answers-your-questions https://developers.slashdot.org/story/13/08/25/2115204/inter... Also, comprehensions are inspired by set-builder notation. https://en.wikipedia.org/wiki/Set-builder_notation https://en.wikipedia.org/wiki/Set-builder_notation
- corethree 3y agoGuido isn't a functional programmer. He doesn't know the trade off. The benefits of functional programming are less technical debt and greater modularity and less bugs. The cost is less readability and less efficiency. Reduce is ultimately the most readable way to iterate and accumulate with FP. The only other way would be recursion which is less readable.
- odyssey7 3y agoThis is fascinating context.