Skip to content

Instantly share code, notes, and snippets.

View leandromoh's full-sized avatar
💭
Talk is Cheap, Show me the code!

Leandro Fernandes leandromoh

💭
Talk is Cheap, Show me the code!
View GitHub Profile
public static class EnumExtensions
{
public static bool IsObsolete(this Enum value)
{
var enumType = value.GetType();
var enumName = enumType.GetEnumName(value);
var fieldInfo = enumType.GetField(enumName);
return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
}
@leandromoh
leandromoh / LogExtension.cs
Last active March 18, 2022 13:41
helper to capture how many time operations consume
class Program
{
void Foo(string[] args)
{
using var _ = this.MeasureTimeCurrentMethod();
// lot of operations
// prints "Programg.Foo was xxx ms"
}
@leandromoh
leandromoh / single mongo bulk vs multiple.js
Created March 3, 2022 15:48
compares the time of doing a single mongo bulk write vs divide all operations in N bulks
function createRandomObj(fieldCount, allowNested) {
var generatedObj = {};
for(var i = 0; i < fieldCount; i++) {
var generatedObjField;
switch(randomInt(allowNested ? 6 : 5)) {
case 0:
generatedObjField = randomInt(1000);
@leandromoh
leandromoh / Validate dynamic schema on C#.cs
Last active February 23, 2022 18:05
Validate dynamic schema on C#
public class CreateRequest
{
public string Name { get; set; }
public string CreatedBy { get; set; }
public JObject Schema { get; set; }
}
public class CreateRequestValidator : AbstractValidator<CreateRequest>
{
private readonly Regex _indexer = new Regex(@"\[\d+\]", RegexOptions.Compiled);
// https://fiis.com.br/${ticker}11/
var dys = [...document.querySelectorAll("#last-revenues--table tbody tr")]
.map(x => x.cells[3].innerHTML.replace("%", '').replace(",",'.'))
.map(x => parseFloat(x));
dys.reduce((a, b) => a + b) / dys.length;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Timers;
public class CustomCache
{
private readonly ConcurrentDictionary<string, Task<(object result, DateTime expirationDate)>> dic = new();
private readonly TimeSpan _threshold;
private readonly Timer _timer;
open System
open PuppeteerSharp
open System.Threading.Tasks
open System.Text.Json
type Position = { Ticker: string; Qtd: int }
let getTaskResult (task: Task<'T>) = task.GetAwaiter().GetResult()
let positions = [
@leandromoh
leandromoh / streaming file through C#.cs
Created September 23, 2021 19:19
tested using .net core 3.1
// controller
public async Task DownloadFile([FromQuery] DownloadFileQueryRequest request, CancellationToken cancellationToken)
{
var responseObj = await process(request, cancellationToken);
HttpContext.Response.ContentType = MediaTypeNames.Application.Octet;
HttpContext.Response.StatusCode = StatusCodes.Status200OK;
HttpContext.Response.Headers["Access-Control-Expose-Headers"] = "Content-Disposition";
HttpContext.Response.Headers["Content-Disposition"] = new ContentDispositionHeaderValue("attachment")
@leandromoh
leandromoh / ObjectSpread.cs
Created September 19, 2021 07:00
Spread object in C#
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.Json;
using static MoreLinq.Extensions.DistinctByExtension;
// adapted from https://gist.github.com/AlbertoMonteiro/f6aa8c92a7d93c9b32b4c2ab19a67def
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using System.Buffers;
using System.Text;
public class Program
{
public static async Task Main(string[] args)
{
#if DEBUG