Skip to content

Instantly share code, notes, and snippets.

View CoditCompany's full-sized avatar
👋
You can find us on @CoditEU

Codit CoditCompany

👋
You can find us on @CoditEU
View GitHub Profile
/// <summary>
/// Extensions to support Functional Concepts
/// </summary>
public static class FunctionalConcepts
{
/// <summary>
/// Executes a given dead-end function <paramref name="f"/> on each element of the given list of <paramref name="items"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">The items.</param>
var maybeThis = Maybe<This>.Just(new This());
maybeThis.Bind(@this => @this.GetMaybeThat())
.Bind(that => that.GetMaybeAnother())
.Bind(another => Maybe<AnotherThing>.Just(another));
if (maybeThis.IsPresent)
{
Maybe<That> maybeThat = maybeThis.Value.GetMaybeThat();
if (maybeThat.IsPresent)
{
Maybe<AnotherThing> maybeAnother = maybeThat.Value.GetMaybeAnother();
if (maybeAnother.IsPresent)
{
...
}
/// <summary>
/// Type to indicate that there might be a value missing.
/// </summary>
/// <typeparam name="TA">The type of a.</typeparam>
public class Maybe<TA>
{
private TA _value;
private bool IsPresent => _value != null;
@CoditCompany
CoditCompany / PipeToCounterExample2.cs
Created August 24, 2017 07:54
PipeTo Counter Example 2
[Property]
public Property Response_With_404_Not_Found_If_Request_Contains_UnknownId(
string unknownId,
string existingId)
{
return (HttpStatusCode.NotFound == ExerciseSendMessageWithKnownId(
new Message(unknownId).WithMethod(HttpMethod.Post)).StatusCode).When(existingId != unknownId);
}
@CoditCompany
CoditCompany / PipeToCounterExample.cs
Created August 24, 2017 07:53
PipeTo Counter Example
[Property]
public Property Response_With_404_Not_Found_If_Request_Contains_UnknownId(
string unknownId,
string existingId)
{
// Arrange
Message fixture = new Message(unknownId).WithMethod(HttpMethod.Post);
// Act
Response response = ExerciseSendMessageWithKnownId(existingETag);
@CoditCompany
CoditCompany / PipeToExample.cs
Created August 24, 2017 07:51
PipeTo Example
[Property]
public Property Response_With_404_Not_Found_If_Request_Contains_UnknownId(
string unknownId,
string existingId)
{
// Arrange
return new Message(unknownId)
.WithMethod(HttpMethod.Post)
// Act
@CoditCompany
CoditCompany / PipeTo.cs
Created August 24, 2017 07:50
PipeTo extension
/// <summary>
/// Extensions to support Functional Concepts
/// </summary>
public static class FunctionalConcepts
{
/// <summary>
/// Pipes a given <paramref name="value"/> to given function <paramref name="f"/>.
/// </summary>
/// <typeparam name="TA">The type of a.</typeparam>
/// <typeparam name="TB">The type of the b.</typeparam>
let isValidIsbn (s : string) =
let numbers =
s.ToCharArray()
|> Array.map (string >> int)
|> List.ofArray
numbers
|> List.take 12
|> List.zip [ 1; 3; 1; 3; 1; 3; 1; 3; 1; 3; 1; 3; ]
|> List.fold (fun result (x, y) -> result + (x * y)) 0
|> fun x -> x % 10
[<Property>]
let ``String50 don't gets created if length isn't 50 chars`` () =
Arb.generate<string>
|> Gen.filter (String.length >> (<>) 50)
|> Arb.fromGen
|> Prop.forAll <| fun x ->
None = string50 x
[<Property>]
let ``String50 gets created if length is 50 chars`` () =