Skip to content

Instantly share code, notes, and snippets.

@stijnmoreels
stijnmoreels / TaskExtensions.cs
Last active February 14, 2018 12:20
Task Extensions with Monad Laws
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Threading.Tasks
{
public class Workspace
{
@stijnmoreels
stijnmoreels / FunctionalExtensions.cs
Last active February 14, 2018 10:45
Functional Extensions
namespace System.Functional
{
public static class FunctionalExtensions
{
public static Func<TA, TA> Id<TA>() => x => x;
public static Func<TA, TB> Const<TA, TB>(this TB x) => _ => x;
public static Func<TA, TB> ToFunc<TA, TB>(Func<TA, TB> f) => f;
@stijnmoreels
stijnmoreels / Maybe.cs
Created February 14, 2018 13:51
Maybe class
namespace System.Monads
{
public class Maybe<TA>
{
private readonly TA _value;
private bool IsPresent => _value != null;
private Maybe() { _value = default(TA); }
private Maybe(TA value) { _value = value; }
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace System.IO
{
public sealed class VirtualStream : Stream, ICloneable
{
/// <summary>
namespace System.IO
open System
open System.IO
type MemoryFlag =
| AutoOverflowToDisk
| OnlyInMemory
| OnlyToDisk
private static Outcome<JObject, string[]> Load(Stream content)
{
if (content is null)
{
throw new ArgumentNullException(nameof(content));
}
var settings = new JsonLoadSettings
{
CommentHandling = CommentHandling.Ignore,
public class BookJson
{
[JsonConstructor]
public BookJson(string author, string isbn13, int pages)
{
Author = Sanitize.RegexReplace(pattern: "( ){2,}", replacement: String.Empty, input: author).WhiteMatch(@"[a-zA-Z\. ]+");
ISBN13 = Sanitize.RemoveWhitespace(isbn13).BlackList(@"\-");
Pages = pages;
}
var schema = JSchema.Parse(
@"{ 'description': 'A book',
'type': 'object',
'properties':
{ 'author': { 'type': 'string', 'minLength': 1, 'maxLength': 100 },
'isbn13': { 'type': 'string', 'minLength': 13, 'maxLength': 17 },
'pages': { 'type': 'number', 'minimum': 1, 'maximum': 1000 } } }");
schema.AllowAdditionalProperties = false;
public class Author
{
private readonly string _name;
private Author(string name) { _name = name; }
public static ValidationResult<Author> Create(string author)
{
const string name = @"[A-Z]{1}[a-z]*\.?";
string pattern = $@"^({name})( {name})*$";
return Spec.Of<string>()
type Author =
private Author of string with
static member create x =
let name = @"[A-Z]{1}[a-z]*\.?"
let pattern = sprintf "^(%s)( %s)*$" name name
specModel Author x {
notNullOrWhiteSpace "author name should not be blank"
greaterThanOrEqualOf String.length 1 "author name should at least be a single character"
matches pattern "author name should only contain names starting with a capital" }