Skip to content

Instantly share code, notes, and snippets.

@JanMalch
JanMalch / SplitList.java
Last active May 20, 2018 19:37
Splitting a List evenly into Sub-Lists
import com.sun.istack.internal.NotNull;
import java.util.*;
/**
* @author github.com/JanMalch
*/
public class SplitList {
/**
* Splits a List into even sub-lists. Doesn't sort the sub-lists.
@JanMalch
JanMalch / regedit_open_folder.md
Created July 4, 2018 15:26
Windows Explorer & regedit - Open folder in a new explorer window

Windows Explorer & regedit - Open folder in a new explorer window

Open regedit.

Context menu for right click on folders in left panel of Windows Explorer or on background of a directory in right panel

  • Navigate to
  • HKEY_CLASSES_ROOT\Directory\Background\shell if you are an administrator
  • HKEY_CURRENT_USER\Software\Classes\directory\Background\shell if you are a normal user
@JanMalch
JanMalch / Angular - custom structural directives.md
Last active June 9, 2024 16:30
Writing your own structural directives with context variables

Writing your own structural directives with context variables

Complete code in math.directive.ts

After reading this you will be able to create a structural directive with inputs and context variables and use it like this:

<div *math="10; exponent: 3; let input; 
            let exponent = exponent; let r = root;
 let p = power; let ctrl = controller"&gt;
@JanMalch
JanMalch / flex.css
Created October 26, 2018 14:54
Basic CSS flexbox utilities
.flex {
display: flex;
}
.inline-flex {
display: inline-flex;
}
.flex.align-center {
align-items: center;
@JanMalch
JanMalch / unreachable.exhaustive-switch.ts
Created January 4, 2019 20:45
Exhaustive switch in TypeScript
// Add and use this function to get compile time errors if your switches are not exhaustive.
export function unreachable(value: never, message = `No such case in exhaustive switch: ${value}`) {
throw new Error(message);
}
// Usage
enum FooBar {
foo,
@JanMalch
JanMalch / rxjs-input.directive.ts
Created January 30, 2019 16:50
Angular directive for input value as RxJS observable
import {Directive, ElementRef, Input, OnDestroy} from "@angular/core";
import {fromEvent, Observable, Subject} from "rxjs";
import {debounceTime, distinctUntilChanged, map, startWith, takeUntil} from "rxjs/operators";
/**
* An input directive that makes it easier to get the input's value in an Observable.
* These directive acts a one-way for output. You can't set the input's value.
* @example
* <input rxjsInput>
*
@JanMalch
JanMalch / demo-with-type-guards.ts
Last active July 3, 2020 22:26
Using TypeScript's features to create a type safe validation flow
// Define your own interfaces and functions:
interface MyForm { name: string; }
function getFormData(): Unvalidated<MyForm> {
return { name: 'John' } as Unvalidated<MyForm>;
}
function process(myForm: Validated<MyForm>) { }
@JanMalch
JanMalch / experiments.ts
Last active June 19, 2022 16:37
TypeScript helper for refactorings
export interface Experiment<T> {
label: string;
use: () => T;
try: () => T;
predicate?: (a: T, b: T) => boolean;
}
function measure<T>(callback: () => T) {
const start = performance.now();
const result = callback();
@JanMalch
JanMalch / Caesar.kt
Created August 4, 2020 17:11
Small demo of combining custom operators and function literals with receivers.
class CaesarConverter {
// cannot use operator plus because it would be shadowed and not used
operator fun String.minus(offset: Int) =
this.toCharArray().map { it - offset }.joinToString(separator = "") { it.toString() }
}
fun caesar(block: CaesarConverter.() -> String) = CaesarConverter().block()
fun main() {
val encrypted = caesar { "test" - 4 }
@JanMalch
JanMalch / rating_bar.css
Created October 6, 2020 20:10
A rating bar with pure CSS
body {
padding: 20px;
}
.container {
display: inline-flex;
/* or display: flex; */
flex-direction: row-reverse;
justify-content: flex-end;
}