Skip to content

Commit

Permalink
Prepare article for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
ploeh committed Jan 22, 2024
1 parent fd508c7 commit d5d8c09
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion _posts/2024-01-01-variations-of-the-range-kata.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ul>
<li><a href="/2024/01/08/a-range-kata-implementation-in-haskell">A Range kata implementation in Haskell</a></li>
<li><a href="/2024/01/15/a-range-kata-implementation-in-f">A Range kata implementation in F#</a></li>
<li>A Range kata implementation in C#</li>
<li><a href="/2024/01/22/a-range-kata-implementation-in-c">A Range kata implementation in C#</a></li>
<li>Range as a functor</li>
</ul>
<p>
Expand Down
2 changes: 1 addition & 1 deletion _posts/2024-01-15-a-range-kata-implementation-in-f.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ <h3 id="c00252811495433987c37f7bcfc751a5">
I didn't succeed doing that with the Range kata this time around, but maybe later.
</p>
<p>
<strong>Next:</strong> A Range kata implementation in C#.
<strong>Next:</strong> <a href="/2024/01/22/a-range-kata-implementation-in-c">A Range kata implementation in C#</a>.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: "A Range kata implementation in C#"
description: "A port of the corresponding F# code."
date: 2024-01-03 6:37 UTC
date: 2024-01-22 7:05 UTC
tags: [Functional Programming]
---
{% include JB/setup %}
Expand All @@ -12,22 +12,22 @@
<em>{{ page.description }}</em>
</p>
<p>
This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. In the <a href="">previous article</a> I made a pass at <a href="https://codingdojo.org/kata/Range/">the kata</a> in <a href="https://fsharp.org/">F#</a>, using property-based testing with <a href="https://hedgehog.qa/">Hedgehog</a> to generate test data.
This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. In the <a href="/2024/01/15/a-range-kata-implementation-in-f">previous article</a> I made a pass at <a href="https://codingdojo.org/kata/Range/">the kata</a> in <a href="https://fsharp.org/">F#</a>, using property-based testing with <a href="https://hedgehog.qa/">Hedgehog</a> to generate test data.
</p>
<p>
In the conclusion I mused about the properties I was able to come up with. Is it possible to describe open, closed, and mixed ranges in a way that's less coupled to the implementation? To be honest, I still don't have an answer to that question. Instead, in this article, I describe a straight port of the F# code to C#. There's value in that, too, for people who wonder <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">how to reap the benefits of F# in C#</a>.
</p>
<p>
The code is <a href="">available on GitHub</a>.
The code is <a href="https://github.com/ploeh/RangeCSharp">available on GitHub</a>.
</p>
<h3 id="2b05848a3b494ec99cc0e50da22bdd15">
First property <a href="#2b05848a3b494ec99cc0e50da22bdd15">#</a>
</h3>
<p>
Both F# and C# are .NET languages. They run in the same substrate, and are interoperable. Hedgehog is written in F#, but it's possible to consume F# libraries from C#, and vice versa. I've done this multiple times with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>, but I admit to never having tried it with Hedgehog.
Both F# and C# are .NET languages. They run in the same substrate, and are interoperable. While Hedgehog is written in F#, it's possible to consume F# libraries from C#, and vice versa. I've done this multiple times with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>, but I admit to never having tried it with Hedgehog.
</p>
<p>
If you want to try property-based testing in C#, a third alternative is available: <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a>. It's written in C# and is more <a href="https://blog.ploeh.dk/2015/08/03/idiomatic-or-idiosyncratic/">idiomatic</a> in that context. While I sometimes <a href="/2021/02/15/when-properties-are-easier-than-examples">still use FsCheck from C#</a>, I often choose CsCheck for didactic reasons.
If you want to try property-based testing in C#, a third alternative is available: <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a>. It's written in C# and is more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> in that context. While I sometimes <a href="/2021/02/15/when-properties-are-easier-than-examples">still use FsCheck from C#</a>, I often choose CsCheck for didactic reasons.
</p>
<p>
The first property I wrote was a direct port of the idea of the first property I wrote in F#:
Expand Down Expand Up @@ -147,7 +147,7 @@ <h3 id="484d1f121cc44050b76f23d92bca429c">
<pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Range</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="color:blue;">where</span>&nbsp;T&nbsp;:&nbsp;IComparable&lt;T&gt;</pre>
</p>
<p>
It made sense to me to constrain the <code>T</code> type argument to <code>IComparable&lt;T&gt;</code>, although it's possible that I could have deferred that constraint to the actual <code>Contains</code> method, like I did with <a href="">my Haskell implementation</a>.
It made sense to me to constrain the <code>T</code> type argument to <code>IComparable&lt;T&gt;</code>, although it's possible that I could have deferred that constraint to the actual <code>Contains</code> method, like I did with <a href="/2024/01/08/a-range-kata-implementation-in-haskell">my Haskell implementation</a>.
</p>
<p>
A <code>Range</code> holds two <code>Endpoint</code> values:
Expand Down

0 comments on commit d5d8c09

Please sign in to comment.