on 2d29699 help sign in
elk/bench/src/PatternMatchBench.elm
70 lines 1.9 kB history raw
1module PatternMatchBench where
3type NestedStructure =
4 Nest Float (NestedStructure)
5 | End Float
6
8intToNest : Int -> NestedStructure
9intToNest i =
10 let
11 helper i accum =
12 case (i % 20) of
13 0 ->
14 accum
16 _ ->
17 helper (i-1) <| Nest (toFloat <| i * i) accum
18 in
19 helper i (End <| toFloat i)
21deepMatchingFunction : NestedStructure -> Float
22deepMatchingFunction struct =
23 case struct of
24 (Nest x1 (Nest x2 (Nest x3 (Nest x4 (Nest x5 (Nest x6 (Nest x7 (Nest x8 (Nest x9 (Nest x10 (Nest x11 (Nest x12 (Nest x13 (Nest x14 (Nest x15 (Nest x16 (Nest x17 (End x18 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ->
25 x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18
27 Nest x _ ->
28 x
30 End x ->
31 x
34wildcardMatchingFunction : NestedStructure -> Float
35wildcardMatchingFunction struct =
36 case struct of
37 (Nest x (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (End y ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ->
38 x + y + x + y + x + y + x + y + x + y + x + y + x + y + x + y + x + y
40 Nest x _ ->
41 x
43 End x ->
44 x
47shallowMatchingFunction : NestedStructure -> Float
48shallowMatchingFunction struct =
49 case struct of
50 (Nest x1 (Nest x2 (Nest x3 (End x4) ) ) ) ->
51 x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2
53 Nest x _ ->
54 x
56 End x ->
57 x
59sumStructures : (NestedStructure -> Float) -> List NestedStructure -> Float
60sumStructures f inList =
61 List.foldr (\x sumSoFar -> sumSoFar + f x) 0.0 inList
64deepMatchSum = sumStructures deepMatchingFunction
67shallowMatchSum = sumStructures shallowMatchingFunction
70wildcardMatchSum = sumStructures wildcardMatchingFunction