0

I have implemented a lazy infinite sequence function along with 3 functions called take,reduce,map. the take function is similar to Haskell's implementation where it takes a finite sequence from the infinite sequence given a finite value and the usual reduce and map functions in Javascript that works with the finite sequence type.

Now I'm trying to evaluate values in the sequence with various conditions applied to the sequence values.

I'm stuck at implementing functions that takes in a number term and calculate the sequence total product and sum with every alternating sequence value sign is changed to a negative sign. Here's the function I implemented for that objective:

function alternatingProduct(term) {
    return reduce((x, y) => x * y, take(term, map(x => -x, generateSequence((v) => (v + 1))(1))), 1);
}

How the function supposed to work is it takes in the sequence lazily and changes every alternate value sign from positive to negative. So when sequence of 1 to 3 is generated for example, the 2 in the sequence should be negative, giving a total product of the whole finite sequence an answer of -6.

Initially I thought my code is working when I called

alternatingProduct(4) 

//where it returns 24.

alternatingProduct(3)

//where it returns -6. 

When i tried to implement a similar function to sum instead, that's when I realised my function is wrong.

function alternatingSum(term) {
    return reduce((x, y) => x + y, take(term, map(x => -x, generateSequence((v) => (v + 1))(1))), 0);
}

alternatingSum(3)
//should return 2 because of 1+(-2)+3  but it returned 5 instead 

alternatingSum(4)
//should return -2 because of 1+(-2)+3+(-4) but it returned -9 instead

Now I realised my function map changes every value in the sequence. I have no ideas how to change my code to only change alternating values of a lazy sequence. I am new to lazy sequence and evaluation and just experimenting with it so any help is appreciated

  • Tip: Read what [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) can do in the way of arguments. One of them is `index`. – tadman Aug 26 '20 at 06:11
  • 1
    @tadman it's a reduce function i created since the builtin function only works on Arrays. The infinite sequence i created is not Array type. Thanks for the pointer, I'll see what i can do to modify the reduce function – mightyandweakcoder Aug 26 '20 at 06:55
  • If you made yours behave in a similar manner you'd get the information you need. – tadman Aug 26 '20 at 20:52

1 Answers1

1

You can just use

map(x => x%2 ? x : -x, …)

so that odd values are kept and even values are negated.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Nice solution. What if I create a sequence of only odd numbers eg 1,3,5,7 and I still want to negate every alternate value? – mightyandweakcoder Aug 26 '20 at 08:35
  • @mightyandweakcoder Then check whether `x-1` is divisible by four? But in general you will want to have a `zipWith` helper that allows you to merge two sequences, such as one of arbitrary values and one of alternating `1`/`-1`. – Bergi Aug 26 '20 at 09:23