Functional Implementation of Co-prime pairs in array Program finds the number of pairs of co-primes in an array using 
functional concepts in Node.js

const assert = require('assert')

const gcd = (a, b) =&gt; (b != 0) ? gcd(b, a % b) : a

const co_prime = (a, b) =&gt; gcd(a, b) == 1

const co_prime_pairs = (_list) =&gt; {
    co_primes = []
    _list.map(
        (x, i) =&gt; _list.slice(i + 1)
                        .filter(y =&gt; co_prime(x, y))
                        .forEach(y =&gt; co_primes.push([x, y]))
        )
    return co_primes.length
} 

const main = () =&gt; {
    assert.equal(co_prime_pairs([1, 2, 3]), 3)
    assert.equal(co_prime_pairs([4, 8, 3, 9]), 4)
}

main()


Is the time complexity similar to that of the simple for-loop implementation?

Is there any scope for improving the time-complexity by using map and filter functions?

How is my choice of variable names?

How is code formatting?
How is my choice of variable names?


In general you used snake case for the function names which is more a ruby-style.  

Additionally the function co_prime_pairs let me acept that it return pairs of co-primes. However it returns a number of co-pairs inside an array.  

The variable co_primes is declared globally.

co_prime_pairs

This function is not a pure function internally because .forEach(y =&gt; co_primes.push([x, y])) modifies co_primes.
I rewrite the function and spit the logic into:


build pairs
filter the co primes
return the length


The functions:

const filterCoPrimes = xss =&gt;
    xss.filter(xs =&gt; isCoPrime(xs[0], xs[1]))

const buildPair = x =&gt; y =&gt;
    [x, y]

const buildPairs = (xs, pairs) =&gt;
    xs.length === 0 || xs.length === 1
        ? pairs
        : buildPairs(
            xs.slice(1), 
            pairs.concat(xs.map(buildPair(xs[0])))
        )


The chain:

const co_prime_pairs = xs =&gt;
    filterCoPrimes(buildPairs(xs, [])).length


If we use pipe as a kind of function composition it can be rewritten to:

const co_prime_pairs = pipe(
    buildPairs([]),
    filterCoPrimes,
    length
)


Full working Example


function calls:




const gcd = (a, b) =&gt; 
  (b != 0) ? gcd(b, a % b) : a

const isCoPrime = (a, b) =&gt; 
  gcd(a, b) === 1

const filterCoPrimes = xss =&gt;
  xss.filter(xs =&gt; isCoPrime(xs[0], xs[1]))

const buildPair = x =&gt; y =&gt;
  [x, y]

const buildPairs = (xs, pairs) =&gt;
  xs.length === 0 || xs.length === 1
      ? pairs
      : buildPairs(
          xs.slice(1), 
          pairs.concat(xs.map(buildPair(xs[0])))
      )

const co_prime_pairs = xs =&gt;
  filterCoPrimes(buildPairs(xs, [])).length

console.log(co_prime_pairs([4, 8, 3, 9]))





with pipe:




const pipe = (...fns) =&gt; fns.reduce((f, g) =&gt; (...args) =&gt; g(f(...args)))

const filter = fn =&gt; xs =&gt;
    xs.filter(fn)

const map = fn =&gt; xs =&gt;
    xs.map(fn)

const length = xs =&gt; 
    xs.length

const gcd = (a, b) =&gt; 
    (b != 0) ? gcd(b, a % b) : a

const isCoPrime = (a, b) =&gt; 
    gcd(a, b) === 1

const filterCoPrimes = xss =&gt;
    xss.filter(xs =&gt; isCoPrime(xs[0], xs[1]))

const buildPair = x =&gt; y =&gt;
    [x, y]

const buildPairs = pairs =&gt; xs =&gt;
    xs.length === 0 || xs.length === 1
        ? pairs
        : buildPairs
            ( pairs.concat(xs.map(buildPair(xs[0]))) )
            ( xs.slice(1) )

const co_prime_pairs = pipe(
    buildPairs([]),
    filterCoPrimes,
    length
)
    
console.log(co_prime_pairs ([4, 8, 3, 9]))