Skip to content

Instantly share code, notes, and snippets.

@KWMalik
Forked from supki/SHITLOAD
Created November 21, 2012 19:16
Show Gist options
  • Save KWMalik/4127004 to your computer and use it in GitHub Desktop.
Save KWMalik/4127004 to your computer and use it in GitHub Desktop.
How to create and use filters via libstackexchange
# First, try to get question body with default filter
μ> (fmap head . askSE $ questionsOnUsers [972985] <> site "stackoverflow" ) >>= (^! field "body") :: IO String
*** Exception: user error (key "body" not present)
# Okay, create appropriate filter for this task and get its name
μ> (askSE $ createFilter ["question.body"] [] "default") >>= (^! field "items" . fields "filter") :: IO [String]
["!9hnGssGO4"]
# See
μ> (fmap head . askSE $ questionsOnUsers [972985] <> site "stackoverflow" <> filter "!9hnGssGO4") >>= (^! field "body") :: IO String
"<p>I have a data type that is an instance of <code>Monoid</code> so I can get nice values composition:</p>\n\n<pre><code>data R a = R String (Maybe (String \8594 a))\n\ninstance Monoid (R a) where\n mempty = R \"\" Nothing\n R s f `mappend` R t g = R (s ++ t) (case g of Just _ \8594 g; Nothing \8594 f)\n</code></pre>\n\n<p>Next, I don't want to combine all <code>R a</code> values with one another, it doesn't make sense in my domain. So I introduce phantom type <code>t</code>:</p>\n\n<pre><code>{-# LANGUAGE DataKinds, KindSignatures #-}\n\ndata K = T1 | T2\ndata R (t \8759 K) a = R String (Maybe (String \8594 a))\n\ninstance Monoid (R t a) where \8230\n</code></pre>\n\n<p>So I have \"restricted\" values:</p>\n\n<pre><code>v1 \8759 R T1 Int\nv2 \8759 R T2 Int\n-- v1 &lt;&gt; v2 =&gt; type error\n</code></pre>\n\n<p>and \"unrestricted\":</p>\n\n<pre><code>v \8759 R t Int\n-- v &lt;&gt; v1 =&gt; ok\n-- v &lt;&gt; v2 =&gt; ok\n</code></pre>\n\n<p>But now I have a problem. When it comes to <code>v30</code>, for example:</p>\n\n<ul>\n<li>I would have huge <strike>data</strike>kind declaration (<code>data K = T1 | \8230 | T30</code>). I could solve by using type level naturals to get infinite source of phantom types (the cure is worse than the disease, isn't it?)</li>\n<li>I should remember which phantom type for what value to use when writing type signatures in dependent code (that is <em>really</em> annoying)</li>\n</ul>\n\n<p>Is there an easier approach to restrict composition somehow?</p>\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment