Skip to content

Instantly share code, notes, and snippets.

View Killavus's full-sized avatar

Marcin Grzywaczewski Killavus

  • Wrocław, Poland
View GitHub Profile
@Killavus
Killavus / ugly_code_refactoring.coffee
Last active March 14, 2022 17:52
Example of possible refactoring of badly written code - loading photos and making it grayed after click
Photos = {}
class Photos.App
constructor: ->
@gui = new Photos.Gui($("#photos-list"))
@backend = new Photos.Backend()
start: =>
@backend.fetchPhotos()
.done(
function asyncOnce(promiseFn) {
let data = null;
let promise = null;
return function returnOnce(...args) {
if (!promise) {
promise = promiseFn(...args).then((promiseReturn) => { data = promiseReturn; })
}
return data;
@Killavus
Killavus / productsListApp.js
Created April 16, 2016 18:52
Products List App example in Hexagonal Architecture - two features for the same UI adapter!
// Here are objects that provides use case and ports:
import ProcessingCustomerCart from "./ProcessingCustomerCart";
import RequestingCustomerCare from "./RequestingCustomerCare";
// Adapters:
import ProductsListUI from "./ProductsListUI";
import AppRouter from "./AppRouter";
import ErrorsUI from "./ErrorsUI";
import CartBackend from "./CartBackend";
import CustomerCareBackend from "./CustomerCareBackend"
@Killavus
Killavus / processingCustomerCartPorts.js
Last active April 26, 2019 10:29
Ports example in hexagonal architecture
const ProcessingCustomerPorts = (useCase, userInterface, router, eventBus, errorHandler) => {
// inbound messages - here event bus approach is used.
eventBus.on('addItemToCart', item => {
useCase.addItemToCart(item);
});
eventBus.on('cartLoaded', items => {
items.forEach(item => {
useCase.addItemToCart(item);
});
@Killavus
Killavus / processingCartUseCase.js
Created April 16, 2016 17:34
A simple use case in hexagonal architecture.
const MAX_TOTAL_ITEMS_IN_CART = 10;
const ProcessingCustomerCart = ports => {
let cartItems = [];
return {
addItemToCart(item) {
const cartItemsWithAddedItem = cartItems.concat([item]);
if (MAX_TOTAL_ITEMS_IN_CART < cartItemsWithAddedItem.length) {
ports.cartErrorHappened("Too many items in cart. Please proceed to checkout.");
@Killavus
Killavus / ex.rs
Last active December 11, 2017 21:26
// ...snip
pub enum ClaimResult<'a> {
Claimed(&'a SpawnClaim),
ClaimedAlready(&'a SpawnClaim),
UnknownSpawn,
}
// ...snip
impl ClaimList {
// ...snip
for(var x = 1; x < 20; ++x) {
var label = '';
if (x % 3 === 0) { label += 'Julia'; }
if (x % 5 === 0) { label += 'James'; }
console.log(label.length > 0 ? label : x);
}
pub enum Command<'a> {
ClaimSpawn { spawn_name: &'a str, message: Message },
ClaimedList { message: Message },
EstablishState { server_id: ServerId },
Unknown,
}
impl<'a> Command<'a> {
fn from_message(message: Message) -> Self {
let content = message.content.trim();
struct TreeNode<T>
where T: Ord
{
value: T,
left: Option<Box<TreeNode<T>>>,
right: Option<Box<TreeNode<T>>>,
}
impl TreeNode<T>
const reduceCompose = (f, g) => x => g(x, f(x))
const reduceComposeArr = (...fns) => fns.reduce(reduceCompose, (x, y) => y)
const isNumber = (x, errors = {}) => Number.isNaN(parseInt(x, 10)) ? Object.assign({}, errors, { number: true }) : errors;
undefined
const length = (z) => (x, errors = {}) => (x && x.length < z) ? Object.assign({}, errors, { length: true }) : errors;
const presence = (x, errors = {}) => x === undefined ? Object.assign({}, errors, { presence: true }) : errors;
reduceComposeArr(presence, length(3), isNumber)('123')