Created
December 16, 2025 04:14
-
-
Save ClarkeRemy/344a6b47531903cff047370d3d204dd8 to your computer and use it in GitHub Desktop.
Typescript Dictionary Passing style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Num<a> = { plus : (l:a,r:a)=>a | |
| , minus : (l:a,r:a)=>a | |
| , mul : (l:a,r:a)=>a | |
| , neg : (arg:a)=>a | |
| , abs : (arg:a)=>a | |
| , signum : (arg:a)=>a | |
| , fromNumber : (arg:number)=>a | |
| } | |
| const NumberEqual : Equal<number> = | |
| { equal : (l,r)=> l===r | |
| , notEqual : (l,r)=> !(l===r) | |
| } | |
| const NumberOrd : Order<number> = | |
| { eq : NumberEqual | |
| , cmp : (l,r) => (l < r) ? "LT" : | |
| (l > r) ? "GT" : | |
| "EQ" | |
| , lt : (l,r) => l < r | |
| , le : (l,r) => l <= r | |
| , gt : (l,r) => l <= r | |
| , ge : (l,r) => l >= r | |
| , max : (l,r) => (l > r) ? l : r | |
| , min : (l,r) => (l > r) ? r : r | |
| } | |
| const NumberNum : Num<number> = | |
| { plus : (l,r) => l + r | |
| , minus : (l,r) => l - r | |
| , mul : (l,r) => l * r | |
| , neg : (a) => -a | |
| , abs : (a) => (a < 0) ? -a : a | |
| , signum : (a) => (a > 0) ? 1 : | |
| (a < 0) ? -1 : | |
| 0 | |
| , fromNumber : (x) => x | |
| } | |
| const sizeNumber : <a>(ord:Order<a>,num:Num<a>)=>(arg:a)=>String | |
| = (ord , num) => { | |
| const three = num.fromNumber(3) | |
| const ten = num.fromNumber(10) | |
| const lessThan = ord.lt | |
| return (n)=> lessThan(n,three) ? "smol" : | |
| lessThan(n, ten) ? "smol+" : | |
| "smol++" | |
| } | |
| const sizeNumber_ : <a>(ord:Order<a>,num:Num<a>)=>(arg:a)=>String | |
| = (ord , num) => (n) => ord.lt(n,num.fromNumber(3)) ? "smol" : | |
| ord.lt(n,num.fromNumber(10)) ? "smol+" : | |
| "smol++" | |
| console.log(sizeNumber(NumberOrd,NumberNum)(15)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment