Skip to content

Instantly share code, notes, and snippets.

View evanxg852000's full-sized avatar
💭
Awesomeness 👌

Evance Soumaoro evanxg852000

💭
Awesomeness 👌
View GitHub Profile
@evanxg852000
evanxg852000 / wait_group_atomic.rs
Created January 9, 2024 12:50
A basic Rust implementation of Golang WaitGroup (atomic & spin-loop)
use std::{sync::{ Arc, atomic::{AtomicUsize, Ordering}}, hint::spin_loop};
#[derive(Clone)]
pub struct WaitGroup(Arc<WaitGroupInner>);
impl WaitGroup {
pub fn new(count: usize) -> Self {
Self(Arc::new(WaitGroupInner::new(count)))
}
@evanxg852000
evanxg852000 / wait_group.rs
Last active January 9, 2024 12:49
A basic Rust implementation of Golang WaitGroup (mutex)
use std::sync::{Condvar, Mutex, Arc};
#[derive(Clone)]
pub struct WaitGroup(Arc<WaitGroupInner>);
impl WaitGroup {
pub fn new(count: usize) -> Self {
Self(Arc::new(WaitGroupInner::new(count)))
}
@evanxg852000
evanxg852000 / spin_lock.rs
Created September 1, 2023 16:21
A spin lock implementation in rust
// To run undefined behavior checks: `cargo +nightly miri run`
use std::{
cell::UnsafeCell,
ops::{Deref, DerefMut},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread,
@evanxg852000
evanxg852000 / main.go
Created August 31, 2023 09:54
Recurce Center pair programming task [Database server]
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sync"
//main
const http = require('http');
const functions = require('./functions')
//create a server object
http.createServer(functions.API)
.listen(8000);
//functions.js
function API(req, res) { //http function
const ip = res.socket.remoteAddress;
console.log(`Client ip address is ${ip}.`);
res.writeHead(200);
res.end('Hello, World!');
}
function Ticker(envs) { //cron function
console.log(envs);
#.eserveless.yaml
repo: https://github.com/evanxg852000/node-eserveless-example
runtime: node
functions:
- name: API
type: http
- name: Ticker
type: cron
schedule: "*/1 * * * *"
meta:
impl InvertedIndex {
// ... methods
pub fn add_document(&mut self, name: &str, content: &str) {
self.count += 1; // increment doc count and use it as next doc id
self.docs.insert(self.count, name.to_string(), ); // insert in docs map
for word in content.split_whitespace() { // split to word (term)
let word = word.to_lowercase();
match self.index.get_mut(&word) { // find term
#[derive(Debug)]
pub struct InvertedIndex {
count: u32,
docs: HashMap<u32, String>,
index: HashMap<String, HashMap<u32, u32>>,
}
impl InvertedIndex {
pub fn new() -> Self {
def encode(data, simple_str=False):
if isinstance(data, ValueError):
return f'-{str(data)}\r\n'
elif isinstance(data, str) and simple_str:
return f'+{data}\r\n'
elif isinstance(data, int):
return f':{data}\r\n'
elif isinstance(data, str):
return f'${len(data)}\r\n{data}\r\n'
elif isinstance(data, list) or isinstance(data, tuple):