Skip to content

Instantly share code, notes, and snippets.

View mrnkr's full-sized avatar
🏠
Working from home

Alvaro Nicoli mrnkr

🏠
Working from home
View GitHub Profile
@mrnkr
mrnkr / auth.ts
Created February 10, 2019 21:20
Redux middleware to exchange a refresh token prior to any request if the access token is expired
async function refresh(dispatch: Dispatch, getState: () => MyState): Promise<void> {
const { auth: { authenticated, refresh_token: refresh } } = getState();
if (!authenticated)
return;
try {
const { access_token, refresh_token, token_type } = await (async () => {
const res = await fetch(`${baseUrl}/oauth2/token`, {
method: 'POST',
@mrnkr
mrnkr / Cell.scss
Created February 10, 2019 19:22
React list cell implementation with buttons hidden below the cell content when on mobile (alla Apple Mail on iOS)
.my-cell {
position: relative;
height: 64px;
width: 100%;
margin-top: 1rem;
margin-bottom: 1rem;
.my-cell-content {
position: absolute;
top: 0;
@mrnkr
mrnkr / Gallery.tsx
Last active February 10, 2019 19:20
React carousel implemented using css transformations and hammerjs
@mrnkr
mrnkr / LazyImage.tsx
Created February 3, 2019 22:34
React component which renders a blurred preview of an image until it enters the viewport (uses intersection observer)
import React, { Component, CSSProperties } from 'react';
export interface Props {
'className'?: string;
'style'?: CSSProperties;
'src': string;
'data-src': string;
[key: string]: any;
}
@mrnkr
mrnkr / List.tsx
Last active February 10, 2019 19:18
React List component which implements infinite scroll automatically with intersection observer
import React, { Component } from 'react';
import get from 'lodash/get';
import EmptyListPlaceholder from './list/EmptyListPlaceholder';
import Loading from './list/Loading';
import './list/List.scss';
interface Props<T> {
className?: string;
grid?: boolean;
@mrnkr
mrnkr / ArrayContainsSubArray.swift
Created December 5, 2018 23:04
Dynamic programing exercise - Recieve two arrays and check if the second one contains the first (like the longest common subsequence)
import Foundation
// Returns true iif a in contained by b :)
// This is a modification of the longest common subsequence problem
func contains(a: [String], b: [String]) -> Bool {
var res: [[Int]] = Array.init(repeating: Array.init(repeating: 0, count: b.count), count: a.count)
for i in 0...a.count - 1 {
for j in 0...b.count - 1 {
if a[i] == b[j] {
@mrnkr
mrnkr / Pedrito.swift
Created December 5, 2018 18:18
Backtracking/Dynamic Programming exercise - Find out the minimum number of throws needed to know which floor to throw a marker from in order to know whether it breaks
import Foundation
func max(a: Int, b: Int) -> Int {
return a > b ? a : b
}
// Assume this one works just fine, since I copied it
func pedritoBt(markers: Int, floors: Int) -> Int {
if floors <= 1 {
return floors
@mrnkr
mrnkr / MalaMiaDisculpame.swift
Last active December 5, 2018 18:54
Backtracking exercise - You just got home and it's late, get to your bedroom making as little noise as humanly possible (alla home invasion in GTA SA)
import Foundation
func calcNoise(standingOn: Character) -> Int {
switch standingOn {
case "A":
return 1
case "S":
return 3
case "H":
return 5
@mrnkr
mrnkr / AyudameNey.swift
Created December 5, 2018 18:10
Dynamic Programming exercise - Help someone on the run from the cops to get to the border stopping in as few gas stations as possible. When you stop at one you drop all your gas cause your car is a bit messed up :P
import Foundation
func min(array: [Int]) -> Int {
var ret = Int.max;
for i in 0...array.count - 1 {
if array[i] < ret {
ret = array[i]
}
}
@mrnkr
mrnkr / Necklace.swift
Created December 5, 2018 14:52
Dynamic programming exercise - Build a necklace with the greatest value without surpassing the maximum allowed weight (modification of the 0/1 knapsack problem)
import Foundation
func max(a: Int, b: Int) -> Int {
return a > b ? a : b
}
func necklace(pearls: [[Int]]) -> [[Int]] {
var res = Array.init(repeating: Array.init(repeating: 0, count: 151), count: pearls.count)
var ret: [[Int]] = []