Skip to content

Commit 293c4a7

Browse files
committed
Faster array equality
1 parent 4c7733c commit 293c4a7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

prelude/prelude.purs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,21 @@ module Prelude
270270
(==) = refEq
271271
(/=) = refIneq
272272

273+
foreign import eqArrayImpl
274+
"function eqArrayImpl(f) {\
275+
\ return function(xs) {\
276+
\ return function(ys) {\
277+
\ if (xs.length !== ys.length) return false;\
278+
\ for (var i = 0; i < xs.length; i++) {\
279+
\ if (!f(xs[i])(ys[i])) return false;\
280+
\ }\
281+
\ return true;\
282+
\ };\
283+
\ };\
284+
\}" :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
285+
273286
instance eqArray :: (Eq a) => Eq [a] where
274-
(==) [] [] = true
275-
(==) (x:xs) (y:ys) = x == y && xs == ys
276-
(==) _ _ = false
287+
(==) xs ys = eqArrayImpl (==) xs ys
277288
(/=) xs ys = not (xs == ys)
278289

279290
data Ordering = LT | GT | EQ

0 commit comments

Comments
 (0)