-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add internal library for unsafe base functions
- Loading branch information
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{-# OPTIONS_GHC -fno-warn-x-partial #-} | ||
|
||
-- | | ||
-- Module: unsafe.Data.Foldable.Unsafe | ||
-- Copyright: Copyright © 2024 Kadena LLC. | ||
-- License: MIT | ||
-- Maintainer: Pact Team | ||
-- Stability: experimental | ||
-- | ||
-- This module provides unsafe versions for all functions in "Data.Foldable" | ||
-- that are either partial or return a 'Maybe' value. | ||
-- | ||
module Data.Foldable.Unsafe | ||
( | ||
-- * Unsafe versions of partial functions | ||
unsafeMaximum | ||
, unsafeMaximumBy | ||
, unsafeMinimum | ||
, unsafeMinimumBy | ||
|
||
-- * Unsafe versions of functions that return 'Maybe' values | ||
, unsafeFind | ||
) where | ||
|
||
import Data.Foldable | ||
|
||
import GHC.Stack | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Unsafe versions of partial functions | ||
|
||
unsafeMaximum :: HasCallStack => Foldable t => Ord a => t a -> a | ||
unsafeMaximum = maximum | ||
|
||
unsafeMaximumBy :: HasCallStack => Foldable t => (a -> a -> Ordering) -> t a -> a | ||
unsafeMaximumBy = maximumBy | ||
|
||
unsafeMinimum :: HasCallStack => (Foldable t, Ord a) => t a -> a | ||
unsafeMinimum = minimum | ||
|
||
unsafeMinimumBy :: HasCallStack => Foldable t => (a -> a -> Ordering) -> t a -> a | ||
unsafeMinimumBy = minimumBy | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Unsafe versions of functions that return Maybe | ||
|
||
unsafeFind :: HasCallStack => Foldable t => (a -> Bool) -> t a -> a | ||
unsafeFind a b = case find a b of | ||
Nothing -> error "Data.List.Unsafe.unsafeFind: not found" | ||
Just x -> x | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# OPTIONS_GHC -fno-warn-x-partial #-} | ||
|
||
-- | | ||
-- Module: Data.List.Unsafe | ||
-- Copyright: Copyright © 2024 Kadena LLC. | ||
-- License: MIT | ||
-- Maintainer: Pact Team | ||
-- Stability: experimental | ||
-- | ||
-- This module provides unsafe versions for all functions in "Data.List" that | ||
-- are either partial or return a 'Maybe' value. | ||
-- | ||
module Data.List.Unsafe | ||
( | ||
-- * Unsafe versions of partial functions | ||
unsafeHead | ||
, unsafeLast | ||
, unsafeTail | ||
, unsafeInit | ||
, unsafeIndex | ||
, unsafeGenericIndex | ||
|
||
-- * Unsafe versions of functions that return 'Maybe' values | ||
, unsafeUncons | ||
, unsafeUnsnoc | ||
, unsafeLookup | ||
, unsafeElemIndex | ||
, unsafeFindIndex | ||
, unsafeStripPrefix | ||
) where | ||
|
||
import Data.List | ||
|
||
import GHC.Stack | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Unsafe versions of partial functions | ||
|
||
unsafeHead :: HasCallStack => [a] -> a | ||
unsafeHead = head | ||
|
||
unsafeLast :: HasCallStack => [a] -> a | ||
unsafeLast = last | ||
|
||
unsafeTail :: HasCallStack => [a] -> [a] | ||
unsafeTail = tail | ||
|
||
unsafeInit :: HasCallStack => [a] -> [a] | ||
unsafeInit = init | ||
|
||
unsafeIndex :: HasCallStack => [a] -> Int -> a | ||
unsafeIndex = (!!) | ||
|
||
unsafeGenericIndex :: Integral i => [a] -> i -> a | ||
unsafeGenericIndex = genericIndex | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Unsafe versions of functions that return Maybe | ||
|
||
unsafeUncons :: HasCallStack => [a] -> (a, [a]) | ||
unsafeUncons a = case uncons a of | ||
Nothing -> error "Data.List.Unsafe.unsafeUncons: empty list" | ||
Just x -> x | ||
|
||
unsafeUnsnoc :: [a] -> ([a], a) | ||
unsafeUnsnoc a = case unsnoc a of | ||
Nothing -> error "Data.List.Unsafe.unsafeUnsnoc: empty list" | ||
Just x -> x | ||
|
||
unsafeLookup :: HasCallStack => Eq a => a -> [(a,b)] -> b | ||
unsafeLookup a b = case lookup a b of | ||
Nothing -> error "Data.List.Unsafe.unsafeLookup: not found" | ||
Just x -> x | ||
|
||
unsafeElemIndex :: Eq a => a -> [a] -> Int | ||
unsafeElemIndex a b = case elemIndex a b of | ||
Nothing -> error "Data.List.Unsafe.unsafeElemIndex: not found" | ||
Just x -> x | ||
|
||
unsafeFindIndex :: (a -> Bool) -> [a] -> Int | ||
unsafeFindIndex a b = case findIndex a b of | ||
Nothing -> error "Data.List.Unsafe.unsafeFindIndex: not found" | ||
Just x -> x | ||
|
||
unsafeStripPrefix :: Eq a => [a] -> [a] -> [a] | ||
unsafeStripPrefix a b = case stripPrefix a b of | ||
Nothing -> error "Data.List.Unsafe.unsafeStripPrefix: not found" | ||
Just x -> x | ||
|
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