Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's get a function out of the <code>replaceAll</code> method:</p> <pre><code>scala&gt; val replace = (from: String, to: String) =&gt; (_:String).replaceAll(from, to) replace: (String, String) =&gt; String =&gt; java.lang.String = &lt;function2&gt; </code></pre> <p>Now you can use <code>Functor</code> instance for function, <a href="https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Functor.scala#L88">defined</a> in scalaz. That way you can compose functions, using <code>map</code> (or to make it look better, using unicode aliases).</p> <p>It will look like this:</p> <pre><code>scala&gt; replace("from", "to") ∘ replace("to", "from") ∘ replace("some", "none") res0: String =&gt; java.lang.String = &lt;function1&gt; </code></pre> <p>If you prefer haskell-way compose (right to left), use <code>contramap</code>:</p> <pre><code>scala&gt; replace("some", "none") ∙ replace("to", "from") ∙ replace ("from", "to") res2: String =&gt; java.lang.String = &lt;function1&gt; </code></pre> <p>You can also have some fun with <code>Category</code> <a href="https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Category.scala#L97">instance</a>:</p> <pre><code>scala&gt; replace("from", "to") ⋙ replace("to", "from") ⋙ replace("some", "none") res5: String =&gt; java.lang.String = &lt;function1&gt; scala&gt; replace("some", "none") ⋘ replace("to", "from") ⋘ replace ("from", "to") res7: String =&gt; java.lang.String = &lt;function1&gt; </code></pre> <p>And applying it:</p> <pre><code>scala&gt; "somestringfromto" |&gt; res0 res3: java.lang.String = nonestringfromfrom scala&gt; res2("somestringfromto") res4: java.lang.String = nonestringfromfrom scala&gt; "somestringfromto" |&gt; res5 res6: java.lang.String = nonestringfromfrom scala&gt; res7("somestringfromto") res8: java.lang.String = nonestringfromfrom </code></pre>
    singulars
    1. This table or related slice is empty.
    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