{-# LANGUAGE Trustworthy #-} -- | -- Module : Data.Text.Short.Partial -- Copyright : © Herbert Valerio Riedel 2018 -- License : BSD3 -- -- Maintainer : hvr@gnu.org -- Stability : stable -- -- Partial functions vocabulary -- -- This module provides common partial functions for operating on 'ShortText'. -- -- The use of these functions is discouraged as they tend to be error-prone. -- -- @since 0.1.2 module Data.Text.Short.Partial ( head , tail , init , last , index , foldl1 , foldl1' , foldr1 ) where import Data.Text.Short import Data.Text.Short.Internal import Prelude () -- | \(\mathcal{O}(1)\) Returns first character of a non-empty 'ShortText' -- -- >>> head "abcd" -- 'a' -- -- __Note__: Will throw an 'error' exception for empty 'ShortText's. -- Consider using the total functions 'uncons' or 'indexMaybe' -- instead. -- -- @since 0.1.2 head :: ShortText -> Char head :: ShortText -> Char head = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. HasCallStack => [Char] -> a error [Char] "head: empty ShortText") forall a b. (a, b) -> a fst forall b c a. (b -> c) -> (a -> b) -> a -> c . ShortText -> Maybe (Char, ShortText) uncons -- | \(\mathcal{O}(n)\) Drop first character from non-empty 'ShortText'. -- -- >>> tail "abcd" -- "bcd" -- -- __Note__: Will throw an 'error' exception for empty 'ShortText's. -- Consider using the total functions 'uncons' or 'drop' instead. -- -- @since 0.1.2 tail :: ShortText -> ShortText tail :: ShortText -> ShortText tail = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. HasCallStack => [Char] -> a error [Char] "tail: empty ShortText") forall a b. (a, b) -> b snd forall b c a. (b -> c) -> (a -> b) -> a -> c . ShortText -> Maybe (Char, ShortText) uncons -- | \(\mathcal{O}(n)\) Drop last character from non-empty 'ShortText'. -- -- >>> tail "abcd" -- "bcd" -- -- __Note__: Will throw an 'error' exception for empty 'ShortText's. -- Consider using the total functions 'unsnoc' or 'dropEnd' instead. -- -- @since 0.1.2 init :: ShortText -> ShortText init :: ShortText -> ShortText init = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. HasCallStack => [Char] -> a error [Char] "init: empty ShortText") forall a b. (a, b) -> a fst forall b c a. (b -> c) -> (a -> b) -> a -> c . ShortText -> Maybe (ShortText, Char) unsnoc -- | \(\mathcal{O}(1)\) Return last character from non-empty 'ShortText'. -- -- >>> last "abcd" -- 'd' -- -- __Note__: Will throw an 'error' exception for empty 'ShortText's. -- Consider using the total functions 'unsnoc' or 'indexEndMaybe' -- instead. -- -- @since 0.1.2 last :: ShortText -> Char last :: ShortText -> Char last = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. HasCallStack => [Char] -> a error [Char] "last: empty ShortText") forall a b. (a, b) -> b snd forall b c a. (b -> c) -> (a -> b) -> a -> c . ShortText -> Maybe (ShortText, Char) unsnoc -- | \(\mathcal{O}(n)\) Retrieve \(i\)-th character (code-point) -- -- >>> index "abcd" 1 -- 'b' -- -- __Note__: Will throw an 'error' exception if index is out of -- bounds. Consider using the total functions 'indexMaybe' or -- 'indexEndMaybe' instead. -- -- @since 0.1.2 index :: ShortText -> Int -> Char index :: ShortText -> Int -> Char index ShortText st Int i = case ShortText -> Int -> Maybe Char indexMaybe ShortText st Int i of Maybe Char Nothing -> forall a. HasCallStack => [Char] -> a error [Char] "index: not within ShortText" Just Char c -> Char c -- $setup -- >>> :set -XOverloadedStrings