Skip to content

Instantly share code, notes, and snippets.

View fxg42's full-sized avatar

François-Xavier Guillemette fxg42

View GitHub Profile
@fxg42
fxg42 / maybe.js
Last active March 9, 2023 18:44
Do-notation for the Maybe monad implemented in JavaScript with a generator function
//
// Simplified (and mostly incorrect) definition of the Maybe monad.
//
class Maybe { }
class Nothing extends Maybe { }
class Just extends Maybe {
constructor(v) {
@fxg42
fxg42 / api
Last active April 9, 2021 11:41
Bash script used to call HTTP/JSON api
#!/usr/bin/env bash
CURL=$(which curl)
if [[ ! -x "${CURL}" ]]; then
echo "Missing dependency. Please install curl (https://formulae.brew.sh/formula/curl):
$ brew install curl" 1>&2
exit 1
fi
readonly PROGRAM=$(basename -- $0)
@fxg42
fxg42 / index.js
Last active July 31, 2019 16:46
Create key from UTF-8 string
const { asKey } = require('./utils');
const text = {
"520407a476092ac19c671e1e75d052bd": "i18n!",
};
const gettext = (str) => text[asKey(str)] || str
console.log(gettext("Iлtèrnåtïonɑlíƶatï߀ԉ")); //=> "i18n!"
console.log(gettext("Autre chose!")); //=> "Autre chose!"
@fxg42
fxg42 / github-mvc-es209.js
Created July 17, 2019 15:39
INF5190 - Exemple d'une application MVC
class Model {
constructor() {
this.userCollection = [ ]
this.selectedUser = null
this.listeners = {
'selectedUser': [ ],
'userCollection': [ ]
}
}
@fxg42
fxg42 / connect-jmx.jsh
Last active April 2, 2018 18:15
JShell script to connect to JMX server
import javax.management.*
import javax.management.remote.*
var serverUrl = "service:jmx:rmi:///jndi/rmi://0.0.0.0:9090/jmxrmi"
var connection = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl)).getMBeanServerConnection()
var objectName = new ObjectName("my-domain:type=my-bean-type,name=my-bean-name")
var pingResult = (int) connection.invoke(objectName, "ping", null, null) // Assumes an `int ping()` method that returns the error code.
/exit pingResult
@fxg42
fxg42 / split.py
Created October 14, 2017 12:58
Split a youtube soundtrack into multiple files. Requires youtube-dl, ffmpeg and python3
import sys
import re
import subprocess
import os
#
# Usage:
# $ youtube-dl -f 140 https://www.youtube.com/watch?v=Iq0xfewozjw
# $ python3 split.py <input file name>.m4a tracks.txt
#
@fxg42
fxg42 / index.js
Last active May 8, 2017 14:15
Rate limiting a Promise returning function
// heavily inspired by https://github.com/JMPerez/promise-throttle
const throttle = (f, rate) => {
// rate is # of calls/sec
const period = 1/rate * 1000 // minimum elapsed time (in ms) between calls (sec/call * 1000ms/sec)
const queue = [ ] // queued function calls
let last = 0 // last time `f` was called
const applyNext = () => {
if (queue.length === 0) return
@fxg42
fxg42 / flow-4.png
Last active September 9, 2021 19:01
8 implementations of the same pipeline in JavaScript
flow-4.png
@fxg42
fxg42 / index.js
Last active March 30, 2017 12:38
Conditional selects...
import React from 'react'
import ReactDOM from 'react-dom'
import thunk from 'redux-thunk'
import logger from 'redux-logger'
import { Provider, connect } from 'react-redux'
import { createStore, applyMiddleware } from 'redux'
import Immutable from 'immutable'
import './index.css'
//
@fxg42
fxg42 / breaker.js
Last active February 19, 2017 23:04
CircuitBreaker with Promises
const handleResolve = (breaker) => (value) => {
breaker.state = closed
breaker.errorCount = 0
return Promise.resolve(value)
}
const handleReject = (breaker) => (reason) => {
breaker.errorCount += 1
breaker.state = (breaker.errorCount >= breaker.maxErrorCount ? opened : closed)
if (breaker.state === opened) setTimeout(() => breaker.state = halfOpened, breaker.resetTimeout)