Skip to content

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2023-05-25'>
<Cube currency='USD' rate='1.0735'/>
@tonyonodi
tonyonodi / prettify-generic.ts
Created February 14, 2023 15:26
Prettify generic
// from this tweet https://twitter.com/mattpocockuk/status/1622730173446557697
type Prettify<T> = {
[K in keyof T]: T[K]
} & {};
{ "date": "11-07-22", "rates": { "USD": "1.0163", "JPY": "138.05", "BGN": "1.9558", "CZK": "24.614", "DKK": "7.4424", "GBP": "0.84585", "HUF": "402.45", "PLN": "4.7630", "RON": "4.9431", "SEK": "10.6665", "CHF": "0.9913", "ISK": "139.50", "NOK": "10.2630", "HRK": "7.5190", "TRY": "17.6026", "AUD": "1.4871", "BRL": "5.4345", "CAD": "1.3201", "CNY": "6.8095", "HKD": "7.9769", "IDR": "15210.73", "ILS": "3.5325", "INR": "80.5280", "KRW": "1321.61", "MXN": "20.8477", "MYR": "4.4992", "NZD": "1.6464", "PHP": "56.882", "SGD": "1.4228", "THB": "36.602", "ZAR": "17.1922" }}
@tonyonodi
tonyonodi / io-ts-compose.ts
Last active December 5, 2021 23:47
Compose io-ts Codecs
export const compose = <Name extends string, I2, A extends I2, B, I1, O1>(
name: Name,
Codec1: t.Type<A, O1, I1>,
Codec2: t.Type<B, A, I2>
): t.Type<B, O1, I1> => {
return new t.Type<B, O1, I1>(
name,
(u): u is B => Codec2.is(u),
(a, c) =>
pipe(
@tonyonodi
tonyonodi / char.ts
Created August 7, 2021 11:42
Not very useful implementation of Char in TypeScript
const chartag = Symbol("char");
export type Char = string & { __tag: typeof chartag };
type IsChar<S extends string> = S extends `${infer Head}${infer Tail}`
? Head extends ""
? never
: Tail extends ""
? Char
: never
type Zero = [];
type Nat = [Nat] | Zero;
type NonZeroNat = [NonZeroNat] | [Zero];
type Inc<A extends Nat> = [A];
type Dec<A extends NonZeroNat> = A[0];
type Add<A extends Nat, B extends Nat> = Add_<A, B>;
// This trick taken from https://github.com/Microsoft/TypeScript/pull/24897
type Add_<A extends Nat, B extends Nat> = {
const getLongestSemigroup = <
A extends { length: number } = never
>(): S.Semigroup<A> => {
return { concat: (x, y) => (x.length > y.length ? x : y) };
};
const getLongestOptionSemigroup = O.getMonoid<Unit>(getLongestSemigroup());
const longestString = getLongestOptionSemigroup.concat(
string1Opt,
@tonyonodi
tonyonodi / more_functors.ts
Created June 17, 2019 23:34
A more class based functor implementation
// Maybe
class Nothing {
readonly tag: "None" = "None";
public map = (): Nothing => new Nothing();
}
class Just<A> {
readonly tag: "Just" = "Just";
constructor(readonly value: A) {}
public map = <B>(fn: (a: A) => B): Just<B> => new Just(fn(this.value));
@tonyonodi
tonyonodi / functor.ts
Created June 17, 2019 22:42
An attempt at higher kinded types in TypeScript
// Maybe
class Nothing {
readonly tag: "None" = "None";
}
class Just<A> {
readonly tag: "Just" = "Just";
constructor(readonly value: A) {}
}
{--
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
--}
let fib = 1 : 2 : zipWith (+) fib (tail fib)
foldr (+) 0 $ filter even $ takeWhile (< 4e6) fib