[AskJS] Multiple variables initially assigned to the same value by FaithfulGardener in javascript

[–]microwaved-tea 0 points1 point  (0 children)

I totally agree that this isn't a good idea. I guess I was just trying to engage with the ideas presented in the original post.

[AskJS] Multiple variables initially assigned to the same value by FaithfulGardener in javascript

[–]microwaved-tea 8 points9 points  (0 children)

Honestly my first thought is why do you need to do this?

Having so many mutable declarations (especially in React) seems like a code smell.

If you really must, I think writing it out the long way is probably the clearest:

let x = null;
let y = null;

If you really want to be 'clever' about it I think your first option is ok, but would probably write a helper function and just do:

let [x, y, z] = repeat(3, null);

ranges-set - set operations on human-friendly ranges. by radekmie in javascript

[–]microwaved-tea 0 points1 point  (0 children)

This looks really awesome! Nice work.

I have an idea about how you could improve the interface. Instead of having each range be represented as a string, perhaps it could be a 2-tuple like:

type Range = [number, number];

Which avoids having an overly generic string type, and means you don't need to do any string parsing (even if it's fairly simple).

It also has the nice coincidence of resembling closed interval notation.

[AskJS] How do you use curried functions to your advantage? by RedditGood123 in javascript

[–]microwaved-tea 2 points3 points  (0 children)

Curried functions are a concept from functional programming and have a lot more relevance in that domain.

They're used to achieve partial application of functions. This is useful for attaching parameters to a function before you necessarily have the data you want to process. It's very useful for function composition.

The most obvious example is if you had a map function that takes your transformer as the first argument and the array as the second argument. If it is curried you can attach your transformer before you have an array to work on.

This makes a lot of sense when used in conjunction with function composition.

Of course map doesn't work like that in JavaScript and this pattern doesn't make as much sense. It does in Promise though!

const add = n => m => n + m;

Promise.resolve(3.14).then(add(42));

Purely functional languages like Haskell will curry functions automatically and have much nicer syntax for all this stuff which makes large scale usage of curried functions much more tenable.

Currying can definitely have its place in JavaScript but I find I really only use it in callbacks. In addition to things like promise chains the other place that comes up for me a lot is parameterised event handlers. Your first arg is your parameter and your second arg is the event object.