Skip to content

Instantly share code, notes, and snippets.

@svieira
svieira / IntersectionTypes.java
Created July 25, 2024 16:35
Tagged types for Java 10+ using Intersection types
public class IntersectionTypes {
public static <S extends String & SafeString> void mainJava8() {
Worker.safeWork(
Validator.validate("Hello world!")
);
S s = Validator.validate("Intersection types must be aliased");
Worker.safeWork(s);
}
public static /* look Ma, no generics for type aliasing! */ void mainJava10plus() {
@svieira
svieira / higher-kinded-types.ts
Created September 1, 2021 01:10
Higher-Kinded Type Approximation in TypeScript
type Replace<T, X, Y> = {
[k in keyof T]: T[k] extends X ? Y : T[k];
};
declare const higherKindedTypeKey: unique symbol;
type HigherKindedTypeBrand = typeof higherKindedTypeKey;
type HigherKindedType<T> = T & HigherKindedTypeBrand;
type $ = {[higherKindedTypeKey]: unknown};
type HigherKindedTypeReplace<T, X, Y> = T extends $ ? Y : Replace<T, X, Y>
type HKT<T extends HigherKindedType<any>, X, Y> = HigherKindedTypeReplace<T, X, Y>
@svieira
svieira / 1-nominal-types.ts
Created May 19, 2020 05:49
The best nominal typing solution as of TS 3.9.x
export declare const nominalTag: unique symbol
type UniqueTypes = string | number | symbol | boolean
/**
* Needs no validation nominal types
* (allows the base type T to be uplifted without a cast)
*/
export type Unique<T, M extends UniqueTypes> = T & { [nominalTag]?: M }
@svieira
svieira / springer-free-maths-books.md
Created December 28, 2015 16:19 — forked from bishboria/springer-free-maths-books.md
Springer have made a bunch of maths books available for free, here are the direct links
@svieira
svieira / quotes.md
Last active October 6, 2015 14:31
Quotations

Finarfin: Aye. That, in truth, none might e'er deny. Yet lettest thou not cease to bear in mind, that elders be but Eldar, e'en as their offspring, and subject no less than more unto equal passions, to the world's storms and the heart's disquiet, and to wrath, and inconstancy, even as to the over-mastering pride, that durst not yield concession of any, lest smallest surrender be presage to the all; and willfulness doth ever raise the cry of Willful! - as 'twere a mirrored shield to turn back just rebuke.

[with a sweeping gesture, looking at his sons even as he addresses her]

For hard indeed, and surpasseth measure, to be held unto reckoning by one subject, for fealty, and if 'tis so, how much more so when him that challengeth is child and student, younger in years, in knowing, and in deed, and holding all those - or so it seemeth - but from one's self, as a gem's light inwrought by the artisan; for so easily and swift do we forget, that neither earth, nor holy fire, are of our own sole making

Abstraction Suggestions

Summary: use good/established messaging patterns like Enterprise Integration Patterns. Don't make up your own. Don't expose transport implementation details to your application.

Broker

As much as possible, I prefer to hide Rabbit's implementation details from my application. In .Net we have a Broker abstraction that can communicate through a lot of different transports (rabbit just happens to be our preferred one). The broker allows us to expose a very simple API which is basically:

  • publish
  • request
  • start/stop subscription
@svieira
svieira / jinja_render.py
Created June 16, 2014 13:42
How to render a jinja template (standalone)
# Via http://pydanny.com/jinja2-quick-load-function.html
from jinja2 import FileSystemLoader, Environment
def render_from_template(directory, template_name, **kwargs):
loader = FileSystemLoader(directory)
env = Environment(loader=loader)
template = env.get_template(template_name)
return template.render(**kwargs)
@svieira
svieira / package.json
Last active August 29, 2015 14:01
User-agent from the command line
{
"name": "parse-user-agent",
"version": "0.0.1",
"private": true,
dependencies: {
"platform": "~1.1.0",
"split": "~0.3.0"
}
}
@svieira
svieira / app.py
Last active October 21, 2019 10:43
Example sub-mounted Flask application
from flask import Flask, url_for
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
app = Flask(__name__)
app.config["APPLICATION_ROOT"] = "/abc/123"
@app.route("/")
def index():
@svieira
svieira / bash-tricks.sh
Last active March 20, 2024 06:08
Things you can do with bash
# Looping per line of input
some | command | sequence | while read var1 var2; do
# something here
done
# Reading a file line by line
while read var1 var2 etc; do
perform --commands --with $var1 $var2 $etc
done < some-file.ext