Skip to content

Instantly share code, notes, and snippets.

View redbar0n's full-sized avatar

redbar0n

View GitHub Profile
@redbar0n
redbar0n / XState-boilerplate-refactor.mdx
Last active April 20, 2024 13:59
The MVC-widget - Through refactoring boilerplate out from an XState example

What if you could have vertically sliced MVC-widgets, that looked something like this?

Using React and XState.

//  A vertically sliced "MVC-widget"

//  VIEW:

export default function Users() {
@redbar0n
redbar0n / rust-simpler-ownership-borrow.rs
Last active April 18, 2024 19:09
Rust - ownership/borrowing could have been simpler without "mutable borrows"
// "Mutable borrows" is where the Rust ownership/borrowing model became too complex for it's own good...
// A function that mutates a value, but does not return the value. So it only performs an effect, often called a side-effect.
fn mutate_value(s: &mut String) {
s.push_str(", world"); // we can mutate the borrowed value. In Rust, push_str returns the unit value, aka. void, so the function does not return the string.
}
fn main() {
let mut hello = String::from("hello");
@redbar0n
redbar0n / routing-ideas.mdx
Last active March 25, 2024 04:46
Routing ideas

a thread where we can discuss some crazy routing ideas a bit out in the open branching out from the previous discussion on filesystem routes over at: https://discord.com/channels/815937377888632913/1014946079965454366

so, first of all, it seems there is a close alignment between how vite-plugin-ssr and router5 handles routing:

I recommend watching this router5 talk from 2:45 - 7:50 https://youtu.be/hblXdstrAg0?t=165 since it's really key to how I think about view / state separation, and I think of routing as a state separate from the view. (After all, and especially in a client-rendered app, what page it is showing is certainly a major part

@redbar0n
redbar0n / clojure-readability.clj
Last active March 3, 2023 22:12
Clojure readability
;; The following example is:
"The program that prints the first 25 integers squared."
;; PS: The example was inspired by this article by C. Martin aka. "Uncle Bob": https://blog.cleancoder.com/uncle-bob/2019/08/22/WhyClojure.html
;; Some necessary background documentation first:
;;
;; range - "Returns a lazy seq of nums from start (inclusive) to end
;; (exclusive), by step, where start defaults to 0, step to 1, and end to
;; infinity." https://clojuredocs.org/clojure.core/range
@redbar0n
redbar0n / solidjs_syntax_suggestion_v2.js
Last active January 4, 2021 00:26
Draft 2 - Alternative SolidJS syntax suggestion.
import { useStore } from "../store";
import ArticlePreview from "./ArticlePreview";
export default ArticleList = props => {
const [{ token }, { unmakeFavorite, makeFavorite }] = useStore(),
handleClickFavorite = (article, e) => {
e.preventDefault();
article.favorited ? unmakeFavorite(slug) : makeFavorite(slug);
},
handlePage = (v, e) => {
@redbar0n
redbar0n / solidjs_syntax_suggestion.js
Last active January 2, 2021 03:04
Alternative SolidJS syntax suggestion
import { useStore } from "../store";
import ArticlePreview from "./ArticlePreview";
export default props => {
const [{ token }, { unmakeFavorite, makeFavorite }] = useStore(),
handleClickFavorite = (article, e) => {
e.preventDefault();
article.favorited ? unmakeFavorite(slug) : makeFavorite(slug);
},
handlePage = (v, e) => {
@redbar0n
redbar0n / do-not-comment-the-self-evident.js
Last active December 27, 2020 14:54
Do not comment the self-evident.
// ---
// The following code is from: https://github.com/dwyl/learn-tdd/blob/master/change.js
// Note that:
// - The JsDoc mentioning the params to getChange became out of sync after it apparently was refactored. Therefore, avoid such comments.
// - There are quite a few comments merely detailing what can be self-evidently / obviously read from the code itself. They don't reveal anything more (like intent/purpose) than the code itself, and just add noise, and can become out of sync, so avoid those.
var coins = [200, 100, 50, 20, 10, 5, 2, 1];
/**
* getChange accepts two parameters (totalPayable and cashPaid) and calculates
* the change in "coins" that needs to be returned.
@redbar0n
redbar0n / form_of_data_construct.md
Last active December 18, 2020 13:59
Is there any IDE that allows you to augment your code with the form of your data in real time?

Here's a crazy idea. Take the following simple example (in javascript):

    var m = [[1], [2], [3]];
    // ... a lot of code in between, perhaps m was passed through
    // several functions without TypeScript type declarations, so you might mistakenly
    // think m is a simple array like [1, 2, 3] at this point...
    var m2 = [];
    m.forEach(x => {
 m2.push(x); //# [[number], [number], ...] will be the form of m2 here
@redbar0n
redbar0n / formatted_duration.rb
Last active December 6, 2020 09:15
Ruby: Convert seconds to HH:MM:SS time notation without will resetting HH to 00 when crossing 24-hour day boundary.
# Will take as input a time in seconds (which is typically a result after subtracting two Time objects),
# and return the result in HH:MM:SS, but instead of resetting HH to 00 when the time exceeds a 24 hour period,
# it will increase it indefinitely. For other variations, see discussion here: https://gist.github.com/shunchu/3175001
def formatted_duration(total_seconds)
total_seconds = total_seconds.round # to avoid fractional seconds to potentially compound and mess up seconds, minutes and hours
hours = total_seconds / (60*60)
minutes = (total_seconds / 60) % 60 # the modulo operator (%) gives the remainder when leftside is divided by rightside
seconds = total_seconds % 60
[hours, minutes, seconds].map do |t|
# Right justify and pad with 0 until length is 2.
@redbar0n
redbar0n / conditional_formatting_with_criteria.gs
Last active July 26, 2020 04:12
Google Sheets - How to format cells through code in Google Sheets - Option 3: Using `SpreadsheetApp.newConditionalFormatRule().withCriteria` instead of using `.whenFormulaSatisfied()`
// Alternatively: Using withCriteria. Requires more boilerplate than using whenFormulaSatisfied.
// This code shows the same case above as a specific instance of the general case of using withCriteria,
// which has a bit more boilerplate setup.
function setRangeToRedBackground(cellsInA1Notation) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(cellsInA1Notation);
var customFormulaString = "=A1=1";
var criteria = SpreadsheetApp.BooleanCriteria.CUSTOM_FORMULA; // booleanConditionCriteriaType is an alternative name for this
var argsArray = [customFormulaString]; // booleanConditionCriteriaValues is an alternative name for this
// Could have used the convenience method whenFormulaSatisfied instead of withCriteria (withCriteria is probably what it uses internally).