Skip to content

Instantly share code, notes, and snippets.

// 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.
@dra1n
dra1n / storybook.md
Last active September 3, 2021 08:40

What it gives?

  • Visual documentation

    • Discoverability (Easier to find existing components. Potentially works for designers too. As a result, we are saving time because we are not doing the same thing again and again)
    • Regression (Breaking design or API changes will be reflected)
    • Describes component API (And edge cases)
    • Hints to intended usage
  • Faster design delivery (than having component embedded into the page)

  • Early design feedback
(ns logical-boys-girls.core
(:require [clojure.core.logic :as logic]
[clojure.core.logic.fd :as fd]))
(logic/run*
[q]
(let [students-count (fd/interval 1 100)]
(logic/fresh [boys girls h-girls q-girls h-boys q-boys]
;; boring part
;; every lvar belongs to number from interval
* The Reasoned Schemer
* Seven More Languages in Seven Weeks, Brues Tate, Chapter 6. miniKanren
* Joy Of Cloure, Michael Fogus, Chapter 16. Thinking Programs
* https://www.youtube.com/watch?v=lzCIyvFgUVk