Skip to content

Commit

Permalink
0.1.6 - added match_fn to be easier to pass to functions like map
Browse files Browse the repository at this point in the history
  • Loading branch information
baweaver committed Apr 12, 2018
1 parent 814470d commit ab3bb31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pryrc
@@ -1,5 +1,5 @@
class Person
attr_reader :name, :age
attr_accessor :name, :age

def initialize(name, age)
@name = name
Expand Down
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -384,6 +384,24 @@ In this case it's trying to do a few things:

If no block function is provided, it assumes an identity function (`-> v { v }`) instead. If no match is found, `nil` will be returned.

Now you _can_ also use a reversed version, `match_fn` (name pending better ideas), to run with map:

```ruby
name_longer_than_three = -> person { person.name.size > 3 }

people_objects.map(&Qo.match_fn(
Qo.m(name_longer_than_three) { |person|
person.name = person.name[0..2]
person
},
Qo.m(:*)
))

# => [Person(age: 22, name: "Rob"), Person(age: 22, name: "Rob"), Person(age: 42, name: "Foo"), Person(age: 17, name: "Bar")]
```

So we just truncated everyone's name that was longer than three characters.

### 5 - Hacky Fun Time

These examples will grow over the next few weeks as I think of more fun things to do with Qo. PRs welcome if you find fun uses!
Expand Down
4 changes: 4 additions & 0 deletions lib/qo.rb
Expand Up @@ -20,6 +20,10 @@ def match(data, *qo_matchers)
}
end

def match_fn(*qo_matchers)
-> data { match(data, *qo_matchers) }
end

def and(*array_matchers, **keyword_matchers)
Qo::Matcher.new('and', *array_matchers, **keyword_matchers)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/qo/version.rb
@@ -1,3 +1,3 @@
module Qo
VERSION = '0.1.5'
VERSION = '0.1.6'
end

0 comments on commit ab3bb31

Please sign in to comment.