Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Data.Set
, subset
, properSubset
, intersection
, choose
) where

import Prelude hiding (map)
Expand Down Expand Up @@ -176,3 +177,11 @@ intersection s1 s2 = fromFoldable $ runPure (runSTArray (emptySTArray >>= inters
LT -> pure $ Loop {a: l + 1, b: r}
GT -> pure $ Loop {a: l, b: r + 1}
else pure $ Done acc

-- | The set of all subsets of specified size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the following are missing:

  • Running time, preferably in big O
  • An example in the docs
  • At least one full sentence in the docs - this seems a little unnecessarily abridged. In particular, what happens when the provided Int argument is larger than the size of the provided set?
  • Handling for the case where a negative Int argument is supplied.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're absolutely right. I threw both this and powerSet over the fence a bit prematurely. I'll fix them up.

choose :: forall a. Ord a => Set a -> Int -> Set (Set a)
choose = go (singleton empty)
where
go acc _ 0 = acc
go acc s n =
unions $ map (\e -> go (map (insert e) acc) (delete e s) (n - 1)) s
8 changes: 8 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ main = do
s2 = S.fromFoldable [2,4,6,8,10]
s3 = S.fromFoldable [2,4]
assert $ S.intersection s1 s2 == s3

log "choose"
do let set = S.fromFoldable [0, 1, 2]
assert $ S.choose set 2 == S.fromFoldable `S.map` S.fromFoldable [
[0, 1]
, [1, 2]
, [0, 2]
]