Open jfmengels opened 11 months ago
A super nice set of checks (I love the get and length checks).
Maybe
Array.fromList []
--> Array.empty
array |> Array.toIndexedList |> List.map Tuple.second
--> array |> Array.toList
Array.repeat 1 x
--> Array.fromList [ x ]
And for Array.slice
I think we can just copy the fixes we already have for String.slice
which also allows negative indexes
I was a little torn on
Array.repeat 1 x
--> Array.fromList [ x ]
but I guess it is a lot more idiomatic, so let's do it, yes. I'll also add
Array.initialize 1 f
--> Array.fromList [ f x ]
I think you meant
Array.initialize 1 f
--> Array.fromList [ f 0 ]
The suggested check
Array.append (Array.repeat n x) (Array.repeat m x)
--> Array.repeat (n + m) x (if n and m are literals, add them up directly)
Only works if we know them as literals and neither is negative. E.g.
let m = -1
in
Array.append (Array.repeat 2 x) (Array.repeat m x)
--> Array.repeat 0 x
would be wrong. (The correct result in this case is Array.repeat 2 x
)
Equally,
Array.push x (Array.repeat n x)
--> Array.repeat (n + 1) x (if n is a literal, just increment it)
Only works if we know n
as a non-negative literal. E.g.
let n = -1
in
Array.push x (Array.repeat n x)
--> Array.repeat 0 x
would be wrong. (The correct result in this case is Array.push x Array.empty
)
All good points :+1:
Should we also do Array.push x Array.empty
--> Array.fromList [ x ]
? It's more idiomatic, though I suspect the former to be faster, but also by only a tiny amount. (And by that logic we should not fix 1 :: []
to [ 1 ]
)
Should we simplify Array.length (Array.repeat n x)
to max 0 n
? It's not the prettiest fix but it's quite a bit simpler and definitely more performant.
I added Array.repeat n x |> Array.toList
-> List.repeat n x
I added Array.get n (Array.repeat n x)
-> Nothing
(and same for initialize
)
array |> Array.toIndexedList |> List.map Tuple.first to List.range 0 (List.length array - 1)
should probably be
array |> Array.toIndexedList |> List.map Tuple.first to List.range 0 (Array.length array - 1)
Seems like we have no simplifications for any
Array
functions. Don't know how I missed that because there's no work nor issues on that :sweat_smile:Array.map
#169Array.filter
#171Array.fromList
Array.fromList []
->Array.empty
#177Array.toList >> Array.fromList
->identity
#190Array.toList
Array.fromList >> Array.toList
->identity
#190Array.toList Array.empty
->[]
#234Array.isEmpty
Array.isEmpty (Array.fromList [x])
->False
Array.isEmpty (Array.fromList [])
->True
Array.isEmpty Array.empty
->True
Array.isEmpty (Array.repeat 1 x)
->False
Array.isEmpty (Array.initialize 1 f)
->False
Array.repeat
Array.repeat 0 x
->Array.empty
#179Array.repeat -1 x
->Array.empty
#179Array.repeat n x |> Array.toList
->List.repeat n x
#234(Good fixes are too inconsistent)Array.repeat 1 x
->Array.fromList [ x ]
(Only when the number is 1)Array.initialize
Array.initialize 0 f
->Array.empty
#185Array.initialize -1 f
->Array.empty
#185(Good fixes are too inconsistent)Array.initialize 1 f
->Array.fromList [ f 0 ]
(Only when the number is 1)Array.length
Array.length Array.empty
->0
#191Array.length (Array.fromList [ a, b, c ])
->3
#191Array.length (Array.repeat 1 x)
->1
#191Array.length (Array.initialize 1 f)
->1
#191Array.length (Array.repeat n x)
tomax 0 n
#191Array.length (Array.initialize n f)
tomax 0 n
#191Array.length (Array.push x (Array.fromList [ a, b, c ]))
->4
Array.length (Array.push x (Array.repeat 3 y))
->4
(same forArray.initialize
)Array.length (Array.push x (Array.repeat n y))
->(Basics.max 0 n) + 1
(same forArray.initialize
)Array.indexedMap
List.indexedMap
#192Array.append
Array.append Array.empty array
->array
#199Array.append array Array.empty
->array
#199Array.append (Array.fromList [ a, b, c ]) (Array.fromList [ d, e, f ])
->Array.fromList [ a, b, c, d, e, f ]
#199Array.append (Array.repeat 2 x) (Array.repeat 3 x)
->Array.repeat 5 x
Array.append (Array.repeat n x) (Array.repeat m x)
->Array.repeat (max 0 n + max 0 m) x
Array.slice
String.slice
#208Array.get
Array.get n Array.empty
->Nothing
#203Array.get -3 array
->Nothing
#203Array.get 2 (Array.fromList [ 1, 2, 3 ])
->Just 3
#203Array.get 100 (Array.fromList [ 1, 2, 3 ])
->Nothing
#203Array.get 2 (Array.repeat 10 x)
->Just x
#252Array.get 100 (Array.repeat 10 x)
->Nothing
#252Array.get 2 (Array.initialize 10 f)
->Just (f 2)
#252Array.get 100 (Array.initialize 10 f)
->Nothing
#252Array.get n (Array.repeat n x)
->Nothing
Array.get n (Array.initialize n f)
->Nothing
Array.get 1 (Array.push x <| Array.push y array)
->Just y
Array.set
Array.set n x Array.empty
->Array.empty
#207Array.set 1 x (Array.fromList [ 1, 2, 3 ])
->Array.fromList [ 1, x, 3 ]
#207Array.set 100 x (Array.fromList [ 1, 2, 3 ])
->Array.fromList [ 1, 2, 3 ]
#207Array.set -3 x array
->array
#207Array.set 100 x (Array.repeat 10 y)
->Array.repeat 10 y
Array.set 100 x (Array.initialize 10 f)
->Array.initialize 10 f
Array.push
Array.push x Array.empty
->Array.fromList [ x ]
Array.push x (Array.fromList [ a, b, c ])
->Array.fromList [ a, b, c, x ]
Array.push x (Array.repeat n x)
->Array.repeat (max 0 n + 1) x
Array.foldl
andArray.foldr
Array.foldr f x Array.empty
->x
#231List
fold operations #231Array.toIndexedList
Array.toIndexedList Array.empty
->[]
#232array |> Array.toIndexedList |> List.map Tuple.second
toarray |> Array.toList
#232array |> Array.toIndexedList |> List.map Tuple.first
toList.range 0 (Array.length array - 1)
Leave a comment if I missed something :)
(I'll probably move this to another issue when I have time) We could also do simplifications across types: