Skip to content

Instantly share code, notes, and snippets.

View Mazuh's full-sized avatar
👨‍💻
Code Summoner

Marcell Guilherme Costa da Silva Mazuh

👨‍💻
Code Summoner
View GitHub Profile
@Mazuh
Mazuh / chained-promises.js
Created July 31, 2024 12:47
Chained promises example
const delayedPrint = (content) => new Promise(r => setTimeout(() => r() & console.log(content), 1000));
await ["João", "Maria", "todo mundo"].reduce(
(chained, n) => chained.then(() => delayedPrint(`Oi, ${n}!`)),
Promise.resolve()
);
@Mazuh
Mazuh / balanced_octopus.ex
Created April 22, 2024 16:15
Checa se delimitadores em uma string estão balanceados.
defmodule BalancedOctopus do
@opening_to_closing %{"(" => ")", "[" => "]", "{" => "}"}
@closing_to_opening %{")" => "(", "]" => "[", "}" => "{"}
def balanced?(text, stack \\ [])
def balanced?("", []), do: true
def balanced?(_text, :error), do: false
def balanced?("", _pending_stack), do: false
def balanced?(text, stack) do
@Mazuh
Mazuh / equals-pm.ex
Created April 16, 2024 12:06
Elixir example of pattern matching for equal values
defmodule Comp do
def equals?(same, same), do: true
def equals?(one, another), do: false
end
IO.inspect Comp.equals?(4, 2)
# false
IO.inspect Comp.equals?(42, 42)
# true
@Mazuh
Mazuh / maybe.py
Created January 23, 2024 03:14
Maybe in Python.
class Maybe:
condition_fn = None
then_fn = None
otherwise_fn = None
def __init__(self, condition_fn):
self.condition_fn = condition_fn
def then(self, then_fn):
self.then_fn = then_fn
@Mazuh
Mazuh / opfs.ts
Created January 23, 2024 00:09
Basic OPFS adapter in TS.
interface OpfsAdapter<T> {
persist: (data: T) => Promise<void>;
retrieve: () => Promise<T | null>;
}
export async function makeOpfsAdapter<T>(filename: string): Promise<OpfsAdapter<T>> {
const opfsRoot = await navigator.storage.getDirectory();
const directoryHandle = await opfsRoot.getDirectoryHandle("my_pretty_stuff", {
create: true,
@Mazuh
Mazuh / .zshrc
Created August 13, 2023 19:45
"NVM use" after entering each directory
# Run 'nvm use' automatically every time there's
# a .nvmrc file in the directory. Also, revert to default
# version when entering a directory without .nvmrc
#
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
@Mazuh
Mazuh / find_node_modules.sh
Created July 11, 2022 12:03
Scripts to clean multiple node_modules recursively.
find . -name 'node_modules' -type d -prune
function getBgColor(variant) {
if (variant === "primary") {
return "blue";
}
if (variant === "danger") {
return "red";
}
if (variant === "warning") {
interface User {
name: string;
age: number;
}
interface AuthorizedUser extends User {
token: string;
}
type UserDisplayProps = { user: User };
import { useState } from "react";
export default function App() {
const [people, setPeople] = useState([]);
const handleSubmit = (event) => {
event.preventDefault();
setPeople([...people, event.target.person.value]);
event.target.reset();
};