Skip to content

Instantly share code, notes, and snippets.

View BoolPurist's full-sized avatar

Florian BoolPurist

View GitHub Profile
@BoolPurist
BoolPurist / example_drawing_gaps.rs
Created March 4, 2023 10:54
example for issue ratatui
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::{
error::Error,
io,
time::{Duration, Instant},
};
@BoolPurist
BoolPurist / AsTextExtension.cs
Created July 2, 2022 18:56
Extension to get string readable string representation for any sequence.
using System.Text;
namespace <SomeNamespace>;
public static class AsTextExtension
{
public static string AsText<T>(this IEnumerable<T> sequence, string separator = ", ", int numberOfBreak = 50)
{
var builder = new StringBuilder();
builder.Append("[ ");
delegate T MapperFunction<T>(T number);
// Ruegabewert ist eine Liste genauso lange wie der Parameter arr.
// Die Liste enthaelt alle gemappten Elemente von arr.
// Mappen bedeutet, dass ein Element durch die Funktion Mapper auf einen neuen Wert abgebildet wird.
// Die Liste enthaelt alle neuen gemappten Werte.
static List<T> Map<T>(T[] array, MapperFunction<T> mapper)
{
var list = new List<T>();
foreach (var element in array)
{
@BoolPurist
BoolPurist / Filter.cs
Last active July 2, 2022 08:23
How delegate work
delegate bool SomePredicate<T>(T a);
public List<T> Filter<T>(T[] input, SomePredicate<T> function)
{
var newList = new List<T>();
foreach(var element in input)
{
if (function(element))
{
newList.Add(element);
@BoolPurist
BoolPurist / DelegateImpl.cs
Last active July 1, 2022 13:07
Delegate under the Hood.
internal static class Program
{
public delegate void DoSomething();
// Compiler created this class in background because of public delegate void DoSomething().
public class DoSomethingDelegate
{
private List<DoSomething>? _invocationList = new List<DoSomething>();
public DoSomethingDelegate(DoSomething function)
@BoolPurist
BoolPurist / makeArgsHandy.cpp
Last active March 9, 2022 21:43
Small function to get a practical vector for the in-line arguments in a console. Otherwise one has to work with them via pointer arithmetic.
#include <string>
#include <vector>
/// Precondition: argc and argv were given as parameter for main function
/// because the represents the in lines arguments for a program.
/// Precondition: argc is the number of elements in argv.
/// Note: Returns an empty vector if argc is zero.
std::vector<std::string> makeArgsHandy(int argc, char** argv)
{
size_t size{static_cast<size_t>(argc)};
std::vector<std::string> converted{size};