Skip to content

Commit bba4985

Browse files
makenowjustRX14
authored andcommittedJan 17, 2018
Use join instead of each_with_index and if i > 0 (#5599)
Just a refactoring.
1 parent 8eb8554 commit bba4985

File tree

3 files changed

+32
-104
lines changed

3 files changed

+32
-104
lines changed
 

Diff for: ‎src/compiler/crystal/syntax/to_s.cr

+23-80
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ module Crystal
128128
@str << "["
129129
end
130130

131-
node.elements.each_with_index do |exp, i|
132-
@str << ", " if i > 0
133-
exp.accept self
134-
end
131+
node.elements.join(", ", @str, &.accept self)
135132

136133
if name
137134
@str << "}"
@@ -183,8 +180,7 @@ module Crystal
183180

184181
def visit(node : NamedTupleLiteral)
185182
@str << "{"
186-
node.entries.each_with_index do |entry, i|
187-
@str << ", " if i > 0
183+
node.entries.join(", ", @str) do |entry|
188184
visit_named_arg_name(entry.key)
189185
@str << ": "
190186
entry.value.accept self
@@ -375,10 +371,7 @@ module Crystal
375371
if node.name.ends_with?('=') && node.name[0].ascii_letter?
376372
@str << decorate_call(node, node.name.rchop)
377373
@str << " = "
378-
node.args.each_with_index do |arg, i|
379-
@str << ", " if i > 0
380-
arg.accept self
381-
end
374+
node.args.join(", ", @str, &.accept self)
382375
else
383376
@str << decorate_call(node, node.name)
384377

@@ -569,15 +562,9 @@ module Crystal
569562
end
570563

571564
def visit(node : MultiAssign)
572-
node.targets.each_with_index do |target, i|
573-
@str << ", " if i > 0
574-
target.accept self
575-
end
565+
node.targets.join(", ", @str, &.accept self)
576566
@str << " = "
577-
node.values.each_with_index do |value, i|
578-
@str << ", " if i > 0
579-
value.accept self
580-
end
567+
node.values.join(", ", @str, &.accept self)
581568
false
582569
end
583570

@@ -614,10 +601,7 @@ module Crystal
614601
@str << "->"
615602
if node.def.args.size > 0
616603
@str << "("
617-
node.def.args.each_with_index do |arg, i|
618-
@str << ", " if i > 0
619-
arg.accept self
620-
end
604+
node.def.args.join(", ", @str, &.accept self)
621605
@str << ")"
622606
end
623607
@str << " "
@@ -639,10 +623,7 @@ module Crystal
639623

640624
if node.args.size > 0
641625
@str << "("
642-
node.args.each_with_index do |arg, i|
643-
@str << ", " if i > 0
644-
arg.accept self
645-
end
626+
node.args.join(", ", @str, &.accept self)
646627
@str << ")"
647628
end
648629
false
@@ -765,10 +746,7 @@ module Crystal
765746

766747
def visit(node : MacroFor)
767748
@str << "{% for "
768-
node.vars.each_with_index do |var, i|
769-
@str << ", " if i > 0
770-
var.accept self
771-
end
749+
node.vars.join(", ", @str, &.accept self)
772750
@str << " in "
773751
node.exp.accept self
774752
@str << " %}"
@@ -784,10 +762,7 @@ module Crystal
784762
@str << node.name
785763
if exps = node.exps
786764
@str << '{'
787-
exps.each_with_index do |exp, i|
788-
@str << ", " if i > 0
789-
exp.accept self
790-
end
765+
exps.join(", ", @str, &.accept self)
791766
@str << '}'
792767
end
793768
false
@@ -838,10 +813,7 @@ module Crystal
838813
def visit(node : ProcNotation)
839814
@str << "("
840815
if inputs = node.inputs
841-
inputs.each_with_index do |input, i|
842-
@str << ", " if i > 0
843-
input.accept self
844-
end
816+
inputs.join(", ", @str, &.accept self)
845817
@str << " "
846818
end
847819
@str << "-> "
@@ -857,10 +829,8 @@ module Crystal
857829
end
858830

859831
def visit(node : Path)
860-
node.names.each_with_index do |name, i|
861-
@str << "::" if i > 0 || node.global?
862-
@str << name
863-
end
832+
@str << "::" if node.global?
833+
node.names.join("::", @str)
864834
end
865835

866836
def visit(node : Generic)
@@ -886,8 +856,7 @@ module Crystal
886856
printed_arg = false
887857

888858
@str << "("
889-
node.type_vars.each_with_index do |var, i|
890-
@str << ", " if i > 0
859+
node.type_vars.join(", ", @str) do |var|
891860
var.accept self
892861
printed_arg = true
893862
end
@@ -932,10 +901,7 @@ module Crystal
932901
end
933902

934903
def visit(node : Union)
935-
node.types.each_with_index do |ident, i|
936-
@str << " | " if i > 0
937-
ident.accept self
938-
end
904+
node.types.join(" | ", @str, &.accept self)
939905
false
940906
end
941907

@@ -970,10 +936,7 @@ module Crystal
970936
@str << keyword("yield")
971937
if node.exps.size > 0
972938
@str << " "
973-
node.exps.each_with_index do |exp, i|
974-
@str << ", " if i > 0
975-
exp.accept self
976-
end
939+
node.exps.join(", ", @str, &.accept self)
977940
end
978941
false
979942
end
@@ -1020,10 +983,7 @@ module Crystal
1020983
first = node.elements.first?
1021984
space = first.is_a?(TupleLiteral) || first.is_a?(NamedTupleLiteral) || first.is_a?(HashLiteral)
1022985
@str << " " if space
1023-
node.elements.each_with_index do |exp, i|
1024-
@str << ", " if i > 0
1025-
exp.accept self
1026-
end
986+
node.elements.join(", ", @str, &.accept self)
1027987
@str << " " if space
1028988
@str << "}"
1029989
false
@@ -1147,8 +1107,7 @@ module Crystal
11471107
end
11481108
if node.args.size > 0
11491109
@str << "("
1150-
node.args.each_with_index do |arg, i|
1151-
@str << ", " if i > 0
1110+
node.args.join(", ", @str) do |arg|
11521111
if arg_name = arg.name
11531112
@str << arg_name << " : "
11541113
end
@@ -1328,10 +1287,7 @@ module Crystal
13281287
append_indent
13291288
@str << keyword("when")
13301289
@str << " "
1331-
node.conds.each_with_index do |cond, i|
1332-
@str << ", " if i > 0
1333-
cond.accept self
1334-
end
1290+
node.conds.join(", ", @str, &.accept self)
13351291
newline
13361292
accept_with_indent node.body
13371293
false
@@ -1401,10 +1357,7 @@ module Crystal
14011357
@str << " :"
14021358
end
14031359
@str << " "
1404-
types.each_with_index do |type, i|
1405-
@str << " | " if i > 0
1406-
type.accept self
1407-
end
1360+
types.join(" | ", @str, &.accept self)
14081361
end
14091362
newline
14101363
accept_with_indent node.body
@@ -1423,10 +1376,7 @@ module Crystal
14231376
def visit(node : TypeOf)
14241377
@str << keyword("typeof")
14251378
@str << "("
1426-
node.expressions.each_with_index do |exp, i|
1427-
@str << ", " if i > 0
1428-
exp.accept self
1429-
end
1379+
node.expressions.join(", ", @str, &.accept self)
14301380
@str << ")"
14311381
false
14321382
end
@@ -1437,8 +1387,7 @@ module Crystal
14371387
if !node.args.empty? || node.named_args
14381388
@str << "("
14391389
printed_arg = false
1440-
node.args.each_with_index do |arg, i|
1441-
@str << ", " if i > 0
1390+
node.args.join(", ", @str) do |arg|
14421391
arg.accept self
14431392
printed_arg = true
14441393
end
@@ -1472,17 +1421,11 @@ module Crystal
14721421
@str << ":"
14731422
if inputs = node.inputs
14741423
@str << " "
1475-
inputs.each_with_index do |input, i|
1476-
@str << ", " if i > 0
1477-
input.accept self
1478-
end
1424+
inputs.join(", ", @str, &.accept self)
14791425
end
14801426
if clobbers = node.clobbers
14811427
@str << " : "
1482-
clobbers.each_with_index do |clobber, i|
1483-
@str << ", " if i > 0
1484-
clobber.inspect(@str)
1485-
end
1428+
clobbers.join(", ", @str, &.inspect @str)
14861429
end
14871430
if node.volatile? || node.alignstack? || node.intel?
14881431
@str << " : "

Diff for: ‎src/compiler/crystal/types.cr

+8-20
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,7 @@ module Crystal
15111511
super
15121512
if generic_args
15131513
io << "("
1514-
type_vars.each_with_index do |type_var, i|
1515-
io << ", " if i > 0
1516-
type_var.to_s(io)
1517-
end
1514+
type_vars.join(", ", io, &.to_s(io))
15181515
io << ")"
15191516
end
15201517
end
@@ -1574,10 +1571,7 @@ module Crystal
15741571
super
15751572
if generic_args
15761573
io << "("
1577-
type_vars.each_with_index do |type_var, i|
1578-
io << ", " if i > 0
1579-
type_var.to_s(io)
1580-
end
1574+
type_vars.join(", ", io, &.to_s(io))
15811575
io << ")"
15821576
end
15831577
end
@@ -1703,14 +1697,12 @@ module Crystal
17031697
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen = false)
17041698
generic_type.append_full_name(io)
17051699
io << "("
1706-
i = 0
1707-
type_vars.each_value do |type_var|
1700+
type_vars.each_value.with_index do |type_var, i|
17081701
io << ", " if i > 0
17091702
if type_var.is_a?(Var)
17101703
if i == splat_index
17111704
tuple = type_var.type.as(TupleInstanceType)
1712-
tuple.tuple_types.each_with_index do |tuple_type, j|
1713-
io << ", " if j > 0
1705+
tuple.tuple_types.join(", ", io) do |tuple_type|
17141706
tuple_type = tuple_type.devirtualize unless codegen
17151707
tuple_type.to_s_with_options(io, codegen: codegen)
17161708
end
@@ -1722,7 +1714,6 @@ module Crystal
17221714
else
17231715
type_var.to_s(io)
17241716
end
1725-
i += 1
17261717
end
17271718
io << ")"
17281719
end
@@ -2007,7 +1998,7 @@ module Crystal
20071998

