跳到正文
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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我: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:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我怎样检查一个邮箱地址是否有效,如果无效,就显示一条清晰的错误提示?”

每周 5 个新技能,1 分钟读完。

工作原理

它的工作方式是定义两条路径:一条是成功路径(带有预期结果),另一条是失败路径(带有问题描述)。然后你可以根据结果选择走哪条路径。

技术内容(原始 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”代表失败,里面装的是发生的错误信息或错误类型。

我可以把它用在可能抛出错误的代码上吗(比如读取文件)?

可以。这个工具提供了一个专门的函数(`tryCatch`),可以把可能出错的操作转换成“Right”(成功)或“Left”(失败)的结果。

如果结果可能是成功,也可能是错误,我怎样拿到最终结果?

你可以使用 `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

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能