Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created November 23, 2016 08:55
Show Gist options
  • Save ccapndave/2d459020227e1fef604abb829366f356 to your computer and use it in GitHub Desktop.
Save ccapndave/2d459020227e1fef604abb829366f356 to your computer and use it in GitHub Desktop.
import Html exposing (Html, div, hr, text)
import Array exposing (..)
-- Some function taken directly from Array.Extra
splitAt : Int -> Array a -> ( Array a, Array a )
splitAt index xs =
-- TODO: refactor (written this way to help avoid Array bugs)
let
len =
length xs
in
case ( index > 0, index < len ) of
( True, True ) ->
( slice 0 index xs, slice index len xs )
( True, False ) ->
( xs, empty )
( False, True ) ->
( empty, xs )
( False, False ) ->
( empty, empty )
sliceUntil : Int -> Array a -> Array a
sliceUntil n a =
if n >= 0 then
slice 0 n a
else
slice 0 (length a + n) a
-- A bit of a test harness
main =
let
init : Array String
init = Array.fromList ["a", "b", "c", "d", "e"]
assert : a -> a -> Html b
assert actual expected =
if actual /= expected then
text <| " × " ++ (toString actual) ++ " /= " ++ (toString expected)
else
text " ✓"
in
div []
[ assert init init
, hr [] []
, assert (move 0 3 init) (Array.fromList ["b", "c", "d", "a", "e"])
, hr [] []
, assert (move 4 0 init) (Array.fromList ["e", "a", "b", "c", "d"])
, hr [] []
, assert (move 2 3 init) (Array.fromList ["a", "b", "d", "c", "e"])
, hr [] []
, assert (move 3 2 init) (Array.fromList ["a", "b", "d", "c", "e"])
]
{-| The function!
-}
move : Int -> Int -> Array a -> Array a
move fromIdx toIdx array =
array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment