on master help sign in
Introduce OneOrMore.getFirstTwo for one specific scenario

There is one line of code in Canonicalize.Environment.Dups where you want to see only one version, and otherwise, you have a guarantee that you have two or more values. This function is only used in the case of an error, thereby avoiding using OneOrMore.destruct which would force some allocations in the normal case. And it is one fewer "incomplete pattern" which is nice.

commit39710430950c4b68bdf530b18969ef201f40bf47 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 30 Apr 2019, 18:34 (8 years ago) parentfa9aba2
2 files changed+30−2
compiler/src/Canonicalize/Environment/Dups.hs+5−2 compiler/src/Data/OneOrMore.hs+25
compiler/src/Canonicalize/Environment/Dups.hs+5−2
@@ -57,8 +57,11 @@ detectHelp toError name values =
5757 OneOrMore.One (Info _ value) ->
5858 return value
5959
60− OneOrMore.More _ _ ->
61− let (Info r1 _ : Info r2 _ : _) = OneOrMore.toList values in
60+ OneOrMore.More left right ->
61+ let
62+ (Info r1 _, Info r2 _) =
63+ OneOrMore.getFirstTwo left right
64+ in
6265 Result.throw (toError name r1 r2)
6366
6467
compiler/src/Data/OneOrMore.hs+25
@@ -5,6 +5,7 @@ module Data.OneOrMore
55 , more
66 , map
77 , destruct
8+ , getFirstTwo
89 )
910 where
1011
@@ -72,3 +73,27 @@ destructRight oneOrMore xs =
7273
7374 More a b ->
7475 destructRight a (destructRight b xs)
76+
77+
78+
79+-- GET FIRST TWO
80+
81+
82+getFirstTwo :: OneOrMore a -> OneOrMore a -> (a,a)
83+getFirstTwo left right =
84+ case left of
85+ One x ->
86+ (x, getFirstOne right)
87+
88+ More lleft lright ->
89+ getFirstTwo lleft lright
90+
91+
92+getFirstOne :: OneOrMore a -> a
93+getFirstOne oneOrMore =
94+ case oneOrMore of
95+ One x ->
96+ x
97+
98+ More left _ ->
99+ getFirstOne left