title: Haskell cassava (Data.Csv): Ignore missing columns/fields
body: <p>How can I set up cassava to ignore missing columns/fields and fill the respective data type with a default value? Consider this example:</p>
  <pre><code>{-# LANGUAGE DeriveGeneric #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  import           Data.ByteString.Lazy.Char8
  import           Data.Csv
  import           Data.Vector
  import           GHC.Generics
  
  data Foo = Foo { 
        a :: String
      , b :: Int
      } deriving (Eq, Show, Generic)
      
  instance FromNamedRecord Foo
  
  decodeAndPrint :: ByteString -&gt; IO ()
  decodeAndPrint csv = do
      print $ (decodeByName csv :: Either String (Header, Vector Foo))
  
  main :: IO ()
  main = do
      decodeAndPrint &quot;a,b,ignore\nhu,1,pu&quot;  -- [1]
      decodeAndPrint &quot;ignore,b,a\npu,1,hu&quot;  -- [2]
      decodeAndPrint &quot;ignore,b\npu,1&quot;     -- [3]
  </code></pre>
  <p><code>[1]</code> and <code>[2]</code> work perfectly fine, but <code>[3]</code> fails with</p>
  <pre><code>Left &quot;parse error (Failed reading: conversion error: no field named \&quot;a\&quot;) at \&quot;\&quot;&quot;
  </code></pre>
  <p>How could I make <code>decodeAndPrint</code> capable of handling this incomplete input?</p>
  <p>I could of course manipulate the input bytestring, but maybe there is a more elegant solution.</p>
  <hr />
  <p>A <strong>Solution</strong> thanks to the input of Daniel Wagner below:</p>
  <pre><code>{-# LANGUAGE DeriveGeneric #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  import           Control.Applicative
  import           Data.ByteString.Lazy.Char8
  import           Data.Csv
  import           Data.Vector
  import           GHC.Generics
  
  data Foo = Foo { 
        a :: Maybe String
      , b :: Maybe Int
      } deriving (Eq, Show, Generic)
      
  instance FromNamedRecord Foo where
      parseNamedRecord rec = pure Foo
          &lt;*&gt; ((Just &lt;$&gt; Data.Csv.lookup rec &quot;a&quot;) &lt;|&gt; pure Nothing)
          &lt;*&gt; ((Just &lt;$&gt; Data.Csv.lookup rec &quot;b&quot;) &lt;|&gt; pure Nothing)
  
  decodeAndPrint :: ByteString -&gt; IO ()
  decodeAndPrint csv = do
      print $ (decodeByName csv :: Either String (Header, Vector Foo))
  
  main :: IO ()
  main = do
      decodeAndPrint &quot;a,b,ignore\nhu,1,pu&quot;  -- [1]
      decodeAndPrint &quot;ignore,b,a\npu,1,hu&quot;  -- [2]
      decodeAndPrint &quot;ignore,b\npu,1&quot;       -- [3]
  </code></pre>