Created
July 14, 2023 09:53
-
-
Save InfiniteXyy/dd3f8d9ce632a9018957e2399f87bc55 to your computer and use it in GitHub Desktop.
Write C# in a JS like way
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
public static class console | |
{ | |
private static void WritePrefix(string prefix, ConsoleColor color) | |
{ | |
Console.ForegroundColor = color; | |
Console.Write($"{prefix}\t"); | |
Console.ResetColor(); | |
} | |
private static string? TransformArg(object? x) | |
{ | |
var formatArray = (IEnumerable<string?> arr) => "[" + string.Join(", ", arr) + "]"; | |
var formatSet = (IEnumerable<string?> arr, int size) => $"Set({size}) {{ {string.Join(", ", arr)} }}"; | |
return x switch | |
{ | |
null => "null", | |
int[] array => formatArray(array.Select(x => TransformArg(x))), | |
String[] array => formatArray(array.Select(x => $"\"{TransformArg(x)}\"")), | |
HashSet<int> set => formatSet(set.ToArray().Select(x => TransformArg(x)), set.Count), | |
HashSet<string> set => formatSet(set.ToArray().Select(x => $"\"{TransformArg(x)}\""), set.Count), | |
object?[] array => formatArray(array.Select(x => TransformArg(x))), | |
_ => x.ToString() | |
}; | |
} | |
private static void FormatAndLog(string prefix, ConsoleColor color, params object?[] args) | |
{ | |
WritePrefix(prefix, color); | |
Console.WriteLine(string.Join(" ", args.ToArray().Select(TransformArg).ToArray())); | |
} | |
public static void log(params object?[] args) | |
{ | |
FormatAndLog("[LOG]", ConsoleColor.Green, args); | |
} | |
public static void info(params object?[] args) | |
{ | |
FormatAndLog("[INFO]", ConsoleColor.Blue, args); | |
} | |
public static void error(params object?[] args) | |
{ | |
FormatAndLog("[ERROR]", ConsoleColor.Red, args); | |
} | |
} |
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
console.log("this is a simple log", 312); | |
console.info("what is info"); | |
console.info(_.reverse(_.array(1, 2, 3)), _.array("Hello", "World")); | |
console.error(_.set("1", "2", "2")); |
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
public static class _ | |
{ | |
public static T[] array<T>(params T[] args) | |
{ | |
return args; | |
} | |
public static HashSet<T> set<T>(params T[] args) | |
{ | |
return new HashSet<T>(args); | |
} | |
public static T[] reverse<T>(T[] array) | |
{ | |
T[] reversedArray = new T[array.Length]; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
reversedArray[i] = array[array.Length - 1 - i]; | |
} | |
return reversedArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment