JavaScriptで関数型プログラミング 関数の結果を反転させる

引数が1ならtrueを返す関数があります。

const isOne = value => value === 1;

関数を反転させる not 関数を定義します。

const not = func => {
	return arg => {
  	return !func(arg);
  }
};

not 関数に先ほどの isOne 関数を渡して結果を得ます。

const not = func => {
	return arg => {
  	return !func(arg);
  }
};

const isOne = value => value === 1;

const isNotOne = not(isOne); // not関数を使って、isOne 関数を反転させる

console.log(isNotOne(1)); // false
console.log(isNotOne(2)); // true

既存の関数から、新たな関数(結果が反転した関数)を作成することができました。