up:: Programming

関数を引数に取る関数、もしくは関数を返す関数を高階関数という。関数に関数入ってる感じの奴。
js コールバック関数は引数に関数を取ってるので高階関数。

関数を返す場合はこんな感じ。

// counterFactory()は「cntをインクリメントしコンソール表示する関数」を戻り値とするため高階関数である
const counterFactory = (): () => void => {
  let cnt: number = 0;
  return () => console.log(++cnt);
}
 
// counterFactory()を実行して関数を生成
const counter1: () => void = counterFactory();
const counter2: () => void = counterFactory();
 
counter1(); // --> 1
counter1(); // --> 2
counter1(); // --> 3
 
counter2(); // --> 1
counter2(); // --> 2

カリー化は複数の引数を取り戻り値に関数をセットしてある高階関数のうち、戻り値の関数に元の関数の引数の一部が入るようにしたやつ。
どうもしっくりこないが、引数を使う部分ごとに関数内部を分割した感じの関数。これを使うと関数を部分適用し、よく似た別の関数を簡単に作れるようになったりする。

const fetch = (method: 'POST' | 'GET') => (url: string) => (param: {}) => {
    return () => {
      // 3種類の引数を使って非同期処理を実行...
    }
}
 
const post = fetch('POST');
const get = fetch('GET');
 
const postHoge = post('http://hogehogehogehoge');
const postFuga = post('http://fugafugafugafuga');
const getFuga = get('http://fugafugafugafuga');
 
const postHogeXXXX = postHoge({value: 'xxxx'});
const postFugaXXXX = postFuga({value: 'xxxx'});
const getFugaNoParam = getFuga({});
 
postHogeXXXX();
postFugaXXXX();
getFugaNoParam();

こんな感じで、引数一つずつ変えたプリセットが作れる。

【TS】今さら聞けない高階関数・カリー化
食べられないほうのカリー化入門 - Qiita

Builder