20081999
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen = false)
20092000
io << "Proc("
2010-
arg_types.each_with_index do |type, i|
2001+
arg_types.each do |type|
20112002
type = type.devirtualize unless codegen
20122003
type.to_s_with_options(io, codegen: codegen)
20132004
io << ", "
@@ -2120,8 +2111,7 @@ module Crystal
21202111

21212112
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen = false)
21222113
io << "Tuple("
2123-
@tuple_types.each_with_index do |tuple_type, i|
2124-
io << ", " if i > 0
2114+
@tuple_types.join(", ", io) do |tuple_type|
21252115
tuple_type = tuple_type.devirtualize unless codegen
21262116
tuple_type.to_s_with_options(io, skip_union_parens: true, codegen: codegen)
21272117
end
@@ -2234,8 +2224,7 @@ module Crystal
22342224

22352225
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen = false)
22362226
io << "NamedTuple("
2237-
@entries.each_with_index do |entry, i|
2238-
io << ", " if i > 0
2227+
@entries.join(", ", io) do |entry|
22392228
if Symbol.needs_quotes?(entry.name)
22402229
entry.name.inspect(io)
22412230
else
@@ -2749,8 +2738,7 @@ module Crystal
27492738
union_types = @union_types.dup
27502739
union_types << union_types.delete_at(nil_type_index)
27512740
end
2752-
union_types.each_with_index do |type, i|
2753-
io << " | " if i > 0
2741+
union_types.join(" | ", io) do |type|
27542742
type = type.devirtualize unless codegen
27552743
type.to_s_with_options(io, codegen: codegen)
27562744
end

Diff for: ‎src/semantic_version.cr

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class SemanticVersion
8080
end
8181

8282
def to_s(io : IO)
83-
identifiers.each_with_index do |s, i|
84-
io << "." if i > 0
85-
io << s
86-
end
83+
identifiers.join(".", io)
8784
end
8885

8986
def <=>(other : self) : Int32

0 commit comments

Comments
 (0)
Please sign in to comment.