From cc55bc1ebff22251bde2a59bd1394ef6d5f598ef Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 14 Jun 2015 16:43:50 +0200 Subject: [PATCH] Fix updateAt and alterAt to get tests running --- docs/Data.List.Lazy.md | 423 ++++++++++++++++++---------------------- src/Data/List/Lazy.purs | 4 +- 2 files changed, 195 insertions(+), 232 deletions(-) diff --git a/docs/Data.List.Lazy.md b/docs/Data.List.Lazy.md index 6e21305..7595eba 100644 --- a/docs/Data.List.Lazy.md +++ b/docs/Data.List.Lazy.md @@ -48,13 +48,25 @@ runList :: forall a. List a -> Lazy (Step a) Unwrap a lazy linked list -#### `step` +#### `fromList` ``` purescript -step :: forall a. List a -> Step a +fromList :: forall f a. (Unfoldable f) => List a -> f a ``` -Unwrap a lazy linked list +Convert a list into any unfoldable structure. + +Running time: `O(n)` + +#### `toList` + +``` purescript +toList :: forall f a. (Foldable f) => f a -> List a +``` + +Construct a list from a foldable structure. + +Running time: `O(n)` #### `Step` @@ -68,6 +80,14 @@ A list is either empty (represented by the `Nil` constructor) or non-empty, in which case it consists of a head element, and another list (represented by the `Cons` constructor). +#### `step` + +``` purescript +step :: forall a. List a -> Step a +``` + +Unwrap a lazy linked list + #### `nil` ``` purescript @@ -78,35 +98,27 @@ The empty list. Running time: `O(1)` -#### `cons` +#### `singleton` ``` purescript -cons :: forall a. a -> List a -> List a +singleton :: forall a. a -> List a ``` -Attach an element to the front of a lazy list. - -Running time: `O(1)` - -#### `toList` +#### `(..)` ``` purescript -toList :: forall f a. (Foldable f) => f a -> List a +(..) :: Int -> Int -> List Int ``` -Construct a list from a foldable structure. +An infix synonym for `range`. -Running time: `O(n)` - -#### `fromList` +#### `range` ``` purescript -fromList :: forall f a. (Unfoldable f) => List a -> f a +range :: Int -> Int -> List Int ``` -Convert a list into any unfoldable structure. - -Running time: `O(n)` +Create a list containing a range of integers, including both endpoints. #### `repeat` @@ -132,13 +144,27 @@ cycle :: forall a. List a -> List a Create a list by repeating another list -#### `unfold` +#### `null` ``` purescript -unfold :: forall a b. (b -> Maybe (Tuple a b)) -> b -> List a +null :: forall a. List a -> Boolean ``` -Unfold a list using a generating function +#### `length` + +``` purescript +length :: forall a. List a -> Int +``` + +Get the length of a list + +Running time: `O(n)` + +#### `cons` + +``` purescript +cons :: forall a. a -> List a -> List a +``` #### `(:)` @@ -151,229 +177,257 @@ a list. Running time: `O(1)` -#### `singleton` +#### `insert` ``` purescript -singleton :: forall a. a -> List a +insert :: forall a. (Ord a) => a -> List a -> List a ``` -Create a list with a single element. +Insert an element into a sorted list. -Running time: `O(1)` +Running time: `O(n)` -#### `uncons` +#### `insertBy` ``` purescript -uncons :: forall a. List a -> Maybe (Tuple a (List a)) +insertBy :: forall a. (a -> a -> Ordering) -> a -> List a -> List a ``` -Break a list into its first element, and the remaining elements, -or `Nothing` if the list is empty. +Insert an element into a sorted list, using the specified function to determine the ordering +of elements. -Running time: `O(1)` +Running time: `O(n)` -#### `index` +#### `head` ``` purescript -index :: forall a. List a -> Int -> Maybe a +head :: forall a. List a -> Maybe a ``` -Get the element at the specified index, or `Nothing` if the index is out-of-bounds. - -Running time: `O(n)` where `n` is the required index. - -#### `(!!)` +#### `last` ``` purescript -(!!) :: forall a. List a -> Int -> Maybe a +last :: forall a. List a -> Maybe a ``` -An infix synonym for `index`. +Get the last element in a list, or `Nothing` if the list is empty. -#### `drop` +Running time: `O(n)`. + +#### `tail` ``` purescript -drop :: forall a. Int -> List a -> List a +tail :: forall a. List a -> Maybe (List a) ``` -Drop the specified number of elements from the front of a list. +Get all but the first element of a list, or `Nothing` if the list is empty. -Running time: `O(n)` where `n` is the number of elements to drop. +Running time: `O(1)` -#### `dropWhile` +#### `init` ``` purescript -dropWhile :: forall a. (a -> Boolean) -> List a -> List a +init :: forall a. List a -> Maybe (List a) ``` -Drop those elements from the front of a list which match a predicate. +Get all but the last element of a list, or `Nothing` if the list is empty. -Running time (worst case): `O(n)` +Running time: `O(n)` -#### `take` +#### `uncons` ``` purescript -take :: forall a. Int -> List a -> List a +uncons :: forall a. List a -> Maybe { head :: a, tail :: List a } ``` -Take the specified number of elements from the front of a list. +Break a list into its first element, and the remaining elements, +or `Nothing` if the list is empty. -Running time: `O(n)` where `n` is the number of elements to take. +Running time: `O(1)` -#### `takeWhile` +#### `index` ``` purescript -takeWhile :: forall a. (a -> Boolean) -> List a -> List a +index :: forall a. List a -> Int -> Maybe a ``` -Take those elements from the front of a list which match a predicate. +#### `(!!)` -Running time (worst case): `O(n)` +``` purescript +(!!) :: forall a. List a -> Int -> Maybe a +``` -#### `length` +An infix synonym for `index`. + +#### `insertAt` ``` purescript -length :: forall a. List a -> Int +insertAt :: forall a. Int -> a -> List a -> List a ``` -Get the length of a list +Insert an element into a list at the specified index, returning a new +list or `Nothing` if the index is out-of-bounds. + +This function differs from the strict equivalent in that out-of-bounds arguments +result in the element being appended at the _end_ of the list. Running time: `O(n)` -#### `filter` +#### `deleteAt` ``` purescript -filter :: forall a. (a -> Boolean) -> List a -> List a +deleteAt :: forall a. Int -> List a -> List a ``` -Filter a list, keeping the elements which satisfy a predicate function. +Delete an element from a list at the specified index, returning a new +list or `Nothing` if the index is out-of-bounds. + +This function differs from the strict equivalent in that out-of-bounds arguments +result in the original list being returned unchanged. Running time: `O(n)` -#### `mapMaybe` +#### `updateAt` ``` purescript -mapMaybe :: forall a b. (a -> Maybe b) -> List a -> List b +updateAt :: forall a. Int -> a -> List a -> List a ``` -Apply a function to each element in a list, keeping only the results which -contain a value. +Update the element at the specified index, returning a new +list or `Nothing` if the index is out-of-bounds. + +This function differs from the strict equivalent in that out-of-bounds arguments +result in the original list being returned unchanged. Running time: `O(n)` -#### `catMaybes` +#### `modifyAt` ``` purescript -catMaybes :: forall a. List (Maybe a) -> List a +modifyAt :: forall a. Int -> (a -> a) -> List a -> List a ``` -Filter a list of optional values, keeping only the elements which contain -a value. +Update the element at the specified index by applying a function to +the current value, returning a new list or `Nothing` if the index is +out-of-bounds. -#### `head` +This function differs from the strict equivalent in that out-of-bounds arguments +result in the original list being returned unchanged. + +Running time: `O(n)` + +#### `alterAt` ``` purescript -head :: forall a. List a -> Maybe a +alterAt :: forall a. Int -> (a -> Maybe a) -> List a -> List a ``` -Get the first element in a list, or `Nothing` if the list is empty. +Update or delete the element at the specified index by applying a +function to the current value, returning a new list or `Nothing` if the +index is out-of-bounds. -Running time: `O(1)`. +This function differs from the strict equivalent in that out-of-bounds arguments +result in the original list being returned unchanged. -#### `tail` +Running time: `O(n)` + +#### `reverse` ``` purescript -tail :: forall a. List a -> Maybe (List a) +reverse :: forall a. List a -> List a ``` -Get all but the first element of a list, or `Nothing` if the list is empty. +#### `concat` -Running time: `O(1)` +``` purescript +concat :: forall a. List (List a) -> List a +``` -#### `last` +Flatten a list of lists. + +Running time: `O(n)`, where `n` is the total number of elements. + +#### `concatMap` ``` purescript -last :: forall a. List a -> Maybe a +concatMap :: forall a b. (a -> List b) -> List a -> List b ``` -Get the last element in a list, or `Nothing` if the list is empty. +Apply a function to each element in a list, and flatten the results +into a single, new list. -Running time: `O(n)`. +Running time: `O(n)`, where `n` is the total number of elements. -#### `init` +#### `filter` ``` purescript -init :: forall a. List a -> Maybe (List a) +filter :: forall a. (a -> Boolean) -> List a -> List a ``` -Get all but the last element of a list, or `Nothing` if the list is empty. +Filter a list, keeping the elements which satisfy a predicate function. Running time: `O(n)` -#### `zipWith` +#### `mapMaybe` ``` purescript -zipWith :: forall a b c. (a -> b -> c) -> List a -> List b -> List c +mapMaybe :: forall a b. (a -> Maybe b) -> List a -> List b ``` -Apply a function to pairs of elements at the same positions in two lists, -collecting the results in a new list. +Apply a function to each element in a list, keeping only the results which +contain a value. -If one list is longer, elements will be discarded from the longer list. +Running time: `O(n)` -For example +#### `catMaybes` -```purescript -zipWith (*) (1 : 2 : 3 : Nil) (4 : 5 : 6 : 7 Nil) == 4 : 10 : 18 : Nil +``` purescript +catMaybes :: forall a. List (Maybe a) -> List a ``` -Running time: `O(min(m, n))` +Filter a list of optional values, keeping only the elements which contain +a value. -#### `zip` +#### `take` ``` purescript -zip :: forall a b. List a -> List b -> List (Tuple a b) +take :: forall a. Int -> List a -> List a ``` -Collect pairs of elements at the same positions in two lists. - -Running time: `O(min(m, n))` - -#### `concat` +#### `takeWhile` ``` purescript -concat :: forall a. List (List a) -> List a +takeWhile :: forall a. (a -> Boolean) -> List a -> List a ``` -Flatten a list of lists. +Take those elements from the front of a list which match a predicate. -Running time: `O(n)`, where `n` is the total number of elements. +Running time (worst case): `O(n)` -#### `concatMap` +#### `drop` ``` purescript -concatMap :: forall a b. (a -> List b) -> List a -> List b +drop :: forall a. Int -> List a -> List a ``` -Apply a function to each element in a list, and flatten the results -into a single, new list. +Drop the specified number of elements from the front of a list. -Running time: `O(n)`, where `n` is the total number of elements. +Running time: `O(n)` where `n` is the number of elements to drop. -#### `null` +#### `dropWhile` ``` purescript -null :: forall a. List a -> Boolean +dropWhile :: forall a. (a -> Boolean) -> List a -> List a ``` -Test whether a list is empty. +Drop those elements from the front of a list which match a predicate. -Running time: `O(1)` +Running time (worst case): `O(n)` #### `span` ``` purescript -span :: forall a. (a -> Boolean) -> List a -> Tuple (List a) (List a) +span :: forall a. (a -> Boolean) -> List a -> { init :: List a, rest :: List a } ``` Split a list into two parts: @@ -416,50 +470,43 @@ equivalence relation to determine equality. Running time: `O(n)` -#### `(\\)` +#### `nub` ``` purescript -(\\) :: forall a. (Eq a) => List a -> List a -> List a +nub :: forall a. (Eq a) => List a -> List a ``` -Delete the first occurrence of each element in the second list from the first list. - -Running time: `O(n^2)` - -#### `insert` +#### `nubBy` ``` purescript -insert :: forall a. (Ord a) => a -> List a -> List a +nubBy :: forall a. (a -> a -> Boolean) -> List a -> List a ``` -Insert an element into a sorted list. +Remove duplicate elements from a list, using the specified +function to determine equality of elements. -Running time: `O(n)` +Running time: `O(n^2)` -#### `insertBy` +#### `union` ``` purescript -insertBy :: forall a. (a -> a -> Ordering) -> a -> List a -> List a +union :: forall a. (Eq a) => List a -> List a -> List a ``` -Insert an element into a sorted list, using the specified function to determine the ordering -of elements. +Calculate the union of two lists. -Running time: `O(n)` +Running time: `O(n^2)` -#### `insertAt` +#### `unionBy` ``` purescript -insertAt :: forall a. Int -> a -> List a -> List a +unionBy :: forall a. (a -> a -> Boolean) -> List a -> List a -> List a ``` -Insert an element into a list at the specified index, returning a new -list or `Nothing` if the index is out-of-bounds. - -This function differs from the strict equivalent in that out-of-bounds arguments -result in the element being appended at the _end_ of the list. +Calculate the union of two lists, using the specified +function to determine equality of elements. -Running time: `O(n)` +Running time: `O(n^2)` #### `delete` @@ -482,92 +529,13 @@ function to determine equality of elements. Running time: `O(n)` -#### `deleteAt` - -``` purescript -deleteAt :: forall a. Int -> List a -> List a -``` - -Delete an element from a list at the specified index, returning a new -list or `Nothing` if the index is out-of-bounds. - -This function differs from the strict equivalent in that out-of-bounds arguments -result in the original list being returned unchanged. - -Running time: `O(n)` - -#### `updateAt` - -``` purescript -updateAt :: forall a. Int -> a -> List a -> List a -``` - -Update the element at the specified index, returning a new -list or `Nothing` if the index is out-of-bounds. - -This function differs from the strict equivalent in that out-of-bounds arguments -result in the original list being returned unchanged. - -Running time: `O(n)` - -#### `modifyAt` - -``` purescript -modifyAt :: forall a. Int -> (a -> a) -> List a -> List a -``` - -Update the element at the specified index by applying a function to -the current value, returning a new list or `Nothing` if the index is -out-of-bounds. - -This function differs from the strict equivalent in that out-of-bounds arguments -result in the original list being returned unchanged. - -Running time: `O(n)` - -#### `alterAt` - -``` purescript -alterAt :: forall a. Int -> (a -> Maybe a) -> List a -> List a -``` - -Update or delete the element at the specified index by applying a -function to the current value, returning a new list or `Nothing` if the -index is out-of-bounds. - -This function differs from the strict equivalent in that out-of-bounds arguments -result in the original list being returned unchanged. - -Running time: `O(n)` - -#### `reverse` - -``` purescript -reverse :: forall a. List a -> List a -``` - -Reverse a list. - -Running time: `O(n)` - -#### `nub` - -``` purescript -nub :: forall a. (Eq a) => List a -> List a -``` - -Remove duplicate elements from a list. - -Running time: `O(n^2)` - -#### `nubBy` +#### `(\\)` ``` purescript -nubBy :: forall a. (a -> a -> Boolean) -> List a -> List a +(\\) :: forall a. (Eq a) => List a -> List a -> List a ``` -Remove duplicate elements from a list, using the specified -function to determine equality of elements. +Delete the first occurrence of each element in the second list from the first list. Running time: `O(n^2)` @@ -592,25 +560,20 @@ function to determine equality of elements. Running time: `O(n^2)` -#### `union` +#### `zipWith` ``` purescript -union :: forall a. (Eq a) => List a -> List a -> List a +zipWith :: forall a b c. (a -> b -> c) -> List a -> List b -> List c ``` -Calculate the union of two lists. - -Running time: `O(n^2)` - -#### `unionBy` +#### `zip` ``` purescript -unionBy :: forall a. (a -> a -> Boolean) -> List a -> List a -> List a +zip :: forall a b. List a -> List b -> List (Tuple a b) ``` -Calculate the union of two lists, using the specified -function to determine equality of elements. +Collect pairs of elements at the same positions in two lists. -Running time: `O(n^2)` +Running time: `O(min(m, n))` diff --git a/src/Data/List/Lazy.purs b/src/Data/List/Lazy.purs index a1c55e3..a9b0bef 100644 --- a/src/Data/List/Lazy.purs +++ b/src/Data/List/Lazy.purs @@ -340,7 +340,7 @@ updateAt n x xs = List (go n <$> runList xs) where go _ Nil = Nil go 0 (Cons _ ys) = Cons x ys - go n (Cons y ys) = Cons y (deleteAt (n - 1) ys) + go n (Cons y ys) = Cons y (updateAt (n - 1) x ys) -- | Update the element at the specified index by applying a function to -- | the current value, returning a new list or `Nothing` if the index is @@ -368,7 +368,7 @@ alterAt n f xs = List (go n <$> runList xs) go 0 (Cons y ys) = case f y of Nothing -> step ys Just y' -> Cons y' ys - go n (Cons y ys) = Cons y (deleteAt (n - 1) ys) + go n (Cons y ys) = Cons y (alterAt (n - 1) f ys) -------------------------------------------------------------------------------- -- Transformations -------------------------------------------------------------