2

What is the idiomatic fp alternative of forEach part in this code

const ios: IO<void>[] = [...]
ios.forEach(io => io());

?

There are some docs which use sequence_ from Foldable but it is no longer available in fp-ts@2.0.3.

EDIT:

The best I could do is

import { array } from 'fp-ts/lib/Array';

array.sequence(io)(ios)();

but sequence collects the results and I do not really have anything to do with void[].

MnZrK
  • 1,330
  • 11
  • 23

1 Answers1

2

I was wondering the same thing. The following seems to work.

import { IO, io } from 'fp-ts/lib/IO';
import { traverse_ } from 'fp-ts/lib/Foldable';
import { array } from 'fp-ts/lib/Array';
import { identity } from 'fp-ts/lib/function';
import { log } from 'fp-ts/lib/Console';

const actions: Array<IO<void>> = [log('hello'), log('world')];
const action: IO<void> = traverse_(io,array)(actions,identity);

cyberixae
  • 843
  • 5
  • 15
  • So basically `sequence_` is `sg => xs => traverse_(sg, array)(xs, identity)`. Good to know. – MnZrK Aug 10 '19 at 16:27