Skip to content

Instantly share code, notes, and snippets.

@fairjm
fairjm / ChatController.java
Last active November 14, 2023 18:49
azure openai java sdk function stream handle example
// just see the logic, some class and annotation is project related just ignore it.
@RestController
@Slf4j
@RequestMapping("/api/chat")
public class ChatController {
... ...
@LoginCheck
@PostMapping(value = "/stream-sse", produces = "text/event-stream")
@fairjm
fairjm / classical_paxos.rs
Created July 11, 2022 08:13
classical_paxos.rs
use std::{
sync::mpsc::{channel, Receiver, Sender},
thread::{self, JoinHandle}, time::Duration,
};
#[derive(Debug)]
enum Request {
PrepareRequest {
rnd: i32,
reply_to: Sender<Response>,
@fairjm
fairjm / main.rs
Last active April 11, 2022 18:19
remove regex matched text in texts of a dir
//! need regex = "1.5"
use regex::Regex;
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
// default is to remove header(better reading for some htmls exported by singleFile, like oreilly, etc)
let mut reg_str= r"<header.*>(.|\n)*?</header>";
let args :Vec<String> = env::args().collect();
let dir;
@fairjm
fairjm / 1. Two Sum.rs
Last active December 22, 2019 10:10
my_bad_solutions
/// https://leetcode.com/problems/two-sum/
impl Solution {
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut nums2 = nums.clone();
nums2.sort();
let mut begin = 0;
let mut end = nums.len() - 1;
while begin < end {
let r = nums2[begin] + nums2[end];
if r == target {
@fairjm
fairjm / elixir_snippet.exs
Last active June 6, 2019 04:32
store some snippets
camelcase = fn a -> Regex.replace(~r/_([a-z]+)/,a, fn _,<<w::utf8, b::binary>> -> String.upcase(<<w>>) <> b end) end
upcaseFirst = fn
"" -> ""
<<a::utf8, b::binary>> -> String.upcase(<<a>>) <> b
end
# md5 see https://gist.github.com/10nin/5713366
def md5(data) do
Base.encode16(:erlang.md5(data), case: :lower)
@fairjm
fairjm / create_sql_to_java.py
Created May 29, 2018 13:28
create sql to java fields
import re
code = """
`name` varchar(10) DEFAULT NULL',
`age` int NOT NULL DEFAULT ''
"""
def parse_field_name(s):
return re.sub(r"_(.)", lambda x: x.group(1).upper(), s.replace("`",""))
def parse_field_type(t):
@fairjm
fairjm / PicGenController.java
Created November 7, 2017 15:16
spring mvc imageIO add text to image
package com.cc.imageio.controller;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
@fairjm
fairjm / filename.fs
Created October 23, 2016 08:08
F# get attachment filename by httpclient
open System.Net.Http
open System.Net.Mime
open System.Text
///get fileName by ContentDisposition or random file name if null
let getFileName (msg: HttpResponseMessage) =
let value = Option.ofObj msg.Content.Headers.ContentDisposition
let result =
value
|> Option.map (fun e -> e.FileName)
@fairjm
fairjm / mysql.fsx
Created September 14, 2016 02:07
FSharp Dapper with FSharp.Interop.Dynamic
open MySql.Data.MySqlClient
open FSharp.Interop.Dynamic
open Dapper
open System
open System.Dynamic
type GList<'a> = System.Collections.Generic.List<'a>
type Ask() =
member val AskId = 0 with get,set
@fairjm
fairjm / GetForegroundWindow.cs
Created February 19, 2016 01:56
get fore ground window
/// <summary>
/// by fairjm.
/// 2016/02/19
/// </summary>
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]