0

How would I go about replacing the string cat with dog in the array below?

My example below is a mix of standard js and fp-ts, was curious if there was something in the Array module that would solve this.

const animals = ['monkey','cat','lion']

const result = pipe(
    animals,
    A.findIndex((animal)=> animal === 'cat'),
    O.matchW(
      () => animals,
      (index) => //Looking for fp-ts solution here
    )
  )

//Expected Result: ['monkey','dog','lion']
Jonathan Southern
  • 1,121
  • 7
  • 22

1 Answers1

0

I would use A.updateAt. Notice I also wrap O.none condition result so they both conditions return an Option<string[]>

O.matchW(
  () => O.some(animals),
  (index) => A.updateAt(index, 'dog')(animals)
)
Jonathan Southern
  • 1,121
  • 7
  • 22