Skip to content

Commit 817fe31

Browse files
committed
Replace map.flatten with flat_map in actionpack
1 parent ffcc617 commit 817fe31

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

actionpack/lib/action_dispatch/journey/gtg/builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def transition_table
2727
marked[s] = true # mark s
2828

2929
s.group_by { |state| symbol(state) }.each do |sym, ps|
30-
u = ps.map { |l| followpos(l) }.flatten
30+
u = ps.flat_map { |l| followpos(l) }
3131
next if u.empty?
3232

3333
if u.uniq == [DUMMY]
@@ -90,7 +90,7 @@ def firstpos(node)
9090
firstpos(node.left)
9191
end
9292
when Nodes::Or
93-
node.children.map { |c| firstpos(c) }.flatten.uniq
93+
node.children.flat_map { |c| firstpos(c) }.uniq
9494
when Nodes::Unary
9595
firstpos(node.left)
9696
when Nodes::Terminal
@@ -105,7 +105,7 @@ def lastpos(node)
105105
when Nodes::Star
106106
firstpos(node.left)
107107
when Nodes::Or
108-
node.children.map { |c| lastpos(c) }.flatten.uniq
108+
node.children.flat_map { |c| lastpos(c) }.uniq
109109
when Nodes::Cat
110110
if nullable?(node.right)
111111
lastpos(node.left) | lastpos(node.right)

actionpack/lib/action_dispatch/journey/gtg/simulator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def simulate(string)
3131

3232
return if acceptance_states.empty?
3333

34-
memos = acceptance_states.map { |x| tt.memo(x) }.flatten.compact
34+
memos = acceptance_states.flat_map { |x| tt.memo(x) }.compact
3535

3636
MatchData.new(memos)
3737
end

actionpack/lib/action_dispatch/journey/gtg/transition_table.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def []=(from, to, sym)
114114
end
115115

116116
def states
117-
ss = @string_states.keys + @string_states.values.map(&:values).flatten
118-
rs = @regexp_states.keys + @regexp_states.values.map(&:values).flatten
117+
ss = @string_states.keys + @string_states.values.flat_map(&:values)
118+
rs = @regexp_states.keys + @regexp_states.values.flat_map(&:values)
119119
(ss + rs).uniq
120120
end
121121

@@ -143,11 +143,11 @@ def states_hash_for(sym)
143143
def move_regexp(t, a)
144144
return [] if t.empty?
145145

146-
t.map { |s|
146+
t.flat_map { |s|
147147
if states = @regexp_states[s]
148148
states.map { |re, v| re === a ? v : nil }
149149
end
150-
}.flatten.compact.uniq
150+
}.compact.uniq
151151
end
152152

153153
def move_string(t, a)

actionpack/lib/action_dispatch/journey/nfa/dot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def to_dot
1616
# end
1717
# " #{n.object_id} [label=\"#{label}\", shape=box];"
1818
#}
19-
#memo_edges = memos.map { |k, memos|
19+
#memo_edges = memos.flat_map { |k, memos|
2020
# (memos || []).map { |v| " #{k} -> #{v.object_id};" }
21-
#}.flatten.uniq
21+
#}.uniq
2222

2323
<<-eodot
2424
digraph nfa {

actionpack/lib/action_dispatch/journey/nfa/simulator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def simulate(string)
3434

3535
return if acceptance_states.empty?
3636

37-
memos = acceptance_states.map { |x| tt.memo(x) }.flatten.compact
37+
memos = acceptance_states.flat_map { |x| tt.memo(x) }.compact
3838

3939
MatchData.new(memos)
4040
end

actionpack/lib/action_dispatch/journey/nfa/transition_table.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def merge(left, right)
4242
end
4343

4444
def states
45-
(@table.keys + @table.values.map(&:keys).flatten).uniq
45+
(@table.keys + @table.values.flat_map(&:keys)).uniq
4646
end
4747

4848
# Returns a generalized transition graph with reduced states. The states
@@ -93,7 +93,7 @@ def generalized_table
9393
# Returns set of NFA states to which there is a transition on ast symbol
9494
# +a+ from some state +s+ in +t+.
9595
def following_states(t, a)
96-
Array(t).map { |s| inverted[s][a] }.flatten.uniq
96+
Array(t).flat_map { |s| inverted[s][a] }.uniq
9797
end
9898

9999
# Returns set of NFA states to which there is a transition on ast symbol
@@ -107,7 +107,7 @@ def move(t, a)
107107
end
108108

109109
def alphabet
110-
inverted.values.map(&:keys).flatten.compact.uniq.sort_by { |x| x.to_s }
110+
inverted.values.flat_map(&:keys).compact.uniq.sort_by { |x| x.to_s }
111111
end
112112

113113
# Returns a set of NFA states reachable from some NFA state +s+ in set
@@ -131,9 +131,9 @@ def eclosure(t)
131131
end
132132

133133
def transitions
134-
@table.map { |to, hash|
134+
@table.flat_map { |to, hash|
135135
hash.map { |from, sym| [from, sym, to] }
136-
}.flatten(1)
136+
}
137137
end
138138

139139
private

actionpack/lib/action_dispatch/journey/path/pattern.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def required_names
5353
end
5454

5555
def optional_names
56-
@optional_names ||= spec.grep(Nodes::Group).map { |group|
56+
@optional_names ||= spec.grep(Nodes::Group).flat_map { |group|
5757
group.grep(Nodes::Symbol)
58-
}.flatten.map { |n| n.name }.uniq
58+
}.map { |n| n.name }.uniq
5959
end
6060

6161
class RegexpOffsets < Journey::Visitors::Visitor # :nodoc:

0 commit comments

Comments
 (0)