最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - array filtering of prime numbers with fat arrows - Stack Overflow

programmeradmin2浏览0评论

Learning how to incorporate fat arrows to write some clever array filtering.

const newArray = [1, 3, 2, 5, 10];
const isPrime = num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1;
};


const myPrimeArray = newArray.filter(element => isPrime(element));
console.log(myPrimeArray);

Is there a way I can incorporate the function isPrime into the fat arrow .filter inline?

Learning how to incorporate fat arrows to write some clever array filtering.

const newArray = [1, 3, 2, 5, 10];
const isPrime = num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1;
};


const myPrimeArray = newArray.filter(element => isPrime(element));
console.log(myPrimeArray);

Is there a way I can incorporate the function isPrime into the fat arrow .filter inline?

Share Improve this question asked Feb 2, 2019 at 18:24 NanoNetNanoNet 3241 gold badge3 silver badges13 bronze badges 2
  • It's not entirely clear (as you can see from the answers), did you want to remove the reference to isPrime so that the function is defined only inside the .filter, or did you want to remove the manual call of isPrime(element) inside the .filter? – CertainPerformance Commented Feb 2, 2019 at 18:31
  • Your code is fine as it is. isPrime is a reusable function. You could probably move it to a separate utils module and import it wherever you need it. Any more changes to this code will be trivial and possibly make it hard to read. – adiga Commented Feb 2, 2019 at 18:35
Add a ment  | 

6 Answers 6

Reset to default 2

Just pass the function reference instead of calling it within an anonymous function in filter()

const myPrimeArray = newArray.filter(isPrime);

Regardless of being written as an arrow function or not the end result is it is still just a function object

Just copy and paste the body of isPrime into the filter callback:

const newArray = [1, 3, 2, 5, 10];
const myPrimeArray = newArray.filter(num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1;
});
console.log(myPrimeArray);

But that's not so readable IMO - I prefer your version, named functions are nice for operations that aren't extremely trivial.

The filter function accepts arrow function, or fat arrow function if you wish :

const myPrimeArray = newArray.filter(isPrime);

Working example :

const newArray = [1, 3, 2, 5, 10];
const isPrime = num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1;
};


const myPrimeArray = newArray.filter(isPrime);
console.log(myPrimeArray);

Just move the function isPrime in callback of filter

const newArray = [1, 3, 2, 5, 10];


const myPrimeArray = newArray.filter(num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1;
});
console.log(myPrimeArray);

const newArray = [1, 3, 2, 5, 10];
const myPrimeArray = newArray.filter(element => {
    for (let i = 2; i < element; i++) {
        if (element % i === 0) return false;
    }
    return element !== 1;
});
console.log(myPrimeArray);

You can use point-free notation (wikipedia), often used in functional programming, and omit the argument. Instead of giving an arrow function taking an argument and calling your isPrime function with that single argument, just pass your isPrime function:

arg => isPrime(arg) is equivalent to isPrime, so you can do this:

const myPrimeArray = newArray.filter(isPrime);

Or you can define your arrow function where it is used, as argument to your filter call. However, this code is less readable and reusable than defining an isPrime function as you did.

const newArray = [1, 3, 2, 5, 10];

const myPrimeArray = newArray.filter(num => {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1 && num !== 0;
});

console.log(myPrimeArray);

Warning: isPrime(0) should return false, add a condition at the end for that.

You can also decrease the plexity of the algorithm from O(n) to O(sqrt(n)) and get the result faster if you only loop until the square root of the number:

const newArray = [1, 3, 2, 5, 10];

const myPrimeArray = newArray.filter(num => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
    if (num % i === 0) return false;
  }
  return num !== 1 && num !== 0;
});

console.log(myPrimeArray);

发布评论

评论列表(0)

  1. 暂无评论