This file contains 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
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))) | |
} |
This file contains 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
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))) | |
} |
This file contains 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
// To run undefined behavior checks: `cargo +nightly miri run` | |
use std::{ | |
cell::UnsafeCell, | |
ops::{Deref, DerefMut}, | |
sync::{ | |
atomic::{AtomicBool, Ordering}, | |
Arc, | |
}, | |
thread, |
This file contains 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
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"log" | |
"net/http" | |
"sync" |
This file contains 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
//main | |
const http = require('http'); | |
const functions = require('./functions') | |
//create a server object | |
http.createServer(functions.API) | |
.listen(8000); |
This file contains 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
//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); |
This file contains 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
#.eserveless.yaml | |
repo: https://github.com/evanxg852000/node-eserveless-example | |
runtime: node | |
functions: | |
- name: API | |
type: http | |
- name: Ticker | |
type: cron | |
schedule: "*/1 * * * *" | |
meta: |
This file contains 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
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 |
This file contains 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
#[derive(Debug)] | |
pub struct InvertedIndex { | |
count: u32, | |
docs: HashMap<u32, String>, | |
index: HashMap<String, HashMap<u32, u32>>, | |
} | |
impl InvertedIndex { | |
pub fn new() -> Self { |
This file contains 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
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): |
NewerOlder