Skip to content

Instantly share code, notes, and snippets.

View ducaale's full-sized avatar

Mohamed Daahir ducaale

View GitHub Profile
@ducaale
ducaale / sql-template-string.js
Last active April 26, 2020 20:02
Generating SQL prepared statements with Javascript tagged template literals
const queryObjectId = Symbol("sql_query")
const sql = (string, ...values) => {
const isQuery = query => query.hasOwnProperty(queryObjectId)
let text = ''
for (const [i, s] of string.entries()) {
text += s
if (i !== string.length - 1) {
if (isQuery(values[i])) {
@ducaale
ducaale / bits.rs
Last active December 29, 2022 18:34
Bit manipulation in Rust
fn main() {
// format a number in two’s complement representation
let a: i8 = -1;
assert_eq!(format!("{:b}", a), "11111111");
// concatenate 3 bit slices
let b: i8 = (0b10 << 4) + (0b10 << 2) + (0b10);
assert_eq!(b, 0b101010);
#[derive(Debug)]
struct User {
name: String,
email: String,
sign_in_count: u64,
active: bool,
}
impl Default for User {
fn default() -> Self {
@ducaale
ducaale / piping-experiments.js
Last active July 7, 2020 20:41
Using ES6 Proxies to turn methods into pipe friendly functions
// .babelrc
// {"plugins": [["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }]]}
const pipeable = (class_) => new Proxy({}, {
get: (target, prop) => (
(prop in class_.prototype)
? (...args) => (receiver) => class_.prototype[prop].call(receiver, ...args)
: class_[prop].bind(class_) // https://stackoverflow.com/a/30819436/5915221
)
});
@ducaale
ducaale / 1d_2d.js
Last active December 29, 2022 17:09
I am tired of having to solve this again and again
const gridToLine = (x, y, width, height) => {
return (y * width) + x;
}
const lineTogrid = (a, width, height) => {
const x = a % width;
const y = a / width;
return [x, y]
}
// Given a list of sets [set1, set2, set3, ... setn], and a window with start and end such that (end - start) is constant.
// What is the fastest way to get all possible aggregates of the sets by moving start and end by 1 each time?
//
// example:
// const data = [set1, set2, set3, set4, set5, set6, set7, set8, set9];
// const windowSize = 4
// const [s, e] = [0, windowSize]
//
// in this case, our result should be:
// const result = [
@ducaale
ducaale / opamx.sh
Last active September 10, 2020 21:53
WIP: A convenience wrapper for opam. Inspired by https://khady.info/opam-npm.html
# inspired by https://khady.info/opam-npm.html
USAGE="A convenience wrapper for opam
USAGE:
opamx [options] [command]
OPTIONS:
-h, --help Print help information
@ducaale
ducaale / notes-on-brisk-reconciler.md
Last active September 30, 2020 08:11
Notes on using brisk-reconciler in OCaml

There are two ways to declare a component in brisk-reconciler:

  1. By using a normal function
let%component counterButtons = () => {
  let%hook (count, setCount) = Hooks.state(0);

  <view>
    <button title="Decrement" onPress={() => setCount(count => count - 1)} />
    <text text={"Counter: " ++ str(count)} />
@ducaale
ducaale / ppxes-I-would-to-see-in-reasonml.re
Created October 3, 2020 23:47
PPXes I would like to see in OCaml/Reasonml
let add = (a, b) => a + b;
// modules with test attribute will be stripped from source in production mode
// running `esy test` should find and run all the functions inside test modules
[@test]
module Test {
[@desciption "1 + 1 is equal to 2"]
let test_1 = () => {
Assert.areEqual(2, add(1, 1));
}
@ducaale
ducaale / cli-tools-i-use.md
Last active May 12, 2021 13:11
Cli tools I use