{"id":15059669,"url":"https://github.com/bigbinary/elm-reader","last_synced_at":"2025-04-10T05:41:31.497Z","repository":{"id":52693418,"uuid":"136333415","full_name":"bigbinary/elm-reader","owner":"bigbinary","description":"Reader type in Elm","archived":false,"fork":false,"pushed_at":"2021-04-20T20:43:52.000Z","size":10,"stargazers_count":7,"open_issues_count":2,"forks_count":2,"subscribers_count":37,"default_branch":"master","last_synced_at":"2025-03-24T06:54:38.454Z","etag":null,"topics":["elm"],"latest_commit_sha":null,"homepage":"http://package.elm-lang.org/packages/bigbinary/elm-reader/latest","language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bigbinary.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-06T13:34:12.000Z","updated_at":"2020-12-29T15:04:40.000Z","dependencies_parsed_at":"2022-08-22T06:40:12.478Z","dependency_job_id":null,"html_url":"https://github.com/bigbinary/elm-reader","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbinary%2Felm-reader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbinary%2Felm-reader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbinary%2Felm-reader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbinary%2Felm-reader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigbinary","download_url":"https://codeload.github.com/bigbinary/elm-reader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166242,"owners_count":21058475,"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":["elm"],"created_at":"2024-09-24T22:46:29.524Z","updated_at":"2025-04-10T05:41:31.479Z","avatar_url":"https://github.com/bigbinary.png","language":"Elm","readme":"# Reader\n\nA ```Reader``` is a handy type that is used for providing a shared \"environment\" to computations.\nIt helps to solve the problem of passing around the same values or configurations to many functions.\nIt is also often used as a way of doing dependency injections. In such cases by using a Reader the concern of passing the relevant context is mostly handled at the very peripheri of the application\n\n\nConsider the following\n\n```elm\n\ntype alias DogImages =\n    { status : String\n    , message : List String\n    }\n    \nrequestDogBreed : String -\u003e String -\u003e Json.Decode.Decoder a -\u003e Http.Request a\nrequestDogBreed apiKey apiPath decoder =\n    Http.get (\"https://dog.ceo/api/breed/\" ++ apiPath ++ \"\u0026apiKey=\" ++ apiKey) decoder\n\n\nrequestShibaImages :  String -\u003e String -\u003e Http.Request DogImages\nrequestShibaImages apiKey queryParameters =\n    requestDogBreed apiKey (\"shiba/images\" ++ queryParameters) decodeDogImages\n\n\nrequestOneShibaImage : String -\u003e Http.Request DogImages\nrequestOneShibaImage apiKey =\n    requestShibaImages apiKey \"?limit=10\"\n\n```\n\nIf you look at ```requestOneShibaImage``` you’ll see ```apiKey``` is not used, but is passed through to the other functions until it reaches ```requestDogBreed``` where it is actually used. Such kind of boilerplate can be avoided with the usage of a ```Reader``` type\n\nInstead of ```apiKey``` being passed down to each function, we can use a ```Reader``` and rewrite this in such a way that the context will get passed implicitly.\n\n\n```elm\nrequestDogBreed : String -\u003e Json.Decode.Decoder a -\u003e Reader String (Http.Request a)\nrequestDogBreed apiPath decoder =\n    Reader (\\apiKey -\u003e Http.get (\"https://dog.ceo/api/breed/\" ++ apiPath ++ \"\u0026apiKey=\" ++ apiKey) decoder )\n\n\nrequestShibaImages : String -\u003e Reader String (Http.Request DogImages)\nrequestShibaImages queryParameters =\n    requestDogBreed (\"shiba/images\" ++ queryParameters) decodeDogImages\n\n\nrequestOneShibaImage : Reader String (Http.Request DogImages)\nrequestOneShibaImage =\n    requestShibaImages \"?limit=1\"\n\n```\n\nNow the intermediate functions ```requestShibaImages``` and ```requestOneShibaImage``` no longer have to take in and pass ```apiKey``` around.\n\nWhen we finally want to send the Http Request we can use the ```run``` function to pass in the context to the Reader and retrive the value i.e the final Request\n\n```elm\ntype Msg\n    = LoadShibaImages (Result Http.Error DogImages)\n\nsend : Cmd Msg\nsend =\n    Http.send LoadShibaImages \u003c|\n        run requestOneShibaImage \"123456\"\n\n```\n\n## About BigBinary\n\n![BigBinary](https://raw.githubusercontent.com/bigbinary/bigbinary-assets/press-assets/PNG/logo-light-solid-small.png?raw=true)\n\nelm-reader is maintained by [BigBinary](https://www.BigBinary.com). BigBinary is a software consultancy company. We build web and mobile applications using Ruby on Rails, React.js, React Native and Elm.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigbinary%2Felm-reader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigbinary%2Felm-reader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigbinary%2Felm-reader/lists"}