Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 1.07 KB

complement.md

File metadata and controls

32 lines (27 loc) · 1.07 KB
title tags author_title author_url author_image_url description image
complement
function
logic
beginner
Deepak Vishwakarma
Implementation of "complement" in typescript, javascript and deno.

TS JS Deno

Returns a function that is the logical complement of the given function, fn.

Use the logical not (!) operator on the result of calling fn with any supplied args.

type Func<T> = (...args: T[]) => any;
const complement =
  (fn: Func<any>) =>
  (...args: any[]) =>
    !fn(...args);
const isEven = (num: number) => num % 2 === 0;
const isOdd = complement(isEven);
isOdd(2); // false
isOdd(3); // true