本文へスキップ
Skillpedia
一般的なプログラミング48k · #4641

fp-either-ref

できること

このツールは、コンピュータプログラムのエラーを整理された方法で管理するのに役立ちます。何かが成功したか(値がある)、失敗したか(具体的なエラーメッセージがある)を明確に扱えるため、プログラムが予期せず停止することを防げます。

使う場面

  • ✓データを使う前に、それが正しいかどうかを確認したいとき。
  • ✓ファイルの読み込みなど、失敗する可能性がある処理を行うとき。
  • ✓エラーを発生させるコードを、プログラムが「成功」か「失敗」として理解できる形に変換したいとき。

スキルのリンク

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-either-ref/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-either-ref/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“メールアドレスが有効かどうかを確認し、無効な場合は分かりやすいエラーメッセージを表示するにはどうすればよいですか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

成功の場合(期待される結果を持つ)と失敗の場合(問題の説明を持つ)の2つの道筋を定義することで動作します。結果に応じて、どちらの道筋を進むかを選べます。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Either Quick Reference

Either = success or failure. Right(value) or Left(error).

When to Use

  • You need a quick fp-ts reference for typed synchronous error handling.
  • The task involves validation, fallible operations, or converting throwing code to Either.
  • You want a compact cheat sheet rather than a long tutorial.

Create

import * as E from 'fp-ts/Either'

E.right(value)           // Success
E.left(error)            // Failure
E.fromNullable(err)(x)   // null → Left(err), else Right(x)
E.tryCatch(fn, toError)  // try/catch → Either

Transform

E.map(fn)                // Transform Right value
E.mapLeft(fn)            // Transform Left error
E.flatMap(fn)            // Chain (fn returns Either)
E.filterOrElse(pred, toErr) // Right → Left if pred fails

Extract

E.getOrElse(err => fallbackValue)  // Get Right or default
E.match(onLeft, onRight)     // Pattern match
E.toUnion(either)            // E | A (loses type info)

Common Patterns

import { pipe, identity } from 'fp-ts/function'
import * as E from 'fp-ts/Either'

// Toy validation; an @ check is not complete email validation
const validateEmail = (s: string): E.Either<string, string> =>
  s.includes('@') ? E.right(s) : E.left('Invalid email')

// Chain validations (stops at first error)
pipe(
  E.right({ email: 'test@example.com', age: 25 }),
  E.flatMap(d => pipe(validateEmail(d.email), E.map(() => d))),
  E.flatMap(d => d.age >= 18 ? E.right(d) : E.left('Must be 18+'))
)

// Convert throwing code
const parseJson = (s: string) => E.tryCatch(
  () => JSON.parse(s),
  (e) => `Parse error: ${e}`
)

vs try/catch

// ❌ try/catch - errors not in types
try {
  const data = JSON.parse(input)
  process(data)
} catch (e) {
  handleError(e)
}

// ✅ Either - errors explicit in types
pipe(
  E.tryCatch(() => JSON.parse(input), String),
  E.flatMap(data => E.tryCatch(() => process(data), String)),
  E.match(handleError, identity)
)

Use Either when error type matters and you want to chain operations.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.

Reference scope

These are fp-ts 2.x fragments. Supply application variables, types, validators and imports, and check the installed version. map does not catch thrown exceptions. Task has no typed error channel; an improperly wrapped promise can still reject. Use tryCatch at fallible boundaries.

Official API: fp-ts modules.

よくある質問

「Right」と「Left」の違いは何ですか?

「Right」は成功を表し、期待していた値を持ちます。「Left」は失敗を表し、発生したエラーメッセージや種類を持ちます。

エラーを発生させる可能性があるコード(ファイルの読み込みなど)でも使えますか?

はい。このツールには、エラーが起きうる処理を「Right」(成功)か「Left」(失敗)の結果に変換する専用の関数(`tryCatch`)があります。

成功かエラーかのどちらかになりうる場合、最終的な結果をどのように取り出しますか?

エラーが発生したときに備えてデフォルト値を指定する`getOrElse`や、成功かエラーかによって異なる処理を行う`match`などの関数を使えます。

詳細

作者による元の説明

Quick reference for Either type. Use when user needs error handling, validation, or operations that can fail with typed errors.

repo: sickn33/agentic-awesome-skills

人気

GitHubのスター
48k
ランキング
#4641
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル