Skip to content

Instantly share code, notes, and snippets.

View grishace's full-sized avatar
🏍️

Grigoriy Belenkiy grishace

🏍️
View GitHub Profile
@grishace
grishace / Progression.cs
Created June 11, 2020 20:10
Arithmetic || geometric progression
var arr = new [] { 0, 2, 4, 6, 8, 10 };
Func<int[], bool, Func<int, Tuple<bool, int>, bool>, bool> aggregate =
(a, b, fn) => a.Skip(2).Aggregate(Tuple.Create(b, a[1]), (res, i) => Tuple.Create(res.Item1 && fn(i, res), i)).Item1;
var diff = arr[1] - arr[0];
var isA = aggregate(arr, true, (i, r) => i - r.Item2 == diff);
arr = new [] { 1, 2, 4, 8, 16, 32 };
diff = arr[1] / arr[0];
@grishace
grishace / questions.cs
Last active June 11, 2020 15:42
Coderbyte - question marks
private static readonly Regex rx = new Regex(@"(\d)(\D+)?");
public static string QuestionsMarks(string str) {
var input = rx.Matches(str).Select(m => Tuple.Create(Int32.Parse(m.Groups[1].Value), m.Groups[2].Value)).ToList();
return input
.Zip(input.Skip(1), Tuple.Create)
.Where(t => t.Item1.Item1 + t.Item2.Item1 == 10)
.DefaultIfEmpty(Tuple.Create(Tuple.Create(0, ""), Tuple.Create(0, "")))
.All(t => t.Item1.Item2.Count(c => c == '?') == 3)
.ToString()
@grishace
grishace / longest.cs
Created June 11, 2020 14:43
Coderbyte - longest word
private static readonly Regex rx = new Regex(@"\b(\w+)\b");
public static string LongestWord(string sen) {
return rx.Matches(sen)
.Select(m => m.Groups[0].Value)
.Select(w => Tuple.Create(w, w.Length))
.OrderByDescending(t => t.Item2)
.Select(t => t.Item1)
.FirstOrDefault();
}
@grishace
grishace / frequency.cs
Last active June 30, 2020 13:51
Words frequency with LINQ
var str = "The quick brown fox jumped over the lazy dog. Hello-world! wOrLd WorLd. The new order!";
var words =
new Regex(@"\b([A-Z-]+)\b", RegexOptions.IgnoreCase)
.Matches(str)
.Select(m => m.Groups[0].Value.ToLower())
.GroupBy(g => g)
.Select(g => KeyValuePair.Create(g.Key, g.Count()))
.OrderByDescending(kv => kv.Value)
.ToDictionary(kv => kv.Key, kv => kv.Value);
open System
open FSharp.Control
[<EntryPoint>]
let main _ =
let rec receiver () =
asyncSeq {
yield async.Return "."
yield! receiver ()
@grishace
grishace / Outist.fs
Created May 5, 2020 06:07
Output parameter as tuple
open System
open System.Runtime.InteropServices
type X() =
static member Outist(x: string, y: int, [<Out>] z: string byref) =
z <- "tuO"
(y, x)
[<EntryPoint>]
let main argv =
@grishace
grishace / SeqAsync.fs
Last active April 7, 2020 16:02
Sequential asyncs
open System
open FSharp.Control
[<EntryPoint>]
let main _ =
let rnd = Random()
let workflows =
[|
for x in 1 .. 100 ->
async {
@grishace
grishace / Program.fs
Last active March 6, 2020 15:24
Benchmarking memory traffic from the while loop inside async CE
open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open BenchmarkDotNet.Jobs
open FSharp.Control.Tasks.V2
[<SimpleJob(RuntimeMoniker.NetCoreApp31); MemoryDiagnoser>]
type Test() =
[<Params(100, 1000, 10000)>]
@grishace
grishace / SimulateSub.fs
Created January 25, 2020 20:33
Trying System.Threading.Channels
open System
open System.Threading
open System.Threading.Channels
open FSharp.Control
let channel =
Channel.CreateUnbounded<int>(UnboundedChannelOptions(SingleWriter=true, SingleReader=true))
let mutable cnt = 0
let timerEvent _ =
@grishace
grishace / README.md
Created March 24, 2019 23:33
Adding arbitrary key=value pairs to the FAKE script
fake build -t all --env a=b --env c=d --env e=f
*** [Env ("a","b"); Env ("c","d"); Env ("e","f")]