Skip to content

Instantly share code, notes, and snippets.

View sibu-github's full-sized avatar

Sibaprasad Maiti sibu-github

  • http://thetaonelab.com/
View GitHub Profile
@sibu-github
sibu-github / lib.rs
Created April 14, 2022 03:31
Compose Higher Order Function in Rust
// compose a Higher Order Function with two function as input parameter
// so the compose function takes two parameters f1, f2 and returns a function f3
// f1, f2 & f3 - all functions takea one parameter i32 and returns a value i32
#[allow(dead_code)]
fn compose(f1: impl Fn(i32) -> i32, f2: impl Fn(i32) -> i32) -> impl Fn(i32) -> i32 {
move |x: i32| f2(f1(x))
}
#[cfg(test)]
@sibu-github
sibu-github / transferBalance.js
Created July 10, 2022 18:51
Example of transaction in Mongo DB
// transfer balance from one account to another
const transferBalance = async (fromAccount, toAccount, amount) => {
// get database client
const client = await database.getClient();
// create a database session
const session = client.startSession();
// define transaction options
const transactionOptions = {
readPreference: 'primary',
@sibu-github
sibu-github / main.rs
Created March 12, 2023 17:34
async closure with the closure taking references in the argument
use futures::future::{BoxFuture, FutureExt};
#[tokio::main]
async fn main() {
println!("Hello, world!");
let db = AppDatabase(0);
let r = db.execute_transaction(transaction).await;
println!("{:?}", r);
}
@sibu-github
sibu-github / main.js
Last active August 30, 2023 19:34
Reverse each word in a sentence
/**
* PROBLEM: Given a sentence reverse each word and print it back.
* For example if the given sentence is "Mother India"
* then output would be "rehtoM aidnI"
* If given sentence is "hare-krishna hare-rama"
* then output would be "erah-anhsirk erah-amar"
*/
// define a list by chars that we need to split the string with
@sibu-github
sibu-github / threadExample.js
Created September 4, 2023 18:17
Example of multi thread creation in Node JS
const {
Worker,
isMainThread,
parentPort,
workerData
} = require("worker_threads");
if (isMainThread) {
// create new threads from the main thread
const thread1 = new Worker(__filename, {workerData: 'thread 1'});