find_and_map 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.markdown +60 -0
- data/Rakefile +1 -0
- data/find_and_map.gemspec +19 -0
- data/lib/find_and_map.rb +10 -0
- data/lib/find_and_map/version.rb +3 -0
- data/spec/find_and_map_spec.rb +17 -0
- metadata +58 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format doc
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Anthony Cook
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Find and Map
|
2
|
+
============
|
3
|
+
|
4
|
+
Anthony M. Cook (2012)
|
5
|
+
|
6
|
+
I was looking for a way to simply find the first matching element in an enumerable and return a mapped result.
|
7
|
+
|
8
|
+
For example:
|
9
|
+
|
10
|
+
````ruby
|
11
|
+
{a: 1, b: 2}.find_and_map do |key, value|
|
12
|
+
value if key == :a
|
13
|
+
end #=> 1
|
14
|
+
````
|
15
|
+
|
16
|
+
Using the same block above `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.
|
17
|
+
|
18
|
+
This method finds the first match (like `Enumerable#find`) and then return the result of the block rather than the matching element (like `Enumerable#map`).
|
19
|
+
|
20
|
+
I 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.
|
21
|
+
|
22
|
+
Here's some alternative ways of getting the desired result that violate the above constraints...
|
23
|
+
|
24
|
+
Using `find`:
|
25
|
+
|
26
|
+
````ruby
|
27
|
+
{a: 1, b: 2}.find do |key, value|
|
28
|
+
break value if key == :a
|
29
|
+
end
|
30
|
+
````
|
31
|
+
|
32
|
+
Using `map`:
|
33
|
+
|
34
|
+
````ruby
|
35
|
+
{a: 1, b: 2}.map do |key, value|
|
36
|
+
value if key == :a
|
37
|
+
end.compact
|
38
|
+
````
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
gem 'find_and_map'
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install find_and_map
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'find_and_map/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "find_and_map"
|
8
|
+
gem.version = FindAndMap::VERSION
|
9
|
+
gem.authors = ["Anthony Cook"]
|
10
|
+
gem.email = ["anthonymichaelcook@gmail.com"]
|
11
|
+
gem.description = %q{Finds the first element for which the block returns non-falsy and returns the value.}
|
12
|
+
gem.summary = %q{Hybrid of find and map!}
|
13
|
+
gem.homepage = 'http://github.com/acook/find_and_map'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/find_and_map.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'pry'
|
3
|
+
require_relative '../lib/find_and_map'
|
4
|
+
|
5
|
+
describe FindAndMap do
|
6
|
+
specify { subject.should be_a(Module)}
|
7
|
+
|
8
|
+
it 'finds the first item that returns non-nil and returns the result' do
|
9
|
+
a = [1,2,3]
|
10
|
+
a.extend FindAndMap
|
11
|
+
a.find_and_map do |element|
|
12
|
+
if element == 2 then
|
13
|
+
'hi!'
|
14
|
+
end
|
15
|
+
end.should == 'hi!'
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: find_and_map
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anthony Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Finds the first element for which the block returns non-falsy and returns
|
15
|
+
the value.
|
16
|
+
email:
|
17
|
+
- anthonymichaelcook@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- .rspec
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.markdown
|
28
|
+
- Rakefile
|
29
|
+
- find_and_map.gemspec
|
30
|
+
- lib/find_and_map.rb
|
31
|
+
- lib/find_and_map/version.rb
|
32
|
+
- spec/find_and_map_spec.rb
|
33
|
+
homepage: http://github.com/acook/find_and_map
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Hybrid of find and map!
|
57
|
+
test_files:
|
58
|
+
- spec/find_and_map_spec.rb
|