Skip to content

Instantly share code, notes, and snippets.

@SchlenkR
Last active August 31, 2022 12:00
Show Gist options
  • Save SchlenkR/b47c8195e7800370e071e5c8a3978af8 to your computer and use it in GitHub Desktop.
Save SchlenkR/b47c8195e7800370e071e5c8a3978af8 to your computer and use it in GitHub Desktop.
FslangSuggestion1178
module FslangSuggestion1178
type Hero =
| AH
| GM
| NT
module Hero =
let inline convert x =
function
| AH -> x &&& 1 <<< 2
| GM
| NT -> x ||| ~~~(1 <<< 2)
let inline multiple x =
function
| AH
| GM -> x * 5
| NT -> x * 7
let inline isolate ([<InlineIfLambda>] cont) =
function
| AH -> cont AH
| GM -> cont GM
| NT -> cont NT
module Generalized =
let inline isolate ([<InlineIfLambda>] cont) x =
cont x
let inline foo x h = Hero.convert x h + Hero.multiple x h
let fooDirect x h = foo x h
(*
[CompilationArgumentCounts(new int[] {1, 1})]
public static int fooIsolateHero(int x, FslangSuggestion1178.Hero h)
{
switch (h.get_Tag())
{
case 1:
return (x | -5) + x * 5;
case 2:
return (x | -5) + x * 7;
default:
return ((x & 1) << 2) + x * 5;
}
}
*)
let fooIsolateHero x h = Hero.isolate (foo x) h
(*
[CompilationArgumentCounts(new int[] {1, 1})]
public static int fooIsolateAnything(int x, FslangSuggestion1178.Hero h)
{
int num1;
switch (h.get_Tag())
{
case 1:
case 2:
num1 = x | -5;
break;
default:
num1 = (x & 1) << 2;
break;
}
int num2;
switch (h.get_Tag())
{
case 2:
num2 = x * 7;
break;
default:
num2 = x * 5;
break;
}
return num1 + num2;
}
*)
let fooIsolateAnything x h = Generalized.isolate (foo x) h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment