Skip to content

Instantly share code, notes, and snippets.

View lucashcl's full-sized avatar
🔎

Lucas Cipriano Lima lucashcl

🔎
View GitHub Profile
@lucashcl
lucashcl / Result.ts
Created September 30, 2025 11:42
Rust's result implemented in typescript
interface Success<T> {
type: 'success';
data: T;
}
interface Failure<E> {
type: 'failure';
error: E;
}
class FullTextSearch<T> {
// token -> (item -> frequência do token no item)
private index: Map<string, Map<T, number>> = new Map();
constructor(items: T[], private textExtractor: (item: T) => string) {
for (const item of items) {
this.addToIndex(item);
}
}
// Eagerly evaluated
export default class Pipe<T> {
constructor(private readonly _value: T) { }
to<U>(fn: (value: T) => U) {
return new Pipe(fn(this._value));
}
get value() {
return this._value;
@lucashcl
lucashcl / autosuggestionsInput.tsx
Created June 17, 2025 23:44
Shadcn Autosuggestion Input
import { useEffect, useState } from "react";
import { Input } from "./ui/input";
type Props = {
suggestions: string[];
};
export function AutosuggestionInput({ suggestions }: Props) {
const [input, setInput] = useState("");
const [suggestion, setSuggestion] = useState("");
@lucashcl
lucashcl / Result.ts
Created June 11, 2025 17:19
Result type
type Result<T> = readonly [T, null] | readonly [null, Error]
@lucashcl
lucashcl / useArrayState.ts
Created February 5, 2025 19:31
UseArrayState react hook
import { useState } from "react";
export function useArrayState<T>(initialState: T[] = []) {
const [state, setState] = useState<T[]>(initialState)
const add = (newValue: T) => {
setState((currentState) => [...currentState, newValue])
}
const remove = (index: number) => {
setState(
(currentState) => currentState.filter((_, i) => i !== index)
export function download(blob: Blob, fileName: string) {
const blobURL = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = blobURL
link.download = fileName
link.click()
URL.revokeObjectURL(blobURL)
link.remove()
}
export function parseCSV<T = Record<string, string>>({
csv,
headers,
delimiter = ","
}: {
csv: string
delimiter?: string
headers?: string[]
}) {
const header = headers ?? csv.slice(0, csv.indexOf("\n")).split(delimiter)
@lucashcl
lucashcl / Led.cc
Last active January 28, 2025 19:40
Led class for arduino
class Led {
private:
int _pin;
int _state;
public:
Led(int pin, int state = LOW){
_pin = pin;
_state = state;
pinMode(_pin, OUTPUT);
digitalWrite(_pin, _state);
export default class AsyncQueue<T> {
private _items: Array<T> = []
private _pending: ((value: T) => void) | null = null;
private wait() {
return new Promise<T>(resolve => { this._pending = resolve })
}
enqueue(item: T) {
if (!this._pending) {
this._items.push(item)