{"id":29272074,"url":"https://github.com/acook/find_and_map","last_synced_at":"2025-07-05T00:10:29.953Z","repository":{"id":5812492,"uuid":"7027472","full_name":"acook/find_and_map","owner":"acook","description":"Hybrid of find and map!","archived":false,"fork":false,"pushed_at":"2013-08-21T23:08:06.000Z","size":148,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-02T09:58:01.635Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/acook/find_and_map#readme","language":"Ruby","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/acook.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-06T00:54:58.000Z","updated_at":"2013-10-05T13:48:55.000Z","dependencies_parsed_at":"2022-08-31T16:51:49.894Z","dependency_job_id":null,"html_url":"https://github.com/acook/find_and_map","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/acook/find_and_map","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Ffind_and_map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Ffind_and_map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Ffind_and_map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Ffind_and_map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acook","download_url":"https://codeload.github.com/acook/find_and_map/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Ffind_and_map/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263636825,"owners_count":23492312,"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":"2025-07-05T00:10:29.258Z","updated_at":"2025-07-05T00:10:29.918Z","avatar_url":"https://github.com/acook.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Find and Map\n============\n\n    Anthony M. Cook (2013)\n\nFinds and returns the value of the block.\n\n[![Gem Version](https://badge.fury.io/rb/find_and_map.png)](http://badge.fury.io/rb/find_and_map)\n[![Build Status](https://travis-ci.org/acook/find_and_map.png?branch=master)](https://travis-ci.org/acook/find_and_map)\n[![Code Climate](https://codeclimate.com/github/acook/find_and_map.png)](https://codeclimate.com/github/acook/find_and_map)\n[![Dependency Status](https://gemnasium.com/acook/find_and_map.png)](https://gemnasium.com/acook/find_and_map)\n\nUsage\n-----\n\nPut it in your Gemfile:\n\n```ruby\ngem 'find_and_map'\n```\n\nThen you can include it on your own Enumerable object:\n\n```ruby\ninclude FindAndMap\n```\n\nOr you can use the core extension directly the Gemfile:\n\n```ruby\ngem 'find_and_map', require: 'find_and_map/core_ext'\n```\n\nThen you can use it like this:\n\n```ruby\n{a: 1, b: 2}.find_and_map do |key, value|\n  value if key == :a\nend #=\u003e 1\n```\n\nBackground\n----------\n\nI was looking for a way to simply find the first matching element in an enumerable and return a mapped result.\n\nUsing the same block above (in the Usage section) `find` returns `[:a, 1]` and `map` returns `[1, nil]`. But I want just the value, or even some other object based on it, but only that and nothing else.\n\nThis method finds the first match (like `Enumerable#find`) and then return the result of the block rather than the matching element (like `Enumerable#map`).\n\nI don't like doing explict returns in the middle of loops, or nil checks if find fails, explicit breaks that alter the functionality of a standard iterator, and using compact after a map just seems excessive.\n\nHere's some alternative ways of getting the desired result that violate the above constraints...\n\nUsing `find`:\n\n````ruby\n{a: 1, b: 2}.find do |key, value|\n  break value if key == :a\nend\n````\n\nUsing `map`:\n\n````ruby\n{a: 1, b: 2}.map do |key, value|\n  value if key == :a\nend.compact\n````\n\nContributing\n------------\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facook%2Ffind_and_map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facook%2Ffind_and_map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facook%2Ffind_and_map/lists"}