Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

1

First point Don't add quotes around property names when defining JS objects. [{'duration': 3600}, {'duration': 3600}] // should be [{duration: 3600}, {duration: 3600}] You state "And I want the output to be {3600: 2}, where 2 is the number of occurrences of 3600." Which is not that clear. Going by your code I assume you want [{duration: 60}, {...


2

To reduce the verboseness of the code, you should extract the main idea of that code into a function: function histogram(data, key) { const count = {}; for (let item of data) { const value = data[key]; count[value] = (count[value] || 0) + 1; } return count; } That way you can write your code as: console.log(histogram(...


3

It's not really clear what you mean by "functional". The use of reduce is nifty, and correctly done, but it seem like cargo cult programming. The realization that a lot of loop-like actions can be represented in terms of reduce (fold, aggregate, etc) is important to functional programing, but it should no more be your first choice than a while-loop. The ...


3

Re 1: You can take advantage of reduce(into:_:) which was introduced in Swift 4 precisely for this purpose: This method is preferred over reduce(_:_:) for efficiency when the result is a copy-on-write type, for example an Array or a Dictionary. See also SE-0171 Reduce with inout: Motivation The current version of reduce needs to make copies of ...


0

Bug: singleton objects have no allocation record Since the garbage collector will try to set the mark() in a SYMBOL object, the T_ object needs a dummy allocation record. NIL_ doesn't need one since an INVALID object will not get marked. pc9obj.c: object T_ = &(1[(union uobject[]){{ .t = 0 },{ .Symbol = { SYMBOL, T, "T" } }}]), NIL_ = (union ...


2

Opportunities missed You have essentially the same algorithm in both examples. When I first looked at the solutions the sort at the start was a worry. The logic is sound, to work from the smallest word as there will be no more characters to test then is contained in that word. However you have the sort AoT and the result is you set first to be the longest ...


1

You can often replace switch statements with lookup functions. For example function returnsHome(moves) { var v = 0, h = 0; const dirs = { U() {v++}, D() {v--}, L() {h--}, R() {h++} }; for (const move of moves) { dirs[move]() } return !(h || v); } // or function returnsHome(moves) { var v = 0, h =...


2

It could be made more functional by avoiding the mutation of acc. The effects are contained within judgeCircle so it's not a big deal, but it feels like if you're going to mutate the accumulator, you might as well just use an imperative loop. I also preferred to be explicit about the final check. I find the intent of !h && !v isn't quite as clear as ...


3

If this was computer graphics each operation would be called a transform. You would never do two or more transforms in sequence you would combine the transforms into a single transform and apply that to the image. As in this case the transforms are abstracted and thus hard coded you can avoid the need to combine transforms and just hard code the combined ...


3

Flipping binary numbers (i.e. bits) from 0 to 1 or vise-versa can be achieved with bitwise operations, like XOR. This may give a slight performance increase in some browsers - refer to this jsPerf. Instead of the ternary: .map(x => x.map(i => i ? 0 : 1)); Use XOR: .map(x => x.map(i => i ^ 1)); Unless I am mistaken, you should be able to ...


2

I agree that this generalization is not particularly useful: to abstract a part of a program to a functional parameter is meaningful when you can reuse that program with a class of different functions, not with two operators like < and >. For this, it is sufficient to define a boolean parameter: (defun most (fn lst &key lowest) "What's the ...


1

You said to ignore getWeekStart(), but I'm afraid I can't do that. It appears that the intention of getWeekStart() is to take a date string (in YYYY-MM-DD form) and find the Sunday that starts the week (in YYYY-MM-DD form). However, depending on the time zone in which the code is executed, it may actually find the Monday, or possibly a Saturday instead. ...


1

Bug Your second functional solution does not run. You forgot to add the second argument to A.reduce. I will assume you wanted an array as the last argument. Why functional sucks This example clearly illustrates the problem with some functional solutions that involve data manipulation. The requirement of no side effects forces the solution to copy the ...


Top 50 recent answers are included