Effect CommunityEC
Effect Community2y ago
12 replies
Jérôme MARTIN

Conditional Function Application in a Pipe

I often happen to need to apply conditionally a function in a pipe. For instance:
pipe(ReadonlyArray.make(3, 6, 4), (arr) => (options.sort ? ReadonlyArray.sort(arr, Order.number) : arr));
So I finally created an iif function:
export const iif =
    <A>(cond: (a: A) => boolean, onTrue: (a: A) => A) =>
    (a: A) =>
        cond(a) ? onTrue(a) : a;

So now I can write:
pipe(ReadonlyArray.make(3, 6, 4), iif(()=>options.sort,ReadonlyArray.sort(Order.number));
But I have a feeling there could be a cleverer solution? Maybe with options?
Was this page helpful?