Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell style to-infix operator in F#
    text
    copied!<p>There is a common problem that F# <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2313479-provide-the-option-of-having-infix-notation-on-fun" rel="nofollow noreferrer">does not natively support</a> infix-style use of functions that is available in Haskell:</p> <pre><code>isInfixOf :: Eq a =&gt; [a] -&gt; [a] -&gt; Bool isInfixOf "bar" "foobarbaz" "bar" `isInfixOf` "foobarbaz" </code></pre> <p>The best known solution for F# can be found <a href="https://stackoverflow.com/a/3765532/974789">here</a>:</p> <pre><code>let isInfixOf (what:string) (where:string) = where.IndexOf(what, StringComparison.OrdinalIgnoreCase) &gt;= 0 let found = "bar" |&gt;isInfixOf&lt;| "foobarbaz" </code></pre> <p>Also, it is easy to improve it a bit, employing native operators precedence:</p> <pre><code>let ($) = (|&gt;) let (&amp;) = (&lt;|) let found = "bar" $isInfixOf&amp; "foobarbaz" </code></pre> <p>There's also XML-ish <code>&lt;/style/&gt;</code>, described <a href="http://cs.hubfs.net/topic/None/58599" rel="nofollow noreferrer">here</a>.</p> <p><strong>I would like to find a better solution, with the following criteria:</strong></p> <ul> <li>Single character operator (or a pair) that does not destroy commonly used operators;</li> <li>It should be the same character, likewise grave accent (back quote) character serves in Haskell;</li> <li><p>It should not destroy associativity (support chaining):</p> <pre><code>let found = "barZZZ" |&gt;truncateAt&lt;| 3 |&gt;isInfixOf&lt;| "foobarbaz" </code></pre></li> <li><p>Optionally, it should support functions taking tuples:</p> <pre><code>let isInfixOf (what:string, where:string) = ... // it will not work with |&gt; and &lt;| </code></pre></li> <li><p>Optionally, it should gracefully handle functions/3:</p> <pre><code>val f: 'a -&gt; 'b -&gt; 'c -&gt; 'd = ... let curried = a |&gt;f&lt;| b c // this wouldn't compile as the compiler would attempt to apply b(c) first </code></pre></li> </ul> <p>P.S. Various coding tricks are also welcome as I believe the good one (when checked by the F# Dev team) can be a part of the language in the future.</p>
 

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