7 ms·
This site embodies everything I dislike about functional programming: an attitude that shorter and less specific is better. What use is a line like const
by skrebbel 9y ago
This site embodies everything I dislike about functional programming: an attitude that shorter and less specific is better.
What use is a line like
const S = f => g => x => f(x)(g(x));
for anyone except someone who already groks what the S combinator does and is for?
Why do these combinators have single-letter names anyway? Does that help anyone? Even Haskell, heaven on earth for single-letter guess-what-i-mean coders, has actual names for half of these - how is knowing that "flip" is also "the C combinator" helpful in any way?
Now, I'm not trying to diss this page in particular, and I very much am not trying to diss functional programming. I like functional programming. But I think that when hype shifted from OOP to FP, we threw half the baby out with the bathwater (ok, maybe a quarter baby). Naming is important and FP does not have any feature in particular that makes "I" a better name than "identity".
I think programming in general, and FP in particular, will become a better place if we all grow up a bit and stop pretending we're too smart to need good naming.
- tome 9y agoThis is an implementation of combinatory logic and nothing to do with functional programming. https://en.wikipedia.org/wiki/Combinatory_logic https://en.wikipedia.org/wiki/Combinatory_logic
- chii 9y agothe term functional programming has taken to mean more than just programming with functions. combinator logic certainly counts
- nothrabannosir 9y agoFor perspective: this is maths, not programming. You learn about these combinators in your typical lambda calculus course at uni. It's a fun POC to actually represent it in code, not a serious proposal for production grade software.
- deleted 9y ago[deleted]
- ams6110 9y agoWhy do equations use single-letter variables? Or those weird greek symbols? Because they are concise, and their use is so localized that you don't need long names to keep track of them.
- smt88 9y agoI agree, and I think the use of single letters is appropriate here, but "concise" is not a good enough reason to use short variable names when you're writing code for other people to consume. In this case, I'd say it's not about being concise, but rather matching the mathematical notation that people are used to seeing when studying these combinators.
- maze-le 9y agoNo one would implement this in a real productive project. This is for explanatory reasons only, and aimed at people already familiar with combinatorial logic. >> I think programming in general, and FP in particular, will become a better place if we all grow up a bit and stop pretending we're too smart to need good naming. I with you on this, but I think this is a bad example. And while we are at it, documentation matters as well. The 'good code is the best documentation' attitude[ * ] might be valid if your methods consist of 3 lines and you use 6 methods in 5 modules. But anything bigger will become hard to maintain in the long run. [ * ]: I encountered this especially in the haskell and erlang world.
- Kequc 9y ago> No one would implement this in a real productive project. Oh my word. If it's code on the internet someone will use it. Especially functional programmers who have somehow landed themselves in a JavaScript position. I've seen much stranger code pulled off the internet and used in production applications by professionals. That's why it's important to generally publish the best code possible whenever we can.
- dmitriid 9y ago> Especially functional programmers who have somehow landed themselves in a JavaScript position. Or Haskell programmers who have somehow landed themselves in any position. I've seen Haskell implemented in Erlang :)
- ycmjason 9y agoEhhhh, I hate reading this kind of comment. Can I suggest try to be more humble and read more before having an opinion on something?
- abritinthebay 9y agoYou’re going to get downvotes to hell because of the tone of this (and that’s probably fair) but you raise a good point: that form IS extremely common in basically all technical books. It’s probably more common that O notation. The comment makes poster look like they haven’t read anything CompSci related.
- sctb 9y agoThis comment breaks the guidelines. Please don't post like this. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- hoosieree 9y agoSame reason we use + instead of Math.addTwoIntegers(). Pure snobbery.
- hungerstrike 9y agoHowever, we don't combine + with other operators to form an illegible mess. Even ++ is frowned upon for being unreadable/easily missed and/or placed in the wrong spot.
- rounce 9y ago`5 + 10 * 2`?
- hungerstrike 9y agoNot sure I see your point.
- deleted 9y ago[deleted]
- abritinthebay 9y agoThey’re just standard “ignore the name this is about how things are combined” notation. It’s algebra but with functions. If you read any computer science books they’ll often use this form. It’s a holdover from mathematical notation.
- always_good 9y ago"What use is <symbols> for anyone except someone who already groks what the S combinator does and is for?" How does this not apply to any and all identifiers and symbols and functions? What use is a given function until you read its signature, implementation, and docstring? You could use this to condemn all abstraction. After all, even a function you take for granted comes at the expense of indirection. It just so happens that we as craftsmen make trade-offs. Imagine a pattern that shows up so much that you can extract it into a function like a combinator. For instance, look at parser combinators. Yes, you'd have to credentialize in some patterns, but once again, that's what we do every day as we work on what amount to machinas of abstraction.
- Nadya 9y agoWhat use is Chinese for someone who doesn't read Chinese? The S combinator written like that is perfectly legible and fine for someone who is already familiar with the S Combinator. I don't see the problem there and I don't already grok the S Combinator. I do appreciate writing code under the assumption people working on the code would have some level of prerequisite knowledge. I'm sure you'd get a kick out of Co-dfns [0]. But once you learn to "speak" APL programming it makes a lot more sense. I'd highly recommend, if you have the time, to listen to the author explaining the code for the compiler [1]. [0] https://github.com/Co-dfns/Co-dfns/blob/master/codfns.pdf https://github.com/Co-dfns/Co-dfns/blob/master/codfns.pdf [1] https://www.youtube.com/watch?v=gcUWTa16Jc0 https://www.youtube.com/watch?v=gcUWTa16Jc0 / HN discussion: https://news.ycombinator.com/item?id=13638086 https://news.ycombinator.com/item?id=13638086
- marcosdumay 9y agoStandard names are good names. That said, Math is very contextual while development has many varying scope levels. Single letters identifiers are normally not a good choice in a global context because of collisions, however good names they may be.
- deleted 9y ago[deleted]
- johnhenry 9y agoI think you've interpreted this as meant to be useful tool for programmer -- and there your criticisms would be spot on. However, this is more of an interesting note for mathematicians than anything else.
- bluetwo 9y agoI agree with your gripe. We are in a field where backgrounds vary widely. A little "what am I looking at and why do I care" would be useful on this page.
- joeframbach 9y agoconst S = f => g => x => f(x)(g(x)); I'm more likely to write const geed = g(x); const effed = f(x); const S = effed(geed); I have no idea when I would actually write a structure like this though. Can someone please fill those names in with something like `buildComparator` or `Math.max.apply` or something, anything, that remotely makes sense?
- Jtsummers 9y agoYou'd use something like this when `f` and `g` are also parameters to your function. some_function(f, g, x) { var effed = f(x); var geed = g(x); var S = effed(geed); // some other logic } Not the S combinator, but consider being able to pass a comparator function to your sort function in C. Or passing an arbitrary predicate to `filter` or an arbitrary binary function/operator to `fold`. The S combinator itself may not be that useful, but higher order functions in general are very useful. You'll also see their utility if you use tacit programming (programming without variables). Again, maybe not the explicit combinators listed here, but the mode of operating is the same. Tacit programming is easily explored through J, APL (less accessible due to the character set), the various MLs and Haskell.
- dmitriid 9y agoIt's one of those things that you arrive at yourself. You rarely, if ever, understand them from Haskell-ish mathematical-ish constructs like these. Sometimes you could end up with this code in situations like this: const data = getDataForUser(user); const transformer = getTransformersForGDPR(); const sanitizedData = transformer(data); where getTransformersForGDPR returns a single function that may contain a chain of data transformations. The example is contrived, of course, but you do end up with similar constructs especially if you have been exposed to functional programming.
- joeframbach 9y agoAh, that does make sense. I think this happens in the React world sometimes, where a single state holds the source of truth for everything, and we end up passing parts of it around to other methods to make decisions. const data = getDataFromState(state); const transformer = getTransformersForState(state); const sanitizedData = transformer(data);
- msla 9y agoCombinators aren't just programming, they're primarily mathematics. If you think mathematical notation needs to be more verbose, say that, but at least have some idea of what you're saying before you try to dump this supposed problem on a different field.
- dmitriid 9y agoThat's why this exists: https://github.com/hemanth/functional-programming-jargon https://github.com/hemanth/functional-programming-jargon :)
- demircancelebi 9y agoYour comment reminded me one of the essays of PG. http://paulgraham.com/power.html http://paulgraham.com/power.html Although I agree with you in general, I think it actually can make us smarter if some piece of code requires us to think on it to internalize.
- z3t4 9y agoThis is why I don't like math. I guess mathematicians write like that because it's faster to write on a blackboard. When I do it myself in code I always go back and rename those single letter variables to proper names. It forces me to know what the code actually resembles, not just a formula I learned by heart.
- Roboprog 9y agoI’m not going to defend single letter names (although shortness does have some virtue compared to some monstrous naming conventions), but... Most math texts have a fair amount of documentation when formulas are introduced. Perhaps substantially more than the table in this article.
- kazinator 9y agoMath papers on the other hand assume that the reader has recursively traversed and read all of the citations and knows most of the notation.
- xyzzyz 9y agoOf course, that’s the whole point of citations and notation. Do you want every single paper to be 500+ pages? I have better things to do trying to figure out where the novel result is in hundreds of pages of the same shit I’ve seen dozens of time already.
- mikekchar 9y agoWhat I find interesting is that I actually don't mind the single letters at all. As others have said, a name is a name and I don't find either "substitution" or "ap" any more enlightening than "S". I also find that the currying of the parameters in the JS code is hard to read. As soon as I see the type signatures in the next chart, though, it becomes obvious what it's doing.
- kmill 9y agoIncidentally, the S combinator shows up in the J language as the "monadic hook". If f and g are functions, where f takes two arguments and is written infix (so "x f y" is how you write the application of the arguments x and y to f), (f g) x equals x f (g x). It's useful for certain idioms, like "equals rounddown" (actually written =<.) to mean "is an integer". The thing with these combinators is that they are so low level, so names for them are almost useless. If you were to program with them directly, you'd learn to use and recognize useful sequences of them. At least that's the theory for APL-like languages like J.
- tathougies 9y agoCalm down... The SKI combinators are not meant to be used by people. They are an assembly-like notation developed for language implementors. Your critique is like someone claiming imperative coders are horrible and don't value naming because of assembly language.