{"id":18019282,"url":"https://github.com/andytill/gen_io","last_synced_at":"2025-04-04T16:28:22.500Z","repository":{"id":11281502,"uuid":"13690521","full_name":"andytill/gen_io","owner":"andytill","description":"gen_io will be an erlang behaviour combining gen_server with improved side effect handling.","archived":false,"fork":false,"pushed_at":"2013-10-28T20:50:35.000Z","size":108,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T02:13:15.302Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Erlang","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/andytill.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":"2013-10-18T21:41:38.000Z","updated_at":"2015-04-11T14:46:08.000Z","dependencies_parsed_at":"2022-09-19T09:00:27.689Z","dependency_job_id":null,"html_url":"https://github.com/andytill/gen_io","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andytill%2Fgen_io","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andytill%2Fgen_io/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andytill%2Fgen_io/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andytill%2Fgen_io/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andytill","download_url":"https://codeload.github.com/andytill/gen_io/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208647,"owners_count":20901601,"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":[],"created_at":"2024-10-30T05:09:41.722Z","updated_at":"2025-04-04T16:28:22.467Z","avatar_url":"https://github.com/andytill.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"#gen_io\n\ngen_io is an erlang behaviour that aims to separate side effects from the code we write, so that more code can be pure functions, and so more easily testable.  gen_io modules must implement all functions required by gen_server, plus optionally implement the process_io/2 function.\n\nIn erlang, side effects **can** be handled in the following manner:\n\n```erlang\nhandle_cast({send, Data}, State) -\u003e\n    Result = handle_send(Data),\n    process_io(Result),\n    {noreply, State}.\n\nhandle_send(Data0) -\u003e\n    Data1 = ...                     % do some work on the data\n    {forward, Data1}.               % return data that can be interpreted as a command\n\nprocess_io({forward, Data}) -\u003e\n    other_process:forward(Data).\n```\n\nEven when code is written like this, only two of the six lines of code (not including function headers) is pure and can be easily tested using eunit.  gen_io allows a new return type `{ok, {io, [SideEffect :: tuple()]}, State}`, the gen_io behaviour will handle the list of side effects by calling `process_io` with the side effect tuple as an argument.  What's more, gen_io provides many common implementations for side effects, so you might not need to code it at all!\n\nSo lets see what this example might look like using gen_io.\n\n```erlang\nhandle_cast({send, Data}, State) -\u003e\n    Result = handle_send(Data),\n    {noreply, {io, [Result]} State}.\n\nhandle_send(Data0) -\u003e\n    Data1 = ...                                 % do some work on the data\n    {func, other_process, forward, [Data1]}.    % return data that can be interpreted as a command\n```\n\nThere is much less boiler plate here and both functions are now purely functions, so its very easy to test.  gen_io will execute the tuple tagged as `func` by executing it as a function.\n\n## Current State\n\ngen_io is an a very experimental state although the prototype is fully working.\n\n## About\n\nThis project was inspired by the [Purely Functional I/O](http://www.infoq.com/presentations/io-functional-side-effects?utm_source=infoq\u0026utm_medium=videos_homepage\u0026utm_campaign=videos_row3) presentation by [Rúnar Óli](https://twitter.com/runarorama).  It is well worth watching to get a quick start on IO in functional programming.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandytill%2Fgen_io","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandytill%2Fgen_io","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandytill%2Fgen_io/lists"}