Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 1.42 KB

either.md

File metadata and controls

46 lines (39 loc) · 1.42 KB
title tags author_title author_url author_image_url description image
either
function,logic,beginner
Deepak Vishwakarma
Implementation of "either" in typescript, javascript and deno.

TS JS Deno

Returns true if at least one function returns true for a given set of arguments, false otherwise.

Use the logical or (||) operator on the result of calling the two functions with the supplied args.

export const either = (f: Function, g: Function) => (...args: any[]) =>
  f(...args) || g(...args);
const isEven = (num: number) => num % 2 === 0;
const isPositive = (num: number) => num > 0;
const isPositiveOrEven = either(isPositive, isEven);

isPositiveOrEven(4); // true
isPositiveOrEven(3); // true

interface User {
  name: string;
  age: number;
}
const user1: User = {
  name: "deepak",
  age: 18,
};
const user2: User = {
  name: "Martha",
  age: 21,
};
const isDeepak = (u: User) => u.name === "deepak";
const isAdult = (minAge: number, u: User) => u.age > minAge;