<commit_before>module CV.ImageOp where

import Foreign
import CV.Image

-- Testing how to handle operation sequences without 
-- copying image for each operation.
newtype ImageOperation = ImgOp (Image -> IO ())
(#>) :: ImageOperation -> ImageOperation -> ImageOperation
(#>) (ImgOp a) (ImgOp b) = ImgOp (\img -> (a img >> b img))
nonOp = ImgOp (\i -> return ())

img <# op = unsafeOperate op img
img <## op = unsafeOperate (foldl1 (#>) op) img

times n op = foldl (#>) nonOp (replicate n op) 

-- This could, if I take enough care, be pure.
runImageOperation :: Image -> ImageOperation -> IO Image
runImageOperation img (ImgOp op) = withClone img $ \clone -> 
                                    op clone >> return clone

directOp i (ImgOp op)  = op i
operate op img = runImageOperation img op
operateOn = runImageOperation
unsafeOperate op img = unsafePerformIO $ operate op img
unsafeOperateOn img op = unsafePerformIO $ operate op img

operateWithROI pos size (ImgOp op) img = withClone img $ \clone ->
                                  withIOROI pos size clone (op clone)<commit_msg>Fixed a bug in ImageOp.<## operator<commit_after>module CV.ImageOp where

import Foreign
import CV.Image

-- Testing how to handle operation sequences without 
-- copying image for each operation.
newtype ImageOperation = ImgOp (Image -> IO ())
(#>) :: ImageOperation -> ImageOperation -> ImageOperation
(#>) (ImgOp a) (ImgOp b) = ImgOp (\img -> (a img >> b img))
nonOp = ImgOp (\i -> return ())

img <# op = unsafeOperate op img
img <## [] = img
img <## op = unsafeOperate (foldl1 (#>) op) img

times n op = foldl (#>) nonOp (replicate n op) 

-- This could, if I take enough care, be pure.
runImageOperation :: Image -> ImageOperation -> IO Image
runImageOperation img (ImgOp op) = withClone img $ \clone -> 
                                    op clone >> return clone

directOp i (ImgOp op)  = op i
operate op img = runImageOperation img op
operateOn = runImageOperation
unsafeOperate op img = unsafePerformIO $ operate op img
unsafeOperateOn img op = unsafePerformIO $ operate op img

operateWithROI pos size (ImgOp op) img = withClone img $ \clone ->
                                  withIOROI pos size clone (op clone)
{'fraction_non_alphanumeric': 0.10483506552191595, 'fraction_numerical': 0.0009037505648441031, 'mean_word_length': 2.9820143884892087, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6220926', 'n_tokens_mistral': 664, 'n_tokens_neox': 640, 'n_words': 262}