{"id":16715537,"url":"https://github.com/stijnmoreels/fsharp.featuretoggle","last_synced_at":"2025-07-10T11:08:30.167Z","repository":{"id":92225589,"uuid":"111537476","full_name":"stijnmoreels/FSharp.FeatureToggle","owner":"stijnmoreels","description":"Feature Toggles in F#","archived":false,"fork":false,"pushed_at":"2022-11-26T10:48:25.000Z","size":7,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T08:48:37.664Z","etag":null,"topics":["feature-toggle","fsharp"],"latest_commit_sha":null,"homepage":null,"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/stijnmoreels.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-21T11:02:22.000Z","updated_at":"2021-12-16T01:51:04.000Z","dependencies_parsed_at":"2023-06-08T01:30:12.840Z","dependency_job_id":null,"html_url":"https://github.com/stijnmoreels/FSharp.FeatureToggle","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/stijnmoreels/FSharp.FeatureToggle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stijnmoreels%2FFSharp.FeatureToggle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stijnmoreels%2FFSharp.FeatureToggle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stijnmoreels%2FFSharp.FeatureToggle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stijnmoreels%2FFSharp.FeatureToggle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stijnmoreels","download_url":"https://codeload.github.com/stijnmoreels/FSharp.FeatureToggle/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stijnmoreels%2FFSharp.FeatureToggle/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264569439,"owners_count":23629601,"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","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":["feature-toggle","fsharp"],"created_at":"2024-10-12T21:09:40.954Z","updated_at":"2025-07-10T11:08:30.113Z","avatar_url":"https://github.com/stijnmoreels.png","language":"F#","readme":"# FSharp.FeatureToggle\n\nNo, I wasn’t very found of the idea when I first heard about the concept of _Feature Toggles_. I thought it was some kind of workaround for a problem or a possible solution that can lead to an enormous Technical Debt (which I still think can happen).\nI’m not going to talk about the pro’s or cons about the concept of a Feature Toggle or how it would increase your Test-Driven approach or decrease you Quality…\nWhat I wanted to do, was to find a way in which this concept could be expressed in my functional playground (because I couldn’t find a nice example of this). \nMy recent task was to implement some kind of Feature Toggle so I thought it was a good time to think about the concept.\n\n## Toggle Alternatives\n\nFeature Toggles can be used to toggle between different functionalities. In the previous snippet, I’ve probably seen the toggled function. This function asks for a Toggle type and returns the Enabled or Disabled value associated with it.\nSo, let’ say we have two kind of sorting algorithms: **QuickSort** and **BubbleSort**; and we want to switch between the two:\n\n```fsharp\nlet sort = Feature.toggled quicksort bubblesort\nlet example1 = sort |\u003e Feature.run Enabled\n```\n\nWe actually use a combinator for this:\n```fsharp\nlet (\u003c|\u003e) = toggled\nlet example1 = quicksort \u003c|\u003e bubblesort |\u003e Feature.run Enabled\n```\n\n## Toggle Logging\n\nFeature Toggles can also be used to toggle the logging (or other “additional” features). Let’s say we want to log the input and output of a function so we can see what’s changed. I came up with this:\n\n```fsharp\nlet tee f x = f x; x\n\nlet logT m = Feature.enabled (tee \u003c| printfn \"%s: %i\" m)\nlet logBeforeT = logT \"Before\"\nlet logAfterT = logT \"After\"\n\nlet add1 = (+) 1\nlet add1T = Feature.retn add1\n\nlet (\u003e\u003e\u003e) f g h = f \u003e\u003e g \u003e\u003e h\nlet example2 = Feature.liftA3 (\u003e\u003e\u003e) logBeforeT add1T logAfterT |\u003e Feature.run Enabled\n```\n\nWe **lift** the add1 function (which is our function we want to monitor) so we can compose it together with the before and after logging Features.\nFor non-familiar _Functional Programmers_, the `liftA3` function will in an Applicative-manner “lift” the function (`\u003e\u003e\u003e` in this example) to the “world” of Features. This way, we have a compose function in the world of Features. Here’s the implementation:\n\n```fsharp\n/// Applies a given function to a Feature.\n/// Feature\u003c('a -\u003e 'b)\u003e -\u003e Feature\u003c'a\u003e -\u003e Feature\u003c'b\u003e\nlet apply fT xT = ask \u003c| fun t -\u003e\n    let f = run t fT\n    let x = run t xT\n    f x\n\nlet (\u003c*\u003e) = apply\n\n/// Combines three Feature values using a specified function.\n/// ('a -\u003e 'b -\u003e 'c -\u003e 'd) -\u003e Feature\u003c'a\u003e -\u003e Feature\u003c'b\u003e -\u003e Feature\u003c'c\u003e -\u003e Feature\u003c'd\u003e\nlet liftA3 f x y z = retn f \u003c*\u003e x \u003c*\u003e y \u003c*\u003e z\n```\n\n## Toggle Memoize\n\nMemoizing is also a term that you frequently hear in the _Functional Programming_-community. So, I thought it would be useful to also add a example of how you would toggle the _Memoization_ of a function.\n\n```fsharp\nlet memoize f =\n    let cache = ref Map.empty\n    let add k v = cache := Map.add k v !cache\n    fun x -\u003e\n        !cache\n        |\u003e Map.tryFind x\n        |\u003e Option.defaultWith\n            (fun () -\u003e f x |\u003e tee (add x))\n\nlet dbCall () = 4815162342L\n\nlet example3 =\n    memoize\n    |\u003e Feature.enabled\n    |\u003e Feature.map (fun mem -\u003e mem dbCall)\n    |\u003e Feature.run Enabled\n```\n\nThe `dbCall` function is just a function I use to memorize. We send the `unit` value with it, so it only calls this function once.\nWe also use the `map` function on a `Feature`:\n\n```fsharp\n/// Allows to compose cross-world Feature functions.\n/// ('a -\u003e Feature\u003c'b\u003e) -\u003e Feature\u003c'a\u003e -\u003e Feature\u003c'b\u003e\nlet bind f xT = ask \u003c| fun t -\u003e\n    let x = run t xT\n    run t (f x)\n\n/// Lifts a function into a Feature function.\n/// ('a -\u003e 'b) -\u003e (Feature\u003c'a\u003e -\u003e Feature\u003c'b\u003e)\nlet map f = bind (f \u003e\u003e retn)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstijnmoreels%2Ffsharp.featuretoggle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstijnmoreels%2Ffsharp.featuretoggle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstijnmoreels%2Ffsharp.featuretoggle/lists"}