Prefer flat_map over map + flatten

Convert several occurrences of `map` + `flatten` to
`flat_map` where applicable.
This commit is contained in:
Peter Leitzen 2019-07-26 10:44:13 +00:00 committed by Lin Jen-Shin
parent 0d2b5bff8c
commit 8136fac26c
9 changed files with 14 additions and 16 deletions

View file

@ -47,7 +47,7 @@ module ProtectedRef
def access_levels_for_ref(ref, action:, protected_refs: nil)
self.matching(ref, protected_refs: protected_refs)
.map(&:"#{action}_access_levels").flatten
.flat_map(&:"#{action}_access_levels")
end
# Returns all protected refs that match the given ref name.

View file

@ -388,7 +388,7 @@ class Group < Namespace
variables = Ci::GroupVariable.where(group: list_of_ids)
variables = variables.unprotected unless project.protected_for?(ref)
variables = variables.group_by(&:group_id)
list_of_ids.reverse.map { |group| variables[group.id] }.compact.flatten
list_of_ids.reverse.flat_map { |group| variables[group.id] }.compact
end
def group_member(user)

View file

@ -59,14 +59,14 @@ class StageEntity < Grape::Entity
end
def latest_statuses
HasStatus::ORDERED_STATUSES.map do |ordered_status|
HasStatus::ORDERED_STATUSES.flat_map do |ordered_status|
grouped_statuses.fetch(ordered_status, [])
end.flatten
end
end
def retried_statuses
HasStatus::ORDERED_STATUSES.map do |ordered_status|
HasStatus::ORDERED_STATUSES.flat_map do |ordered_status|
grouped_retried_statuses.fetch(ordered_status, [])
end.flatten
end
end
end

View file

@ -10,13 +10,13 @@ module Ci
update_retried
new_builds =
stage_indexes_of_created_processables.map do |index|
stage_indexes_of_created_processables.flat_map do |index|
process_stage(index)
end
@pipeline.update_status
new_builds.flatten.any?
new_builds.any?
end
private

View file

@ -10,7 +10,7 @@ module API
when String
value.split(',').map(&:strip)
when Array
value.map { |v| v.to_s.split(',').map(&:strip) }.flatten
value.flat_map { |v| v.to_s.split(',').map(&:strip) }
when LabelsList
value
else

View file

@ -33,7 +33,7 @@ module Banzai
#
# data - An Array of a Hashes mapping an HTML document to nodes to redact.
def redact_document_nodes(all_document_nodes)
all_nodes = all_document_nodes.map { |x| x[:nodes] }.flatten
all_nodes = all_document_nodes.flat_map { |x| x[:nodes] }
visible = nodes_visible_to_user(all_nodes)
metadata = []

View file

@ -40,7 +40,7 @@ module Gitlab
end
def reload!
@shards = Hash[*Shard.all.map { |shard| [shard.name, shard.id] }.flatten]
@shards = Hash[*Shard.all.flat_map { |shard| [shard.name, shard.id] }]
end
end

View file

@ -46,7 +46,7 @@ module Gitlab
parallelized_job_names = @parallelized_jobs.keys.map(&:to_s)
parallelized_config.each_with_object({}) do |(job_name, config), hash|
if config[:dependencies] && (intersection = config[:dependencies] & parallelized_job_names).any?
parallelized_deps = intersection.map { |dep| @parallelized_jobs[dep.to_sym].map(&:first) }.flatten
parallelized_deps = intersection.flat_map { |dep| @parallelized_jobs[dep.to_sym].map(&:first) }
deps = config[:dependencies] - intersection + parallelized_deps
hash[job_name] = config.merge(dependencies: deps)
else

View file

@ -34,11 +34,9 @@ module Gitlab
def extended_statuses
return @extended_statuses if defined?(@extended_statuses)
groups = self.class.extended_statuses.map do |group|
@extended_statuses = self.class.extended_statuses.flat_map do |group|
Array(group).find { |status| status.matches?(@subject, @user) }
end
@extended_statuses = groups.flatten.compact
end.compact
end
def self.extended_statuses