Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created January 2, 2021 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natefaubion/7337e443b421d07323f550efb6b323c2 to your computer and use it in GitHub Desktop.
Save natefaubion/7337e443b421d07323f550efb6b323c2 to your computer and use it in GitHub Desktop.
Array slice
module Main where
import Prelude
import Data.Array as Array
import Partial.Unsafe (unsafePartial)
data Slice a = Slice (Array a) Int Int
fromArray :: forall a. Array a -> Slice a
fromArray arr = Slice arr 0 (Array.length arr)
toArray :: forall a. Slice a -> Array a
toArray (Slice arr beg end)
| beg == 0 && end == Array.length arr =
arr
| otherwise =
Array.slice beg end arr
data Uncons a
= Cons a (Slice a)
| Nil
uncons :: forall a. Slice a -> Uncons a
uncons (Slice arr beg end)
| beg == end =
Nil
| otherwise =
unsafePartial $ Cons (Array.unsafeIndex arr beg) (Slice arr (beg + 1) end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment