Mark Grealish

the life and code of a crazy cat man


Coding Zen in all the strange places

in code

I continue to return to Zen as a way to reach peace with myself. Zen encourages introspection and meditation alongside expressive acts such as calligraphy. The core of Zen calligraphy, such that I understand it, is to cultivate the beginner’s mind. The expert mind is packed with presumption; while an expert mind has a powerful ability to categorize a problem, such that it can immediately a ready-made solution, this procedural reduction prevents it from naive consideration of the actual problem as presented.

This is evident at work: I think “Aha! This task is in fact this problem with this solution!,” and then I solve it. I get faster, but I don’t grow: practice reinforces dogma. Practice strengthens your current paradigm inhibits personal development.

I bought If Hemingway Wrote JavaSciript by Angus Coll from a Humble Bundle a few months ago. The book’s comparative approaches to the same simple problem inspired me to fuse Zen na├»vety and functional JavaScript. Michael Fogus’ approach in Functional JavaScript underlies my recent series of JavaScript snippets.

Snippets should be taken at face value: exercises in a base level of code that encapsulates knowledge and naive practice. What is a map? What is an elegant way to find factorials? The goal is to improve my ability to problem-solve with JavaScript without preconception.

function range(size) {
    return [...Array(size + 1).keys()].slice(1);
}

function reduce(arr, sum, callback) {
    return !arr.length ? sum : reduce(arr.slice(1), callback(arr[0], sum), callback);
}

function adder(number, sum) {
    return number + sum;
}

let numbers = range(10);

reduce(numbers, 0, adder); // 55

There’s quite a of Project Euler in this series, and I think this series will segue into attempts on different problems. For now, its enjoyable and I intend to continue.


each()

in code

Simple recursive array each method.

function each(array, callback) {
    if (array.length) {
        callback(array[0]);
        each(array.slice(1), callback);
    }
}

factorial()

in code

Recursive factorial in one line.

function factorial(number) {
    return number < 2 ? 1 : number * factorial(number - 1);
}

Lough Glenicmurrin

in me

I close my eyes now, and open them two years ago. The sun is on my face. There are only two colours in the world, green and blue.

Water laps, insects buzz, and the wind sighs soft in my ear. The only things I see are the lake, the mountains, and my friends.

Eadaoin’s by my side, quiet; Aafke’s at the end of the stone pier across from us.

We’re all happy. Today is a good day.

And I’m here, now in this hush.

There’s no past, no future. There isn’t a last week or tomorrow, just this perfect eternal present.

We go back to the car and drive on.


Dump (and pretty print) all function arguments to the console

in code

A Lodash-infused variant of this function is my lifeline at work (ALL the ES6 syntaxes!):

function dump() {
    [...arguments].forEach(arg => {
        console.log(JSON.stringify(arg, null, '\t'));
    });
}

For example:

dump({ messages: { hello: 'World!', goodbye: 'Moon!' }}, ['Apples', 'Oranges', 'Peaches', 'Pears']);

{
    "messages": {
        "hello": "World!",
        "goodbye": "Moon!"
    }
}

[
    "Apples",
    "Oranges",
    "Peaches",
    "Pears"
]

with recursion

in code

today i solved a problem with recursion today i solved a problem with today i solved a problem today i today solved i recursion i today with^C^C^C