Created
January 2, 2021 21:41
-
-
Save natefaubion/7337e443b421d07323f550efb6b323c2 to your computer and use it in GitHub Desktop.
Array slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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