Skip to content

Instantly share code, notes, and snippets.

View zhangr4's full-sized avatar

zhangr4 zhangr4

View GitHub Profile
@zhangr4
zhangr4 / Generate-CsProjDependencyGraph.ps1
Created February 14, 2025 04:01
Generate Project Dependency of a CSharp project to dot file, which can be visualized by graphviz
$projects = Get-ChildItem -Recurse -Filter *.csproj # find all csharp project recursively from current working path
$dotFile = "dependencies.dot" # file name of generated dot file
$edges = @() # represent edges as an array of objects
foreach ($project in $projects) {
$projectXml = [xml](Get-Content $project.FullName) # parse .csproj as xml
$projectDir = Split-Path -Parent $project.FullName
$references = $projectXml.Project.ItemGroup.ProjectReference # get project references of $project
@zhangr4
zhangr4 / guard.cs
Last active December 18, 2023 13:39
guard usage
// refer to https://github.com/ardalis/GuardClauses/tree/main
public interface IGuardClause { }
public class Guard : IGuardClause
{
private Guard() { }
public static IGuardClause Against { get; } = new Guard();
}
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges
#region Indexable Example
//System.Index
// use '^' operator: means read from end = true
// '^' operator requires type is countable and have indexer:
interface IIndexable<T>{
public int Length { get; } // need public property 'Length', which has get access
public T this[int index] { get; } // need public indexer, which takes a single int as the argument.
@zhangr4
zhangr4 / SimpleImplementationOfMiddlewarePipeline.cs
Last active March 9, 2023 08:21
a simple implementation like asp.net middleware pipeline
/*
* just for learning
* key concept, LinkedList
* refer https://blog.csdn.net/qq_16587307/article/details/104301877
* refer https://medium.com/@bonnotguillaume/software-architecture-the-pipeline-design-pattern-from-zero-to-hero-b5c43d8a4e60
*/
namespace pipeline
{
public class Program
@zhangr4
zhangr4 / domainUserName.csx
Last active March 5, 2025 10:04
csharp snippets
Console.WriteLine();
// <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName);
//this one can get domain and user name
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Console.WriteLine();
Console.WriteLine(userName);
@zhangr4
zhangr4 / commonScripts.ps1
Created November 25, 2022 04:13
windows PowerShell scripts
# get internal ip address
ipconfig
# get external ip address
Invoke-WebRequest ifconfig.me
@zhangr4
zhangr4 / stack_fibonacci
Created July 11, 2022 14:51
a trial on using stack implement fibonacci
import java.util.Stack;
class Program {
public static void main(String[] args) {
for(int i = 0; i <= 10; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();
for(int i = 0; i <= 10; i++) {
@zhangr4
zhangr4 / quickSort.java
Created June 27, 2022 14:49
quick sort_example
public class quickSort {
public static void main(String[] args) {
int[] test = new int[]{5,1,1,2,0,0};
quickSortArr(test, 0, test.length-1);
printArr(test);
}
public static void printArr(int[] arr) {
@zhangr4
zhangr4 / JavaStreamPredicateAndMapper.java
Created June 27, 2022 09:29
static functions for filter() and map() in Java 8 - stream
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.Arrays;
/* raw problem and better solution:
https://stackoverflow.com/questions/57836937/call-custom-static-functions-from-filter-and-map-in-java-8-stream
*/
class Solution {
import pandas as pd
a = pd.DataFrame([1,2,2,3,3,3,1,1,2])
b = a.loc[:,0]
c = b.loc[b.shift()!=b]
print(c.tolist())
// https://stackoverflow.com/questions/19463985/pandas-drop-consecutive-duplicates