替换 fp-ts 中的数组元素
Replace array element in fp-ts
如何将下面数组中的字符串 cat
替换为 dog
?
我下面的例子是标准 js 和 fp-ts 的混合,很好奇 Array 模块中是否有什么东西可以解决这个问题。
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']
我会使用 A.updateAt
。请注意,我还包装了 O.none
条件结果,因此它们都是条件 return 和 Option<string[]>
O.matchW(
() => O.some(animals),
(index) => A.updateAt(index, 'dog')(animals)
)
如何将下面数组中的字符串 cat
替换为 dog
?
我下面的例子是标准 js 和 fp-ts 的混合,很好奇 Array 模块中是否有什么东西可以解决这个问题。
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']
我会使用 A.updateAt
。请注意,我还包装了 O.none
条件结果,因此它们都是条件 return 和 Option<string[]>
O.matchW(
() => O.some(animals),
(index) => A.updateAt(index, 'dog')(animals)
)