title: Sequencing monadic actions where every action takes the original input
body: <p>I'm currently experimenting with the <a href="https://github.com/ghcjs/jsaddle" rel="nofollow noreferrer">JSaddle</a> library in ghcjs.</p>
  
  <p>I don't have much of any experience with lenses yet. Somewhere in this library they seem to define a getter lens which is actually a sort of monadic setter:</p>
  
  <p><a href="https://github.com/ghcjs/jsaddle/blob/3f8b32833917f1a2dfbdb81ef00992fb54733c9a/jsaddle/src/Language/Javascript/JSaddle/Object.hs#L201" rel="nofollow noreferrer">https://github.com/ghcjs/jsaddle/blob/3f8b32833917f1a2dfbdb81ef00992fb54733c9a/jsaddle/src/Language/Javascript/JSaddle/Object.hs#L201</a></p>
  
  <pre><code>-- | Makes a setter for a particular property name.
  --
  -- &gt; jss name = to (&lt;#name)
  --
  -- &gt;&gt;&gt; testJSaddle $ eval "'Hello World'.length = 12"
  -- 12
  -- &gt;&gt;&gt; testJSaddle $ val "Hello World" ^. jss "length" 12
  -- undefined
  jss :: (ToJSString name, ToJSVal val)
     =&gt; name          -- ^ Name of the property to find
     -&gt; val
     -&gt; forall o . MakeObject o =&gt; IndexPreservingGetter o (JSM ())
  jss name val = to (\o -&gt; o &lt;# name $ val)
  </code></pre>
  
  <p>Using this in the following manner to set properties on a new javascript object works:</p>
  
  <pre><code>makeConfig :: JSM Object
  makeConfig = do
      object &lt;- obj
      object ^. jss "foo" 5
      object ^. jss "bar" 7
      return object
  </code></pre>
  
  <p>But I'd like to drop the cruft and have something like this:</p>
  
  <pre><code>makeConfig :: JSM Object
  makeConfig = obj ^. jss "foo" 5
                   ^. jss "bar" 7
  </code></pre>
  
  <p>I know you can do this in the <code>lens</code> library like this because the setters actually return the updated object:</p>
  
  <pre><code>obj &amp; _1 .~ 45
      &amp; _2 .~ 49
  </code></pre>
  
  <p>Right now I made a small shim function that returns the input value every time:</p>
  
  <pre><code>(~.) :: MakeObject o =&gt; o -&gt; IndexPreservingGetter o (JS.JSM ()) -&gt; JS.JSM o
  a ~. b = a ^. b &gt;&gt; return a
  </code></pre>
  
  <p>This allows me to use <code>jss</code> the way I'd like to:</p>
  
  <pre><code>makeConfig :: JSM Object
  makeConfig = obj ~. jss "foo" 5
                   ~. jss "bar" 7
  </code></pre>
  
  <p>But I can't help but feel that I'm trying to fix a solved problem. Is there a better way I don't know about?</p>
  
  <p>[EDIT]</p>
  
  <p>I did try something like this:</p>
  
  <pre><code>props :: MakeObject o =&gt; o -&gt; [IndexPreservingGetter o (JS.JSM ())] -&gt; JS.JSM o
  props object p = mapM_ (object ^.) p &gt;&gt; return object
  </code></pre>
  
  <p>But then GHCJS complains about impredicative polymorphism...</p>