Skip to content

Instantly share code, notes, and snippets.

View bst-insert.ps
/bst-insert { % T x
exch dup % x T T
null eq % x T ?
{ % x T (T is null)
pop pop %
}
{ % x T
exch % T x
1 index bst-get-value % T x v
2 copy eq % T x v x=v?
View mockmock.cs
var mockmock = new Mock<Mock<object>>();
mockmock.Setup(mock =>
mock.Setup(it => it.ToString()).Returns("object"))
.Returns(new Mock<IReturnsResult<object>>().Object);
var mock = mockmock.Object;
mock.Setup(o => o.ToString()).Returns("axolotl");
Assert.Equal("axolotl", mock.Object.ToString());
@einarwh
einarwh / tf.ps
Last active Sep 17, 2021
Term frequency in PostScript (Exercises in Programming Style by Cristina Videira Lopes)
View tf.ps
% Read file input using a buffer of the specified size.
% Stack: Z fn -> A
% Z: Buffer size
% fn: File name
% A: Array of lines of input
/read-input-bufsize { % Z fn
[ % Z fn [
3 1 roll % [ Z fn
(r) file % [ Z F
{
@einarwh
einarwh / NullableExt.cs
Last active Jul 7, 2021
Map and bind over nullables
View NullableExt.cs
public static class NullableExt {
public static TR? MapR2R<T, TR>(this T? nullable, Func<T, TR> mapper)
where T : class where TR : class
{
return nullable == null ? null : mapper(nullable);
}
public static TR? MapR2V<T, TR>(this T? nullable, Func<T, TR> mapper)
where T : class where TR : struct
@einarwh
einarwh / GetFaultConfig.cs
Last active Aug 23, 2021
GetFaultConfig.
View GetFaultConfig.cs
private IFaultConfig GetFaultConfig()
{
if (_unleash.IsEnabled("ps-playback-api-channel-blob-storage-inject-fault-always"))
{
return new EnabledFault { InjectionRate = 1.00 };
}
else if (_unleash.IsEnabled("ps-playback-api-channel-blob-storage-inject-fault-terrible-rate"))
{
return new EnabledFault { InjectionRate = 0.90 };
}
View GetPolicy.cs
public IAsyncPolicy GetPolicy()
{
var faultConfig = GetFaultConfig();
var faultMonkey = MonkeyPolicy.InjectExceptionAsync(with =>
with.Fault(new ThreeHeadedMonkeyStorageException())
.InjectionRate(faultConfig.InjectionRate)
.Enabled(faultConfig.Enabled));
var latencyConfig = GetLatencyConfig();
var latencyMonkey = MonkeyPolicy.InjectLatencyAsync(with =>
View result.ps
%%% Result: [v T]
%%% Error: [e F]
/ok { % v
[ exch true ] % [v T]
} def
/error { % e
[ exch false ] % [e F]
} def
View Result.ex
defmodule Result do
def ok(value) do
{:ok, value }
end
def error(err) do
{:error, err}
end
View option.ps
%%% Some: [v]
%%% None: []
/some { % v
[ exch ] % [v]
} def
/none {
[]
} def
View Option.ex
defmodule Option do
def some(value) do
{:some, value }
end
def none() do
nil
end