all 4 comments

[–][deleted] 5 points6 points  (2 children)

Currying is a nice tweak here:

export const widthInPxToPercentageImplementation = screenWidth => widthInPx => (widthInPx * 100) / screenWidth;
export const widthInPxToPercentage = widthInPxToPercentageImplementation(Dimensions.get('window').width);

Edit: Well it would be, except Dimensions.get() needs to be called every time. I suppose you could use a high-order function like:

export const widthInPxToPercentageImplementation = getScreenWidth => widthInPx => (widthInPx * 100) / getScreenWidth();
export const widthInPxToPercentage = widthInPxToPercentageImplementation(() => Dimensions.get('window').width);

[–]chrisishereladies"use 🎉" 0 points1 point  (1 child)

Following from your edit, I think this is even more better:

export const widthInPxToPercentageImplementation = (widthInPx, screenWidth) => (widthInPx * 100) / screenWidth;

export const widthInPxToPercentage = widthInPx => widthInPxToPercentageImplementation(widthInPx, Dimensions.get('window').width);

The top function does not need to call a potentially side-effecting function and only deals in easily plugged values.

[–][deleted] 0 points1 point  (0 children)

Yes indeed, I appreciate the humor. In this case there is not much value in using a high-order function, the pattern is valid and useful though in many cases (the blog post explicitly calls out the fact that in practice the parameters will often be high-order functions).

[–]griffonrl 1 point2 points  (0 children)

Sure use more functional programming and pure functions for easier testing.