Skip to content

Instantly share code, notes, and snippets.

View jbrestan's full-sized avatar

Honza Brestan jbrestan

  • Czech Republic
  • 22:22 (UTC +01:00)
  • X @jbrestan
View GitHub Profile
@jbrestan
jbrestan / PureDI.cs
Last active January 20, 2020 10:44 — forked from davidfowl/PureDI.cs
Modified from: DI under the hood. This is what DI containers automate for you.
using System;
using System.Threading;
namespace PureDI
{
class Program
{
static void Main(string[] args)
{
// Create the singletons once
@jbrestan
jbrestan / LICENSE
Created January 10, 2018 13:06
This license applies to all public gists at https://gist.github.com/jbrestan
MIT License
Copyright (c) 2014 Jan Brestan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@jbrestan
jbrestan / filesToExcludeFromTestCoverage.fs
Created January 10, 2018 12:47
Finds all Paket DLL dependencies and builds a CLI exclusion filter for dotTrace
let filesToExcludeFromTestCoverage rootDir projectExcludes =
rootDir</>"paket.lock"
|> File.ReadLines
|> Seq.choose (fun line ->
// Expecting paket.lock line format for dependencies and transitive dependencies, e.g.:
// xunit (2.3)
// xunit.analyzers (>= 0.7)
// We'll use the version to recognize it from `remote: <url>` definitions, but take just the package name.
match Regex.Match(line,"^[ ]{4,6}([^ ]+) \((.+)\)") with
| m when m.Success && m.Groups.Count = 3 -> Some m.Groups.[1].Value
@jbrestan
jbrestan / AsyncGotcha.fs
Last active June 29, 2017 18:16
Snippet showing how `Async<'t>` always gets fully executed, so there's nothing like "completed Async instance". Took me a while to understand why there are no `Async.IsCompleted` or `Async.Result` properties like on Task
let rec retryForever (delay: TimeSpan) (workflow: Async<Result<_, _>>) = async {
(*
this always fully executes the `workflow` again,
no result caching like Task's Result property
*)
let! result = workflow
match result with
| Ok r ->
return Ok r
| Error _ ->
@jbrestan
jbrestan / fizzbuzz.hs
Last active December 14, 2016 21:05
Interview buster
fizzbuzz n = last $ zipWith fb fbs [1..n]
where
fbs = zipWith (++) (cycle ["","","fizz"]) (cycle ["","","","","buzz"])
fb s m = if s == "" then (show m) else s
map fizzbuzz [1..100]
type Result<'result, 'message> =
| Success of 'result
| Failure of 'message list
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Result =
let inline succeed v =
Success v
@jbrestan
jbrestan / ExponentialBackoff.fs
Last active January 10, 2018 13:07
Thoroughly shaven yak
module ExponentialBackoff =
open System
[<Measure>] type ms
type Backoff =
| Immediate
| Initial
| Double of previousDelay: int<ms>
@jbrestan
jbrestan / Reducees.fsx
Created January 7, 2016 13:12
General reducees F# implementation, based on this http://blog.plataformatec.com.br/2015/05/introducing-reducees/
module Reducees =
type Instruction = Cont | Done | Halt
let rec reduce source instruction func =
match source, instruction with
| head::tail, (Cont, acc) -> reduce tail (func head acc) func
| [], (Cont, acc) -> Done, acc
| _ -> instruction
public IDictionary<string, TValue> Dict<TValue>(
params Func<dynamic, TValue>[] pairs)
{
return pairs.ToDictionary(pair => pair.Method.GetParameters()[0].Name,
pair => pair(null));
}
@jbrestan
jbrestan / AutoLogger.cs
Last active August 29, 2015 14:26
Dynamic logger experiment. Allows writing descriptive log calls without the need to implement them by hand.
public class AutoLogger : DynamicObject
{
private readonly Action<string> _logMethod;
const string SplitterName = "splitNameBy";
public AutoLogger(Action<string> logMethod)
{
_logMethod = logMethod;
}