2

Consider the following implementation of take:

const take = (n, [x, ...xs]) =>
    n === 0 || x === undefined ?
    [] : [x, ...take(n - 1, xs)];

console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5]
console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3]
console.log(take(1, [undefined, 1]));  // []

As you can see, it doesn't work for arrays with undefined because x === undefined is not the best way to test whether an array is empty. The following code fixes this problem:

const take = (n, xs) =>
    n === 0 || xs.length === 0 ?
    [] : [xs[0], ...take(n - 1, xs.slice(1))];

console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5]
console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3]
console.log(take(1, [undefined, 1]));  // [undefined]

However, writing xs[0] and xs.slice(1) isn't as elegant. In addition, it's problematic if you need to use them multiple times. Either you'll have to duplicate code and do unnecessary extra work, or you'll have to create a block scope, define constants and use the return keyword.

The best solution would be to use a let expression. Unfortunately, JavaScript doesn't have them. So, how to simulate let expressions in JavaScript?

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299

2 Answers2

6

In Lisp, a let expression is just syntactic sugar for a left-left lambda (i.e. an immediately-invoked function expression). For example, consider:

(let ([x 1]
      [y 2])
  (+ x y))

; This is syntactic sugar for:

((lambda (x y)
    (+ x y))
  1 2)

In ES6, we can use arrow functions and default parameters to create an IIFE that looks like a let expression as follows:

const z = ((x = 1, y = 2) => x + y)();

console.log(z);

Using this hack, we can define take as follows:

const take = (n, xxs) =>
    n === 0 || xxs.length === 0 ?
    [] : (([x, ...xs] = xxs) => [x, ...take(n - 1, xs)])();

console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5]
console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3]
console.log(take(1, [undefined, 1]));  // [undefined]

However, it would be best to use statements instead of expressions here.

const take = (n, xxs) => {
  if (n === 0 || xxs.length === 0) return [];
  const [x, ...xs] = xxs;
  return [x, ...take(n - 1, xs)];
};

console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5]
console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3]
console.log(take(1, [undefined, 1]));  // [undefined]
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • Using default parameter values is definitely what I would consider a hack :-) – Bergi Jul 18 '18 at 17:20
  • Could as well pass the value(s) as arguments instead of using default params, no? – Felix Kling Jul 18 '18 at 17:22
  • @FelixKling Indeed. However, it just looks more like a let expression when you use default parameters. – Aadit M Shah Jul 18 '18 at 17:23
  • A hack is just an euphemism for incorrect use of the language. Instead, you should reduce the need for `let` expressions in JS by introducing memoization, for instance. –  Jul 19 '18 at 14:56
  • @ftor I agree, hacks should be kept to a minimum. However, I fail to see how memoization would help reduce the need for let expressions. – Aadit M Shah Jul 19 '18 at 14:59
  • Because you don't need to assign the result of a memoized function to a variable, but only have to pass the same argument. This will at least cover some of the use cases of let expressions, right? –  Jul 19 '18 at 15:13
  • @ftor No. They are used for very different things. – Aadit M Shah Jul 19 '18 at 15:14
  • 2
    @Bergi It's quite hacky, but I love it because it's much more cleaner than `(() => {const x = 1; const y = 2; return x + y;})()` – Wong Jia Hau Sep 23 '19 at 07:33
  • IMO it's actually pretty readable. Arrow IIFEs(e.g. `(() => 0)()`) are already pretty common in js, so `((a=1, b=2) => a+b))()` reads pretty easily as "let a=1 and b=2 return a+b". Definitely better than `((a, b) => a+b)(1, 2)`. Although I kind of wish that `let` & `const` could be used in expressions and would evaluate to their right hand side the same way that implicit global declaration works. – emegolf123 Jan 23 '20 at 17:22
0

Instead of using an IIFE just use a normal function with a proper name to make things more explicit:

const _let = f =>
  f();

const collateBy = f => xs =>
  xs.reduce((m, x) =>
    _let((r = f(x), ys = m.get(r) || []) =>
      m.set(r, (ys.push(x), ys))), new Map());

const includes = t => s =>
  s.includes(t);

xs = ["Dev", "Jeff", "Kalib", "Amy", "Gemma"];

const collation = collateBy(includes("e")) (xs);

console.log(collation.get(true));
console.log(collation.get(false));