Chuyển đến nội dung
Skillpedia
Lập trình chung48k · #4650

fp-ts-pragmatic

Skill này làm gì

Hướng dẫn này dạy cách dùng thư viện fp-ts trong TypeScript một cách thực tế. Nó tập trung vào các phần hữu ích nhất để viết mã sạch hơn, dễ bảo trì hơn, mà không đi sâu vào các khái niệm phức tạp.

Khi nào nên dùng

  • ✓Khi mới bắt đầu với fp-ts và cần các ví dụ đơn giản, dễ theo.
  • ✓Khi viết mã TypeScript xử lý các giá trị null, lỗi hoặc thao tác bất đồng bộ có thể xảy ra.
  • ✓Khi muốn cải thiện mã của mình mà không cần học toàn bộ lý thuyết lập trình hàm.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-ts-pragmatic/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm thế nào để xử lý các giá trị null có thể xảy ra trong TypeScript bằng fp-ts một cách đơn giản?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Hướng dẫn trình bày các mẫu và ví dụ mã áp dụng những khái niệm cốt lõi của fp-ts. Trọng tâm là ứng dụng trực tiếp để giải quyết các vấn đề lập trình thường gặp.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Pragmatic Functional Programming

Read this first. This guide cuts through the academic jargon and shows you what actually matters. No category theory. No abstract nonsense. Just patterns that make your code better.

Detailed Guide

Read the detailed guide before executing this skill. It retains the complete procedure and reference material. Treat its safety, prerequisites, and validation requirements as mandatory. For focused work, load the relevant sections; for end-to-end work, read the guide completely.

When to Use This Skill

  • When starting with fp-ts and need practical guidance
  • When writing TypeScript code that handles nullable values, errors, or async operations
  • When you want cleaner, more maintainable functional code without the academic overhead
  • When refactoring imperative code to functional style

When NOT to Use FP

Functional programming is not always the answer. Here's when to keep it simple.

Simple Null Checks

// Just use optional chaining - it's built into the language
const city = user?.address?.city ?? 'Unknown'

// DON'T overcomplicate it
const city = pipe(
  O.fromNullable(user),
  O.flatMap(u => O.fromNullable(u.address)),
  O.flatMap(a => O.fromNullable(a.city)),
  O.getOrElse(() => 'Unknown')
)

Simple Loops

// A for loop is fine when you need early exit or complex logic
function findFirst(items: Item[], predicate: (i: Item) => boolean): Item | null {
  for (const item of items) {
    if (predicate(item)) return item
  }
  return null
}

// DON'T force FP when it doesn't help
const result = pipe(
  items,
  A.findFirst(predicate),
  O.toNullable
)

Performance-Critical Code

// For hot paths, imperative is faster (no intermediate arrays)
function sumLarge(numbers: number[]): number {
  let sum = 0
  for (let i = 0; i < numbers.length; i++) {
    sum += numbers[i]
  }
  return sum
}

// fp-ts creates intermediate structures
const sum = pipe(numbers, A.reduce(0, (acc, n) => acc + n))

When Your Team Doesn't Know FP

If you're the only one who can read the code, it's not good code.

// If your team knows this pattern
async function getUser(id: string): Promise<User | null> {
  try {
    const response = await fetch(`/api/users/${id}`)
    if (!response.ok) return null
    return await response.json()
  } catch {
    return null
  }
}

// Don't force this on them
const getUser = (id: string): TE.TaskEither<Error, User> =>
  pipe(
    TE.tryCatch(() => fetch(`/api/users/${id}`), E.toError),
    TE.flatMap(r => r.ok ? TE.right(r) : TE.left(new Error('Not found'))),
    TE.flatMap(r => TE.tryCatch(() => r.json(), E.toError))
  )

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.
Câu hỏi thường gặp

fp-ts là gì?

Đây là thư viện cho TypeScript giúp viết mã theo các nguyên lý lập trình hàm, làm cho mã an toàn và có tổ chức hơn.

Khi nào KHÔNG nên dùng fp-ts?

Hãy tránh dùng fp-ts cho các tác vụ rất đơn giản như kiểm tra null cơ bản, vòng lặp đơn giản, mã cần hiệu năng tối đa, hoặc khi nhóm của bạn chưa quen với lập trình hàm.

Tôi có cần đọc toàn bộ tài liệu fp-ts không?

Không cần. Hướng dẫn này tập trung vào 20% quan trọng nhất (cách tiếp cận 80/20) để bạn nhanh chóng có kết quả mà không bị phức tạp về học thuật.

Chi tiết

Mô tả gốc của tác giả

A practical, jargon-free guide to fp-ts functional programming - the 80/20 approach that gets results without the academic overhead. Use when writing TypeScript with fp-ts library.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#4650
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự