Fsharp

From G1Joshi/Agent-Skills · View interactive version

Fsharp is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert F# functional programming assistance covering pattern matching, immutability, async workflows, and .NET interop. Use when developing type-safe domain models, data processing pipelines, or cloud services.

How to use this skill

Skill file

F#

F# is a functional-first, cross-platform programming language on .NET, combining strong static typing, pattern matching, lightweight syntax, and seamless C# interoperability.

When to Use

Quick Start

type Person = { Name: string; Age: int }

let describePerson person =
    match person.Age with
    | age when age < 18 -> sprintf "%s is a minor" person.Name
    | _ -> sprintf "%s is an adult" person.Name

let alice = { Name = "Alice"; Age = 25 }
printfn "%s" (describePerson alice)

Core Concepts

Discriminated Unions & Pattern Matching

Makes illegal business states unrepresentable through closed algebraic data types:

type PaymentMethod =
    | CreditCard of cardNumber: string * cvv: string
    | BankTransfer of iban: string
    | Crypto of walletAddress: string

let processPayment method amount =
    match method with
    | CreditCard(num, _) -> printfn "Charging card %s: $%.2f" num amount
    | BankTransfer iban  -> printfn "Initiating SEPA transfer to %s" iban
    | Crypto wallet      -> printfn "Broadcasting crypto payment to %s" wallet

Type Providers (Compile-Time Schema Generation)

Generates strongly-typed types directly from external JSON, CSV, or SQL sources at compile-time:

open FSharp.Data

type UserApi = JsonProvider<"https://api.example.com/user.json">

let parseUser jsonString =
    let user = UserApi.Parse(jsonString)
    printfn "User Name: %s, Email: %s" user.Name user.Email

Functional Pipelines & Partial Application

Composes functions using forward pipe (|>) and composition (>>) operators:

let addTax amount = amount * 1.10
let applyDiscount amount = amount - 5.0

let calculateFinalPrice = applyDiscount >> addTax

let finalTotal = 100.0 |> calculateFinalPrice

Common Patterns

Discriminated Unions for Exhaustive Domain Modeling

Problem: Representing complex state transitions with boolean flags leads to invalid object states.

Solution:

Use Discriminated Unions with compiler-verified pattern matching:

type PaymentMethod =
    | CreditCard of cardNumber: string * cvv: string
    | PayPal of email: string
    | Crypto of walletAddress: string

let processPayment method amount =
    match method with
    | CreditCard(num, _) -> sprintf "Charging $%.2f to Card ending in %s" amount (num.Substring(num.Length - 4))
    | PayPal email -> sprintf "Charging $%.2f via PayPal (%s)" amount email
    | Crypto wallet -> sprintf "Requesting crypto transfer to %s" wallet

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :----------------------------------------------------------------- | :------------------------------------------------------------------- | :-------------------------------------------------------------------------- |

| Incomplete pattern matches on this expression | Pattern match missing one or more cases of a Discriminated Union. | Add missing cases to make pattern match exhaustive. |

| FS0001: Type mismatch | Expected one type but received another; F# does not implicitly cast. | Cast explicitly using functions like float, int, or conversion methods. |

| FS0041: A unique overload for method ... could not be determined | .NET method overload resolution ambiguous. | Provide explicit type annotations on method parameters. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.