{"id":22796134,"url":"https://github.com/tatsuyafujisaki/fsharp-cheat-sheet","last_synced_at":"2025-09-14T03:32:41.235Z","repository":{"id":65626012,"uuid":"61043851","full_name":"tatsuyafujisaki/fsharp-cheat-sheet","owner":"tatsuyafujisaki","description":"F# cheat sheet","archived":false,"fork":false,"pushed_at":"2021-01-21T15:38:09.000Z","size":87,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-11T06:26:20.989Z","etag":null,"topics":["cheat-sheet","fsharp"],"latest_commit_sha":null,"homepage":"","language":"F#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tatsuyafujisaki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-13T14:40:09.000Z","updated_at":"2024-12-25T23:39:29.000Z","dependencies_parsed_at":"2023-02-01T11:35:11.329Z","dependency_job_id":null,"html_url":"https://github.com/tatsuyafujisaki/fsharp-cheat-sheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tatsuyafujisaki/fsharp-cheat-sheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatsuyafujisaki%2Ffsharp-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatsuyafujisaki%2Ffsharp-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatsuyafujisaki%2Ffsharp-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatsuyafujisaki%2Ffsharp-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tatsuyafujisaki","download_url":"https://codeload.github.com/tatsuyafujisaki/fsharp-cheat-sheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tatsuyafujisaki%2Ffsharp-cheat-sheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275058185,"owners_count":25398168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-14T02:00:10.474Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheat-sheet","fsharp"],"created_at":"2024-12-12T05:10:45.753Z","updated_at":"2025-09-14T03:32:41.188Z","avatar_url":"https://github.com/tatsuyafujisaki.png","language":"F#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Best practices in settings\n* `F# Power Tools` \u003e `Configuration` \u003e `Project Cache Size` \u003e `0`\n* Project \u003e `Properties` \u003e `Build`\n  * `Other flags` \u003e `--warnon:1182`\n  * `Treat warnings as errors` \u003e `All`\n\n# Best practices in coding\n* A rather than B.\n  * Use `int[]` rather than `int array`.\n  * Use `A.toB` rather than `B.ofA` as follows\n  * Use `List.collect f xs` rather than `List.concat (List.map f xs)`\n  * Use `List.map (g \u003e\u003e f) xs` rather than `List.map f (List.map g xs)`\n  * Use %A rather than %s to double-quote a string if the string can be empty.\n\nRecommended|Not recommended\n---|---\nArray.toSeq|Seq.ofArray\nArray.toList|List.ofArray\nSeq.toArray|Array.ofSeq\nSeq.toList|List.ofSeq\nList.toArray|Array.ofList\nList.toSeq|Seq.ofList\nSet.ofArray|(none)\nSet.ofSeq|(none)\nSet.ofList|(none)\nSet.toArray|(none)\nSet.toSeq|(none)\nSet.toList|(none)\n\n# Pokemon exception handling\n```fsharp\ntry\n    // Do something\nwith\n| _ -\u003e // Do something\n```\n\n# Array manipulation\n```fsharp\nlet xs = [| 1 .. 5 |]\n\n// Use properties rather than functions\nprintfn \"%A\" (xs.Length) // Simpler than (Array.length xs)\nprintfn \"%A\" (xs.[2]) // Simpler than (Array.get xs 2)\n```\n\n# List manipulation\n```fsharp\nlet xs = [ 1 .. 5 ]\n\n// Use properties rather than functions\nprintfn \"%A\" (xs.IsEmpty) // Simpler than (List.isEmpty xs)\nprintfn \"%A\" (xs.Length) // Simpler than (List.length xs)\nprintfn \"%A\" (xs.Head) // Simpler than (List.head xs) or (xs.Item 0) or (List.item 0 xs)\nprintfn \"%A\" (xs.Tail) // Simpler than (List.tail xs)\nprintfn \"%A\" (xs.[2]) // Simpler than (xs.Item 2) or (List.item 2 xs)\n```\n\n# Set manipulation\n```fsharp\nlet xs = set [ 1 .. 5 ]\nlet ys = set [ 1 .. 5 ]\n\n// Use properties rather than functions\nprintfn \"%A\" (xs.IsEmpty) // Simpler than (Set.isEmpty xs)\nprintfn \"%A\" (xs.Count) // Simpler than (Set.count xs)\nprintfn \"%A\" (xs.MaximumElement) // Simpler than (Set.maxElement xs)\nprintfn \"%A\" (xs.MinimumElement) // Simpler than (Set.minElement xs)\nprintfn \"%A\" (xs.Contains 2) // Simpler than (Set.contains 2 xs)\nprintfn \"%A\" (xs.Add 6) // Simpler than (Set.add 6 xs)\nprintfn \"%A\" (xs.Remove 2) // Simpler than (Set.remove 2 xs)\nprintfn \"%A\" (xs.IsProperSubsetOf ys) // Simpler than (Set.isProperSubset xs ys)\nprintfn \"%A\" (xs.IsSubsetOf ys) // Simpler than (Set.isSubset xs ys)\nprintfn \"%A\" (xs.IsProperSupersetOf ys) // Simpler than (Set.isProperSuperset xs ys)\nprintfn \"%A\" (xs.IsSupersetOf ys) // Simpler than (Set.isSuperset xs ys)\n```\n\n# Map manipulation\n```fsharp\nlet m = Map.ofArray [| \"bacon\", 100; \"lettuce\", 200 |]\n\n// Use properties rather than functions\nprintfn \"%A\" (m.IsEmpty) // Simpler than (Map.isEmpty m)\nprintfn \"%A\" (m.ContainsKey \"lettuce\") // Simpler than (Map.containsKey \"lettuce\" m)\nprintfn \"%A\" (m.Count) // \"Map.count\" does not exist.\nprintfn \"%A\" m.[\"lettuce\"] // Simpler than (m.Item \"lettuce\") or (Map.find \"lettuce\" m)\nprintfn \"%A\" (m.TryFind \"bacon\") // Simpler than (Map.tryFind \"bacon\" m)\nprintfn \"%A\" (m.Add (\"tomato\", 300)) // Simpler than (Map.add \"tomato\" 300 m)\nprintfn \"%A\" (m.Remove \"lettuce\") // Simpler than (Map.remove \"lettuce\" m)\n```\n\n# How to initialize a map\n```fsharp\nlet m = Map.ofList [ 1, \"one\"; 2, \"two\" ]\n```\n\n# How to initialize a seq\n```fsharp\nlet xs = seq { 0 .. 5 }\n```\n\n# How to initialize a dictionary of .NET Framework\n```fsharp\nlet d = dict [ 1, \"one\"; 2, \"two\" ]\n```\n\n# Discriminated union\nCan have static members as follows.\n```fsharp\ntype Rank = \n    /// Represents the rank of cards 2 .. 10\n    | Value of int\n    | Ace\n    | King\n    | Queen\n    | Jack\n    static member GetAllRanks() = \n        [ yield Ace\n          for i in 2 .. 10 do yield Value i\n          yield Jack\n          yield Queen\n          yield King ]\n```\n\n# Pattern matching on records\n```fsharp\ntype Person = { First : string; Last : string }\n\nlet person = { First = \"John\"; Last = \"Doe\" }\n\nmatch person with \n| { First = \"John\" } -\u003e printfn \"Hi John !\" \n| _  -\u003e printfn \"Not John ..\"\n```\n\n# References\n## fsharp.org\n* [F# Language Specification](https://fsharp.org/specs/language-spec/)\n* [Learning F#](https://fsharp.org/learn/)\n\n## Docs\n### Guidelines\n* [F# code formatting guidelines](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting)\n* [F# coding conventions](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/conventions)\n* [F# component design guidelines](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/component-design-guidelines)\n* [F# style guide](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/)\n\n### Language references\n* [F# Language Reference](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/)\n* [Access Control](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/access-control)\n* [Casting and Conversions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/casting-and-conversions)\n* [Compiler Options](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-options)\n* [Computation Expressions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions)\n* [Constraints](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints)\n* [Keyword reference](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/keyword-reference)\n* [Statically Resolved Type Parameters](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/statically-resolved-type-parameters)\n* [Symbol and operator reference](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/)\n\n### Others\n* [Debugging F#](https://docs.microsoft.com/en-us/visualstudio/debugger/debugging-f-hash)\n* [Tail calls in F#](https://docs.microsoft.com/en-us/archive/blogs/fsharpteam/tail-calls-in-f)\n* [Tour of F#](https://docs.microsoft.com/en-us/dotnet/fsharp/tour)\n\n## Exceptions\n* [failwith](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/exception-handling/the-failwith-function)\n```fsharp\nsprintf \"Something went wrong with %A.\" var1 |\u003e failwith\n```\n* [invalidArg](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/exception-handling/the-invalidarg-function)\n```fsharp\ninvalidArg \"var1\" (sprintf \"Must be foo, but was %A.\" var1)\ninvalidArg \"var1\" (sprintf \"Must be %A, but was %A.\" (toUnionCase UnionCase1) (toUnionCase var1))\n```\n* invalidOp\n* nullArg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftatsuyafujisaki%2Ffsharp-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftatsuyafujisaki%2Ffsharp-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftatsuyafujisaki%2Ffsharp-cheat-sheet/lists"}