<commit_before>{-# LANGUAGE MultiParamTypeClasses #-}
-----------------------------------------------------------------------------
-- |
-- Module     : BLAS.Tensor.ReadOnly
-- Copyright  : Copyright (c) , <NAME> <<EMAIL>>
-- License    : BSD3
-- Maintainer : <NAME> <<EMAIL>>
-- Stability  : experimental
--

module BLAS.Tensor.ReadOnly (
    RTensor(..),
    readElem,
    ) where

import Data.Ix
import BLAS.Tensor.Base

-- | Class for mutable read-only tensors.
class (Tensor x i e, Monad m) => RTensor x i e m where
    -- | Get the number of elements stored in the tensor.
    getSize :: x e -> m Int
    
    -- | Get a copy of the tensor.
    newCopy :: x e -> m (x e)
    
    -- | Creates a new tensor with elements all initialized to zero.
    newZero :: i -> m (x e)
    
    -- | Creates a new tensor with elements all initialized to the 
    -- given value.
    newConstant :: i -> e -> m (x e)
    
    -- | Get the value at the specified index, without doing any 
    -- range-checking.
    unsafeReadElem :: x e -> i -> m e

    -- | Returns a lazy list of the indices in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getIndices :: x e -> m [i]
    getIndices x = getAssocs x >>= return . fst . unzip

    -- | Returns a lazy list of the elements in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getElems :: x e -> m [e]
    getElems x = getAssocs x >>= return . snd . unzip
    
    -- | Returns a lazy list of the elements-index pairs in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getAssocs :: x e -> m [(i,e)]
    getAssocs x = do
        is <- getIndices x
        es <- getElems x
        return $ zip is es


-- | Gets the value at the specified index after checking that the argument
-- is in bounds.
readElem :: (RTensor x i e m, Show i) => x e -> i -> m e
readElem x i =
    case (inRange b i) of
        False -> 
            fail $ "tried to get element at a index `" ++ show i ++ "'"<commit_msg>BLAS/Tensor/ReadOnly.hs: remove newZero and newConstant<commit_after>{-# LANGUAGE MultiParamTypeClasses #-}
-----------------------------------------------------------------------------
-- |
-- Module     : BLAS.Tensor.ReadOnly
-- Copyright  : Copyright (c) , <NAME> <<EMAIL>>
-- License    : BSD3
-- Maintainer : <NAME> <<EMAIL>>
-- Stability  : experimental
--

module BLAS.Tensor.ReadOnly (
    RTensor(..),
    readElem,
    ) where

import Data.Ix
import BLAS.Tensor.Base

-- | Class for mutable read-only tensors.
class (Tensor x i e, Monad m) => RTensor x i e m where
    -- | Get the number of elements stored in the tensor.
    getSize :: x e -> m Int
    
    -- | Get a copy of the tensor.
    newCopy :: x e -> m (x e)
    
    -- | Get the value at the specified index, without doing any 
    -- range-checking.
    unsafeReadElem :: x e -> i -> m e

    -- | Returns a lazy list of the indices in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getIndices :: x e -> m [i]
    getIndices x = getAssocs x >>= return . fst . unzip

    -- | Returns a lazy list of the elements in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getElems :: x e -> m [e]
    getElems x = getAssocs x >>= return . snd . unzip
    
    -- | Returns a lazy list of the elements-index pairs in the tensor.  
    -- Because of the laziness, this function should be used with care.
    getAssocs :: x e -> m [(i,e)]
    getAssocs x = do
        is <- getIndices x
        es <- getElems x
        return $ zip is es


-- | Gets the value at the specified index after checking that the argument
-- is in bounds.
readElem :: (RTensor x i e m, Show i) => x e -> i -> m e
readElem x i =
    case (inRange b i) of
        False -> 
            fail $ "tried to get element at a index `" ++ show i ++ "'"
{'fraction_non_alphanumeric': 0.14714867617107943, 'fraction_numerical': 0.0005091649694501018, 'mean_word_length': 1.8286537077033838, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3270881', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1180, 'n_words': 517}