Commit 8136fac2 authored by Peter Leitzen's avatar Peter Leitzen Committed by Lin Jen-Shin

Prefer `flat_map` over `map` + `flatten`

Convert several occurrences of `map` + `flatten` to
`flat_map` where applicable.
parent 0d2b5bff
...@@ -47,7 +47,7 @@ module ProtectedRef ...@@ -47,7 +47,7 @@ module ProtectedRef
def access_levels_for_ref(ref, action:, protected_refs: nil) def access_levels_for_ref(ref, action:, protected_refs: nil)
self.matching(ref, protected_refs: protected_refs) self.matching(ref, protected_refs: protected_refs)
.map(&:"#{action}_access_levels").flatten .flat_map(&:"#{action}_access_levels")
end end
# Returns all protected refs that match the given ref name. # Returns all protected refs that match the given ref name.
......
...@@ -388,7 +388,7 @@ class Group < Namespace ...@@ -388,7 +388,7 @@ class Group < Namespace
variables = Ci::GroupVariable.where(group: list_of_ids) variables = Ci::GroupVariable.where(group: list_of_ids)
variables = variables.unprotected unless project.protected_for?(ref) variables = variables.unprotected unless project.protected_for?(ref)
variables = variables.group_by(&:group_id) 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 end
def group_member(user) def group_member(user)
......
...@@ -59,14 +59,14 @@ class StageEntity < Grape::Entity ...@@ -59,14 +59,14 @@ class StageEntity < Grape::Entity
end end
def latest_statuses def latest_statuses
HasStatus::ORDERED_STATUSES.map do |ordered_status| HasStatus::ORDERED_STATUSES.flat_map do |ordered_status|
grouped_statuses.fetch(ordered_status, []) grouped_statuses.fetch(ordered_status, [])
end.flatten end
end end
def retried_statuses def retried_statuses
HasStatus::ORDERED_STATUSES.map do |ordered_status| HasStatus::ORDERED_STATUSES.flat_map do |ordered_status|
grouped_retried_statuses.fetch(ordered_status, []) grouped_retried_statuses.fetch(ordered_status, [])
end.flatten end
end end
end end
...@@ -10,13 +10,13 @@ module Ci ...@@ -10,13 +10,13 @@ module Ci
update_retried update_retried
new_builds = new_builds =
stage_indexes_of_created_processables.map do |index| stage_indexes_of_created_processables.flat_map do |index|
process_stage(index) process_stage(index)
end end
@pipeline.update_status @pipeline.update_status
new_builds.flatten.any? new_builds.any?
end end
private private
......
...@@ -10,7 +10,7 @@ module API ...@@ -10,7 +10,7 @@ module API
when String when String
value.split(',').map(&:strip) value.split(',').map(&:strip)
when Array when Array
value.map { |v| v.to_s.split(',').map(&:strip) }.flatten value.flat_map { |v| v.to_s.split(',').map(&:strip) }
when LabelsList when LabelsList
value value
else else
......
...@@ -33,7 +33,7 @@ module Banzai ...@@ -33,7 +33,7 @@ module Banzai
# #
# data - An Array of a Hashes mapping an HTML document to nodes to redact. # data - An Array of a Hashes mapping an HTML document to nodes to redact.
def redact_document_nodes(all_document_nodes) 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) visible = nodes_visible_to_user(all_nodes)
metadata = [] metadata = []
......
...@@ -40,7 +40,7 @@ module Gitlab ...@@ -40,7 +40,7 @@ module Gitlab
end end
def reload! 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
end end
......
...@@ -46,7 +46,7 @@ module Gitlab ...@@ -46,7 +46,7 @@ module Gitlab
parallelized_job_names = @parallelized_jobs.keys.map(&:to_s) parallelized_job_names = @parallelized_jobs.keys.map(&:to_s)
parallelized_config.each_with_object({}) do |(job_name, config), hash| parallelized_config.each_with_object({}) do |(job_name, config), hash|
if config[:dependencies] && (intersection = config[:dependencies] & parallelized_job_names).any? 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 deps = config[:dependencies] - intersection + parallelized_deps
hash[job_name] = config.merge(dependencies: deps) hash[job_name] = config.merge(dependencies: deps)
else else
......
...@@ -34,11 +34,9 @@ module Gitlab ...@@ -34,11 +34,9 @@ module Gitlab
def extended_statuses def extended_statuses
return @extended_statuses if defined?(@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) } Array(group).find { |status| status.matches?(@subject, @user) }
end end.compact
@extended_statuses = groups.flatten.compact
end end
def self.extended_statuses def self.extended_statuses
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment