Skip to content

Instantly share code, notes, and snippets.

View alieniasty's full-sized avatar

Adrian Jagodzinski alieniasty

  • Poland
View GitHub Profile
@alieniasty
alieniasty / retryPolicy.cs
Last active February 17, 2021 10:43
Polly retry with Circuit Breaker
_retryPolicy = Policy
.Handle<CosmosException>()
.WaitAndRetryAsync(2, i =>
{
var timeToWait = TimeSpan.FromSeconds(1);
return timeToWait;
});
_concurrencyExceptionPolicy = Policy
.Handle<CosmosException>()
@alieniasty
alieniasty / OptVsRes.txt
Created January 29, 2020 19:31
Option vs Result
I think it's quite easy.. Option is for functions that may or may not return a value (say, a string search may find something or may not find something). So, both return values are valid and expected in your program.
Result, as is is shown by the Err enum, is for functions that are expected to succeed (thus, Ok()), but may fail and this has to be catched. Example: Opening a connection to some other server would generally be expected to work but of course can fail.

Introduction

Roslyn provides a rich set of APIs for analyzing C# and Visual Basic source code, but constructing a context in which to perform analysis can be challenging. For simple tasks, creating a Compilation populated with SyntaxTrees, MetadataReferences and a handful of options may suffice. However, if there are multiple projects involved in the analysis, it is more complicated because multiple Compilations need to be created with references between them.

To simplify the construction process. Roslyn provides the Workspace API, which can be used to model solutions, projects and documents. The Workspace API performs all of the heavy lifting needed to parse SyntaxTrees from source code, load MetadataReferences, and construct Compilations and add references between them.

Generally, F# doesn't allow variable re-assignment. Rather it favors immutable named values via let bindings. So, the following is not possible:
let a = 3
a = 4
Unless you explicitly mark a as mutable:
let mutable a = 3
a <- 4
@alieniasty
alieniasty / FsharpDI.fs
Created April 9, 2019 11:47
How to perform DI in F#
namespace HelloWorld.Grains
{
/// <summary>
/// Orleans grain implementation class HelloGrain.
/// </summary>
public class HelloGrain : Orleans.Grain, IHello
{
private readonly ILogger logger;
public HelloGrain(ILogger<HelloGrain> logger)
@alieniasty
alieniasty / Kurwa.js
Created April 8, 2019 08:49
Skrypt dodający losową kurwę
javascript:{/** * Made by kurwa Kerai * chcesz rozpowszechniac? podawaj źródło * chcesz cos zmieniac? wszystkie komentarze pisz * w komentarzach wieloliniowych, bo sie moze popsuc pod chrome i opera... */var whitespace = "-\/|.,!:;'\" \t\n\r{}[]()";var stack = [];var curNIndex = 0;var curNode = null;var curIndex = -1;var state = 1;var lastword = '';String.prototype.startsWith = function(prefix) {return this.indexOf(prefix) === 0;};String.prototype.endsWith = function(suffix) {return this.match(suffix+"$") == suffix;};/* Nie moge tak robić niestety */function kurwizeLink(url){if(url.startsWith('javascript:'))return url; /* chyba niewiele moge zrobic kurwa */if(url.startsWith('#'))return url;if(url.startsWith('http://kurwa.keraj.net/?url='))return url; /* no, bo kurwa, bez jaj ;p */var base = '';if(!url.startsWith('http')){base = location.protocol + "://" + location.host;if(!url.startsWith('/')){var pathname = location.pathname;base += pathname.substring(0, pathname.lastIndexOf('/'));}}return 'http://kurwa.kera
Why do we need monads?We want to program only using functions. ("functional programming" after all -FP).
Then, we have a first big problem. This is a program:
f(x) = 2 * x
g(x,y) = x / y
How can we say what is to be executed first? How can we form an ordered sequence of functions (i.e. a program) using no more than functions?
1. A continuation is simply a function that you pass into another function to tell it what to do next.
@alieniasty
alieniasty / GetNumberFromStrin.fs
Created March 26, 2019 14:04
Filter list based on predicate
open System
let getNumberFromString s =
s
|> Seq.toList
|> List.filter Char.IsDigit
|> String.Concat
|> int
@alieniasty
alieniasty / RecRevList.fs
Created March 26, 2019 11:27
*Given [2] to h::t. Head is 2, tail i empty list.
let revR list =
let rec shift acc tail =
match tail with
| [] -> acc
| h::t -> shift ([h] @ acc) t
shift [] list