Skip to content

Instantly share code, notes, and snippets.

@aSapien
aSapien / printMatrixInSpiral.js
Last active August 31, 2016 06:44
Print a square matrix (2x2, 4x4, 16x16, etc) in a clockwise spiral from outside in
var mat = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
];
var DIRECTIONS = {
FORWARD: 1,
BACKWARD: -1
@aSapien
aSapien / compose.js
Last active October 8, 2017 16:31
A small utility function to compose functions, lazily evaluated
function compose(func, ...funcs) {
return function(...args) {
return funcs.length > 0
? compose(...funcs)(func(...args))
: func(...args)
}
}
// Or, in a more compact version:
const compose = (func, ...funcs) => (...args) =>

Keybase proof

I hereby claim:

  • I am asapien on github.
  • I am sapienz (https://keybase.io/sapienz) on keybase.
  • I have a public key ASDqalDydfkpzj6b7eR9ndIeP288fvART4m63ZnIB65s0Ao

To claim this, I am signing this object:

### Feature development Planning Workflow<sup>Functional approach</sup>
In FP, impurity (aka Effects), is handled in a Monadic approach.
This approach enables developers to focus on pure logic implementation and testing, keeping the "Side Effects" on the envelope.
I would like to give a shot to this approach to development planning and time estimations.
The software we are developing is ususlly not self contained. We need to integrate with other software, APIs, Operating Systems, etc...
All those "effectful" aspects, in practice, take an *unknown* amount of time to complete.
This makes our estimation prone to errors, too "flexible" or unprecise, or in some cases "way off".
@aSapien
aSapien / parseq.js
Last active December 3, 2022 13:05
Parallel / Sequential execution control flow with Promises in JS #javascript
export const parallel = (...fs) => sequential(
() => fs.map(f => f()),
Promise.all.bind(Promise));
export const sequential = (f, ...fs) => val => Promise.resolve()
.then(() => f(val))
.then(fs.length ? sequential(...fs) : id => id);
@aSapien
aSapien / monorepo-sparse.md
Last active August 13, 2018 07:26
[Working with a monorepo in isolated subrepos] A way to mimic a multi-repo git environment inside a single monorepo #monorepo #git #trick

Clone a monorepo as isolated subrepos

Motivation & Goal

When working on a monorepo, we often create uncommitted changes in multiple "subrepos", which turns into a mess when preparing a commit, when pulling changes from remote, etc...

Git allows us to create a sparse-clone of the monorepo, containing only the subrepo that we are interested in, yet keeping the integrity and functions of a VCS.

For example, we have a monorepo with the following structure (computer@user:~$ tree monorepo):

@aSapien
aSapien / code golf.js
Last active August 13, 2018 07:24
[Code Golf Game] The shortest code wins (according to the instructions here: https://grammarly.ai/code-golf/) #challange #game #golf #code
// version 1
z = (k, a, b, c) => (
i = r => k in r[0] ? r[0][k] : r[1],
d = j => Intl.DateTimeFormat('en', { month: 'long' }).format(new Date().setMonth(j)),
f = [[...[,,,,].fill(0.1) ,3.6], 1],
g = [[1,10,5,9,7,3,7,8], 6],
h = [[1,1,2,2,2,2,5,3,3,1], k % 2],
`\nJDK ${ a.toFixed(k > 4 ? 0 : 1) } ${k < 11 ? 'was' : 'will be' } released in ${d(b)} ${c}` + (41 < k ? '' : z(k+1, a + i(f), b + i(g), c + i(h)))
)
@aSapien
aSapien / # octave - 2018-08-11_19-46-09.txt
Created August 11, 2018 16:57
octave on macOS 10.13.6 - Homebrew build logs
Homebrew build logs for octave on macOS 10.13.6
Build date: 2018-08-11 19:46:09
import scalaz.zio._
trait Tap[-E1, +E2] {
def apply[R, E >: E2 <: E1, A](effect: ZIO[R, E, A]): ZIO[R, E, A]
}
object Tap {
type Percentage = Int
def make[E1, E2](errBound: Percentage,
@aSapien
aSapien / deepTransform.ts
Created December 9, 2019 13:49
transform a deep object tree
import _ from 'lodash'
// accumulator, value, key, object)
export const transformDeep = (root: object, iteratee: (accumulator: object, nodeOrLeaf: any, key: string, parent: object) => void): object => {
const deepIteratee = (deepAccumulator: object, deepNodeOrLeaf: any, deepKey: string, deepParent: object): void => {
iteratee(deepAccumulator, deepNodeOrLeaf, deepKey, deepParent) //run potential mutation
if (_.isPlainObject(deepNodeOrLeaf) || _.isArray(deepNodeOrLeaf)) { // run deep (maybe shouldRunDeep with eq check?)