Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Lists are always homogeneous, so you can't put items with a different <code>a</code> in to the same list (as you noticed). There are several ways to design around this, but I usually prefer using <a href="http://en.wikibooks.org/wiki/Haskell/GADT" rel="nofollow">GADTs</a>. For example:</p> <pre><code>{-# LANGUAGE GADTs #-} import Data.ByteString (ByteString) import Data.Word data DuplicateReason = Size | Checksum | Content data DuplicateGroup data DuplicateTest where DuplicateTest :: Eq a =&gt; (FilePath -&gt; IO a) -&gt; DuplicateReason -&gt; DuplicateTest getSize :: FilePath -&gt; IO Integer getSize = undefined getChecksum :: FilePath -&gt; IO Word32 getChecksum = undefined getContent :: FilePath -&gt; IO ByteString getContent = undefined getDuplicatesByMethods :: [DuplicateTest] -&gt; IO [DuplicateGroup] getDuplicatesByMethods = undefined </code></pre> <p>This solution still needs a new type, but at least you don't have to specify all cases in advance or create boilerplate type-classes. Now, since the generic type <code>a</code> is essentially "hidden" inside the GADT, you can define a list that contains functions with different return types, wrapped in the <code>DuplicateTest</code> GADT.</p> <pre><code>getDuplicatesByMethods [ DuplicateTest getSize Size , DuplicateTest getChecksum Checksum , DuplicateTest getContent Content ] </code></pre> <p>You can also solve this without using any language extensions or introducing new types by simply re-thinking your functions. The main intention is to group files according to some property <code>a</code>, so we could define <code>getDuplicatesByMethods</code> as</p> <pre><code>getDuplicatesByMethods :: [([FilePath] -&gt; IO [[FilePath]], DuplicateReason)] -&gt; IO [DuplicateGroup] </code></pre> <p>I.e. we take in a function that groups files according to some criteria. Then we can define a helper function</p> <pre><code>groupWith :: Eq a =&gt; (FilePath -&gt; IO a) -&gt; [FilePath] -&gt; IO [[FilePath]] </code></pre> <p>and call <code>getDuplicatesByMethods</code> like this</p> <pre><code>getDuplicatesByMethods [ (groupWith getSize, Size) , (groupWith getChecksum, Checksum) , (groupWith getContent, Content) ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload