Search code examples
haskellfilterdistinct-valuesconduit

Haskell conduit filter distinct values


If I use the following Source:

 sourceList [1,3,3,1,2,3]

Is it possible to apply some filter or combinator to only allow distinct values to be passed downstream?

So in my example, only [1,3,2] would be passed downstream?


Solution

  • Something like this should do:

    #!/usr/bin/env stack
    -- stack --resolver lts-6.19 runghc --package conduit-combinators
    import Conduit
    import Data.Conduit.List (sourceList)
    
    main = do
        print $ runConduitPure $ sourceList [1,3,3,1,2,3] .| myConduit [] .| sinkList
    
    myConduit dup = do
      num <- await
      case num of
        Just x -> if x `elem` dup
                  then myConduit dup
                  else do
                    yield x
                    myConduit (x:dup)
        Nothing -> return ()
    

    On execution:

    sibi::casey { ~/scripts }-> ./cond.hs
    [1,3,2]