Is there anything in containers that allows me to get a subset of Data.Set?

StackOverflow https://stackoverflow.com/questions/22847655

  •  27-06-2023
  •  | 
  •  

سؤال

I cannot find any functions that given a min and a max return all the values in that interval for any of the structures in containers. Surely there must be...?

هل كانت مفيدة؟

المحلول

inRange :: Ord a => a -> a -> Set a -> Set a
inRange min max s = fst $ S.split max gt
  where (_, gt) = S.split min s

نصائح أخرى

You can write this really easily with Data.Set.filter:

import Data.Set (Set)
import qualified Data.Set as S

between :: Ord a => a -> a -> Set a -> Set a
between min_ max_ = S.filter (\x -> min_ <= x && x <= max_)

Then you can use it as

> between 3 7 $ S.fromList [1..10]
fromList [3, 4, 5, 6, 7]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top