Skip to content

Commit

Permalink
Memoize #find_all
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Apr 1, 2017
1 parent 698eb90 commit 9e33620
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/nanoc/base/entities/identifiable_collection.rb
Expand Up @@ -39,8 +39,11 @@ def [](arg)

contract C::Any => C::IterOf[C::RespondTo[:identifier]]
def find_all(arg)
pat = Nanoc::Int::Pattern.from(arg)
select { |i| pat.match?(i.identifier) }
if frozen?
find_all_memoized(arg)
else
find_all_unmemoized(arg)
end
end

contract C::None => C::ArrayOf[C::RespondTo[:identifier]]
Expand Down Expand Up @@ -83,6 +86,18 @@ def get_memoized(arg)
end
memoize :get_memoized

contract C::Any => C::IterOf[C::RespondTo[:identifier]]
def find_all_unmemoized(arg)
pat = Nanoc::Int::Pattern.from(arg)
select { |i| pat.match?(i.identifier) }
end

contract C::Any => C::IterOf[C::RespondTo[:identifier]]
def find_all_memoized(arg)
find_all_unmemoized(arg)
end
memoize :find_all_memoized

def object_with_identifier(identifier)
if frozen?
@mapping[identifier.to_s]
Expand Down

0 comments on commit 9e33620

Please sign in to comment.