Skip to content

Instantly share code, notes, and snippets.

Command Line Navigation Cheat Sheet

Alt key may need to be Esc on macOS depending on your terminal settings.

Cursor Movement

Shortcut Action
Ctrl+A Move to beginning of line
Ctrl+E Move to end of line
@dra1n
dra1n / 01-dhall_preview.md
Last active August 20, 2024 08:52
Dhall talk
@dra1n
dra1n / knight.ts
Last active December 17, 2024 11:00
const range = (start: number, end: number) =>
Array.from({ length: end - start + 1 }, (v, k) => k + start);
/* Monad */
const ret = <T>(x: T): T[] => [x];
const bind = <T>(xs: T[], f: (v: T) => T[]): T[] => xs.map(f).flat();
const fail = <T>(_: T): T[] => [];
/* Knight problem */
type KnightPos = [number, number];
// user module
const receive = (_user, sender, message) => {
console.log(`${new Date().toLocaleString()} [${sender}]: ${message}`);
};
// chat room module
const createChatRoom = (users = []) => ({
users
});
class ChatRoom {
constructor(users = []) {
this.users = users;
}
addUser(user) {
this.users.push(user);
}
sendMessage(user, message) {
const execute = (command, ...args) => command(...args);
function PlaceOrderCommand(order, id) {
return orders => {
const result = [...orders, { id }];
console.log(`You have successfully ordered ${order} (${id})`);
return { success: true, result };
};
}
(ns number-puzzles.imbaru
(:refer-clojure :exclude [==]) ;; Prevent ns conflict
(:require [clojure.core.logic :refer :all]))
(def answers (run* [q]
(fresh [r1 r2 r3 r4
r5 r6 r7 r8
r9 r10 r11 r12
r13 r14 r15 r16]

Teaching New Toys Old Tricks

What's in the toolbox?

  • createSlice
  • createAction
  • createAsyncThunk
  • configureStore

What's the slice?

Where we left

  • UI should be an afterthought
  • Write code that allows to sepearate business logic frow UI.
  • Write code that allows to inject any hard-to-test, too-verbose, want-this-mocked stuff logic.

Is redux a good tool for impelmenting these ideas?

  • It has special place for handling business logic, called middleware.
  • Popular middleware have a mechanism for context injection. If you are writing your own custom middleware, then you probably should think about adding this too.

UI should be an afterthought

What this logic separation gives?

  • Better testability via the fewer levels of indirection. To trigger business processes you call methods or invoke functions, not clicking buttons.
  • Better testability via DI in some form. In React code jest.mock is the only "unobtrusive" way to replace dependencies.
  • Discoverability. Business logic isn't scattered across a bunch of components.
  • Reusability. Sometimes you want to reuse only business logic and not the UI tied to it.
  • Readability. It is easier to reason about business logic.