DEV Community

Iven Marquardt
Iven Marquardt

Posted on

2 1

f is not the same as x => f(x)

f is not the same as x => f(x) when it comes to evaluation in a strictly evaluated language like Javascript. The latter renders a function slightly more lazy:

const mapFold = f => acc => ix => {
  for (let [i, x] of ix)
    acc = f(acc) (x);

  return acc;
};

const arrSnoc = xs => x =>
  (xs.push(x), xs);

const mapToArr =
  mapFold(arrSnoc) ([]);

const mapToArr_ = ix =>
//                ^^
  mapFold(arrSnoc) ([]) (ix);
//                      ^^^^

const foo = new Map([[0, "foo"], [1, "bar"], [2, "baz"]]);

mapToArr(foo);
mapToArr_(foo);

mapToArr(foo); // ["foo", "bar", "baz", "foo", "bar", "baz"]
mapToArr_(foo); // ["foo", "bar", "baz"]
Enter fullscreen mode Exit fullscreen mode

mapToArr gets a fesh array as accumulator each time it is called and hence keeps the side effect caused by arrSnoc local. Adding redundant lambda abstractions to a derived function is called eta abstraction and the opposite operation eta reduction.

Read more on lazy evaluation.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay