Skip to content

Instantly share code, notes, and snippets.

View adleroliveira's full-sized avatar

Adler Oliveira adleroliveira

View GitHub Profile
@adleroliveira
adleroliveira / lazy-list.js
Created September 1, 2018 17:57 — forked from gvergnaud/lazy-list.js
Lazy List, implemented with es6 generators
/* ----------------------------------------- *
Lazy List Implementation
* ----------------------------------------- */
// Haskell-like infinite List, implemented with es6 generators
// Lazyness lets you do crazy stuff like
List.range(0, Infinity)
.drop(1000)
.map(n => -n)
const waterVolume = a => {
const max = Math.max(...a)
let sum = 0
for (let i = max; i >0; i--) {
let started = false, tmpSum = 0
for (let j = 0; j < a.length; j++) {
if (a[j] >= i) {
if (!started) {
started = true
} else {
@adleroliveira
adleroliveira / morse_binary_hex_encoding.js
Created December 27, 2017 16:06
Message encoder: Morse, binary and hex
const encode = msg => {
const abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const morsecode =[
'.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....',
'..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.',
'--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..'
]
const words = msg.toUpperCase().split(' ')
@adleroliveira
adleroliveira / transducer.js
Last active August 21, 2017 18:34
transducer from scratch in JS, just to learn and understand how it works.
// Function Composition
const identity = x => x
const compose = (f, g) => x => f(g(x))
const composer = fnArr => fnArr.reduce(compose, identity)
// Map implemented in terms of reduce
const mmap = fn => reducer => (acc, val) => reducer(acc, fn(val))
// Filter implemented in terms of reduce
const mfilter = fn => reducer => (acc, val) => fn(val) ? reducer(acc, val) : acc
const findAnagramIndexes = (w1, w2) => {
let result = [];
let control = [];
for(let i = 0; i < w1.length; i++){
if(control[i] === undefined) control[i] = w2.split('');
for(let j = 0; j < control.length; j++) {
if(control[j] === null) continue;
if(Array.isArray(control[j]) && control[j].length === 0) continue;
let indexOfLetter = control[j].indexOf(w1[i]);
@adleroliveira
adleroliveira / max_square_sum.js
Last active July 4, 2017 19:47
max square sum problem (functional)
const neighbours = (pos, transf) => {
const x = pos[0]
const y = pos[1]
return [[x-1, y-1],[x, y-1],[x+1, y-1],[x-1, y],[x,y],[x+1, y],[x-1, y+1],[x, y+1],[x+1, y+1]].map(transf)
}
const maxSumRect = (rows, cols, nums) => {
const revVirtMatrix = pos => [parseInt(pos / cols), pos % cols]
const virtualMatrix = pos => (pos[0] * cols + pos[1])
return nums.reduce((max, val, idx) => {
@adleroliveira
adleroliveira / run phoenix on amazon linux.sh
Created February 17, 2017 12:12 — forked from eikes/run phoenix on amazon linux.sh
run phoenix on amazon linux
# app deps
sudo yum install git
# erlang deps
sudo yum groupinstall "Development Tools"
sudo yum install ncurses-devel
# erlang
wget http://www.erlang.org/download/otp_src_18.1.tar.gz
tar -zxvf otp_src_18.1.tar.gz
@adleroliveira
adleroliveira / recur.js
Created January 19, 2017 20:45 — forked from CreaturePhil/recur.js
A Million Ways to Fold in JS Notes
// from http://forwardjs.com/university/a-million-ways-to-fold-in-js
'use strict'
const first = (xs) => xs[0]
const rest = (xs) => xs.slice(1)
const concat = (xs1, xs2) => xs1.concat(xs2)
// recursion
const sum = (xs) => {
@adleroliveira
adleroliveira / cis194-week2.hs
Last active June 29, 2016 18:27
Mastermind solver (CIS194 Week 2)
import Data.List
data Peg = Red | Green | Blue | Yellow | Orange | Purple deriving (Show, Eq, Ord)
type Code = [Peg]
data Move = Move Code Int Int deriving (Show, Eq)
colors :: [Peg]
colors = [Red, Green, Blue, Yellow, Orange, Purple]
exactMatches :: (Eq a) => [a] -> [a] -> Int
let edificios = [
[2, 9, 10],
[2, 4, 12],
[3, 7, 15],
[5, 12, 12],
[15, 20, 10],
[19, 24, 8]
]
function skyline(buildings){