3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

高階関数:関数を操作する関数で設計を開く

Posted at

概要

**高階関数(Higher-Order Function)**は、関数型プログラミングの根幹をなす考え方である。
関数を「引数」として扱い、「戻り値」として返すことで、ロジックを動的に構築できる。

この仕組みは制御の抽象化・再利用性・柔軟な設計を可能にし、
「関数=値」というパラダイムを支える力そのものだ。

本稿では TypeScript をベースに、高階関数の設計的意義と実装応用を明快に解説する。


1. 高階関数とは何か?

関数を引数にとる、または関数を返す関数

function withLogging<T>(fn: (arg: T) => T): (arg: T) => T {
  return (arg: T) => {
    console.log('Before:', arg)
    const result = fn(arg)
    console.log('After:', result)
    return result
  }
}
  • withLogging は関数を引数として受け取り、拡張された関数を返す
  • ロジックに「追加の振る舞い」を注入できる

2. 実用例①:ロジックのラッピング

バリデーションロジックを共通化する

function validate<T>(fn: (arg: T) => void): (arg: T) => void {
  return (arg: T) => {
    if (arg == null) throw new Error('Input is null or undefined')
    fn(arg)
  }
}

const saveUser = validate((user: { name: string }) => {
  console.log(`Saving ${user.name}`)
})

saveUser({ name: 'Alice' }) // OK
saveUser(null) // throws
  • どんな処理にも 「共通の前処理」 を差し込める
  • 抽象化されたパターンが設計を開放的にする

3. 実用例②:関数生成(関数を返す)

function makeMultiplier(factor: number): (x: number) => number {
  return (x: number) => x * factor
}

const double = makeMultiplier(2)
const triple = makeMultiplier(3)

double(4) // => 8
triple(4) // => 12
  • パラメタライズされた関数を動的に生成できる
  • 依存や振る舞いを閉じ込めた小さな関数を量産可能

4. 実用例③:カリー化(Curry)

function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R {
  return a => b => fn(a, b)
}

const add = (a: number, b: number) => a + b
const curriedAdd = curry(add)

const add5 = curriedAdd(5)
add5(3) // => 8
  • 高階関数の応用で、関数の部分適用が可能になる
  • 設計の流動性が高まる

5. 設計判断フロー

① この処理は共通化できるパターンか? → YES → 高階関数でラップ可能

② ロジックの一部だけを注入したいか? → YES → 関数を引数として受け取る設計に

③ 関数を返すことで使い分けたいか? → YES → クロージャとして設計

④ 多段階の変換があるか? → YES → Curry・Composeと組み合わせる

よくある誤解と対策

❌ 「高階関数は読みづらい」

→ ✅ 関数名と責務を明確に定義すれば、むしろ処理の流れが見通しやすくなる


❌ 「ただの関数の入れ子と何が違うの?」

→ ✅ 高階関数は関数レベルでの設計テンプレート。設計を抽象化するための枠組み


❌ 「使いこなせるのは上級者だけ」

→ ✅ map, filter, reduce全て高階関数。身近に使われている技術


結語

高階関数とは、関数を操作対象とすることで設計そのものを柔軟にする抽象機構である。

  • 関数を受け取り、振る舞いを拡張し
  • 関数を返し、構成と依存を分離し
  • 関数を束ね、流れるような設計へと昇華する

関数型設計における高階関数とは、
“静的なロジックを、動的な構成可能性へと解き放つ構文的構造化である。”

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?