5 ms·
const 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
by joeframbach 9y ago
const 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);
- ljm 9y agoIt looks like something you’d do when the function args are the wrong way around and you rely on currying, and you don’t have a clojure style ‘thread-last’ operator.