Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / sample.fs
Created February 23, 2014 10:55
Private constructor in sum type crashes `sprintf "%A"`?
module MyModule
open Xunit
open FsUnit.Xunit
type Sample = Sample of int
with override x.ToString() = sprintf "%A" x
[<Fact>]
let ``sample to string`` ()=
let actual = Sample 1
@dtchepak
dtchepak / 00_background.cs
Last active August 29, 2015 13:56
Injection samples
/* Dependency */
public class AnotherClass {
public virtual string DoSomething(string str1) { return str1.ToUpper(); } /* virtual so we can change/mock it.
* Alternatively use an interface, or abstract. */
}
/* Class we want to test */
public class MyClass {
public string str1;
@dtchepak
dtchepak / Sample.fsproj
Created February 7, 2014 06:28
Microsoft.FSharp.Targets in fsproj
<Choose>
<When Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets')">
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<When Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
//Extensions.cs
public static IObservable<T> ObservableFunc<T>(this Func<T> f) {
return Observable.Create<T>(obs => {
try {
obs.OnNext(f());
obs.OnCompleted();
} catch (Exception e) {
obs.OnError(e);
}
return () => {};
@dtchepak
dtchepak / rx.cs
Last active January 4, 2016 07:19
Rather than explicitly unsubscribing observers, is building completion into all observables a good idea? (For observables that don't naturally complete; streams of mouse moves, driver events etc)
var done = new Subject<Unit>();
var status = Observable
.Interval(TimeSpan.FromMilliseconds(450))
.Select(_ => driver.Status()) // driver keep-alive
.TakeUntil(done);
// On dispose, window close, etc...
done.CompletedWith(Unit.Value);
@dtchepak
dtchepak / DoQueue.cs
Last active January 4, 2016 04:19
Example of using a queue to run multiple actions with NSubstitute `When..Do`. [Disclaimer: for illustrative purposes only. Use at your own risk!]
using System;
using System.Collections.Generic;
using System.Linq;
using NSubstitute;
using NSubstitute.Core;
using NUnit.Framework;
using Shouldly;
//Disclaimer: for illustrative purposes only. Use at your own risk! :)
namespace Sample
public class KVStoreExamples
{
[Fact]
public void GetValueForExistingKey()
{
var kv =
from a in KVStore.add("a", 1)
from b in KVStore.add("b", 2)
from c in KVStore.add("c", 3)
select c
@dtchepak
dtchepak / ViewModel.cs
Created December 5, 2013 22:54
`_service.Devices` is hooked up to a `ConnectedDevicesChanged` event, and updates with a full list of connected devices. Need to display all connected devices on the UI. (ideally not completely clearing collection, just adding/removing newly connected/disconnected devices, but i'm not doing that at the moment)
public class MainViewModel : Screen
{
private ConnectionService _service;
public ObservableCollection<DeviceViewModel> Devices { get; private set; }
public MainViewModel()
{
Devices = new ObservableCollection<DeviceViewModel>();
}
public class ObservableItems<T> : INotifyPropertyChanged
{
private IEnumerable<T> _items;
public ObservableItems(IObservable<IEnumerable<T>> source, IEnumerable<T> initialValue)
{
Items = initialValue;
source
.ObserveOnDispatcher()
.Subscribe(x => Items = x);
void Main()
{
var x = new Thing();
Action<string> onFoo = s => Console.WriteLine (s);
x.OnFoo += onFoo.Invoke;
x.RaiseEvent("a");
x.RaiseEvent("b");
x.OnFoo -= onFoo.Invoke;
x.RaiseEvent("c");
}