Skip to content

Instantly share code, notes, and snippets.

View betafcc's full-sized avatar
🎯
Focusing

Brendon betafcc

🎯
Focusing
View GitHub Profile
@betafcc
betafcc / obj-map.js
Created December 8, 2017 18:27
Mapping over plain objects in JS
// objMap(x => x*x, {a: 2, b: 3}) -> {a: 4, b: 9}
const objMap = (f, obj) =>
Object
.entries(obj)
.reduce((acc, [k, v]) =>
(acc[k] = f(v, k, obj), acc)
, {});
// Need this function or equivalent to deep map
@betafcc
betafcc / groupby.py
Created February 17, 2018 21:50
Simple python groupby
from functools import reduce
def groupby(f, it):
def reducer(acc, n):
key = f(n)
if key in acc:
acc[key].append(n)
else:
acc[key] = [n]
@betafcc
betafcc / screenshot_all.sh
Last active August 22, 2018 08:00
Take screenshot from all urls in a file
npm i -g slugify-cli
npm i -g puppeteer-screenshot-cli
print_all() {
urls="$1"
output="$2"
mkdir -p "${output}"
cat "${urls}" |
while read line
@betafcc
betafcc / __main__.py
Last active September 4, 2018 13:16
Tiny regex based tokenizer in python
from .lexer import Lexer
code = '''
(define (fib x)
(if (lt x 2)
x
(add (fib (sub x 1))
(fib (sub x 2)))))
'''
import asyncio
from random import randint
async def get(url):
# simula a demora na request
await asyncio.sleep(1)
# simula uma resposta duma realistica função 'get'
return bool(randint(0, 1))
@betafcc
betafcc / data.json
Last active November 18, 2018 13:45
Vega donut chart with centered rotated labels https://bl.ocks.org/betafcc/3125c2287224b86b98e83ae2ebdd8fa0
[
{"letter": "A", "number": 4},
{"letter": "B", "number": 6},
{"letter": "C", "number": 10},
{"letter": "D", "number": 3},
{"letter": "E", "number": 7},
{"letter": "F", "number": 8}
]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Vega Lite pie chart</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/3.23.1/vega-embed.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/4.3.0/vega.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/2.6.0/vega-lite.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/3.23.1/vega-embed.min.js"></script>
[
{"sign":"Aries","count":2328,"sum_of_votes":39017358},
{"sign":"Taurus","count":2281,"sum_of_votes":51056695},
{"sign":"Gemini","count":2301,"sum_of_votes":42472945},
{"sign":"Cancer","count":2230,"sum_of_votes":41106817},
{"sign":"Leo","count":2297,"sum_of_votes":35133564},
{"sign":"Virgo","count":2287,"sum_of_votes":26846077},
{"sign":"Libra","count":2292,"sum_of_votes":41968051},
{"sign":"Scorpio","count":2167,"sum_of_votes":56140744},
{"sign":"Sagittarius","count":2019,"sum_of_votes":32702931},
@betafcc
betafcc / infix.py
Created December 17, 2018 18:28
Dead simple infix function application support in python, with sections partial application also
class infix:
"""
Makes a function 'infix' eg:
>>> @infix
... def power(x, n):
... return x ** n
>>> 2 |power| 3
8
Maybe::Just() {
printf 'Maybe::Just(%s)' "${1}"
}
Maybe::Nothing() {
printf 'Maybe::Nothing'
}
Maybe::map() {
case "${2}" in