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

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-either-ref/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“كيف أتحقق من صحة بريد إلكتروني، وإن لم يكن صحيحًا، أعرض رسالة خطأ واضحة؟”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

تعمل المهارة بتحديد مسارين: مسار للنجاح (مع النتيجة المتوقعة) ومسار للفشل (مع وصف المشكلة). ويمكنك بعد ذلك اختيار المسار الذي تتبعه حسب النتيجة.

المحتوى التقني (ملف 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
آخر تحديث
15‏/10‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة