Skip to content

Instantly share code, notes, and snippets.

View rockneurotiko's full-sized avatar
🐈
When in doubt flat out

Rock Neurotiko rockneurotiko

🐈
When in doubt flat out
View GitHub Profile
defmodule StrongParams do
def extract(params, allowed_fields, atomize \\ false)
def extract(params, allowed_fields, atomize) when not is_list(allowed_fields),
do: extract(params, [allowed_fields], atomize)
def extract(params, allowed_fields, atomize)
when is_map(params) and is_list(allowed_fields) do
Enum.reduce(allowed_fields, %{}, fn field, acc ->
case extract_field(field, params, atomize) do
@rockneurotiko
rockneurotiko / dictsave.py
Created March 29, 2016 16:20
Class that allows to save and load from json
import json
class DictSave(dict):
def save(self, fpath):
with open(fpath, 'w') as f:
json.dump(self, f)
def load(self, fpath):
with open(fpath, 'r') as f:
self = json.load(f)
return self
// Part one
println(io.StdIn.readLine.foldLeft(0) {
case (cur, '(') => cur + 1
case (cur, ')') => cur - 1
case (cur, _) => cur
})
// Part two
println(io.StdIn.readLine.scanLeft(0) {
case (cur, '(') => cur + 1
@rockneurotiko
rockneurotiko / FIDASHRequests.js
Last active January 18, 2016 10:09
FIDASH Requests
var FIDASHRequests = (function() {
"use strict";
// http://stackoverflow.com/questions/1714786/querystring-encoding-of-a-javascript-object
var serialize = function serialize(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
@rockneurotiko
rockneurotiko / keybase.md
Created January 20, 2015 14:41
keybase.md

Keybase proof

I hereby claim:

  • I am rockneurotiko on github.
  • I am rockneurotiko (https://keybase.io/rockneurotiko) on keybase.
  • I have a public key whose fingerprint is 4946 D290 EC2E 817F F126 E0B7 B6AE 2B3F BD58 B1CD

To claim this, I am signing this object:

@rockneurotiko
rockneurotiko / tail_rec_print.py
Created September 17, 2014 06:18
tail_rec_print
import functools
def tail_call(func):
def _optimize_partial(*args, **kwargs):
old_reference = func.__globals__[func.__name__]
func.__globals__[func.__name__] = functools.partial(functools.partial, func)
to_execute = functools.partial(func, *args, **kwargs)
while isinstance(to_execute, functools.partial):
from typing import List, Tuple
def calc_overload(out: List[int], ins: List[int]) -> List[int]:
out1, out2 = out[0], out[1]
in1, in2 = ins[0], ins[1]
if out1 <= in1 < out2 < in2: # Case right overlap: [1, 50] with [32, 60]
return [out1, in2]
if out1 <= in1 < out2 >= in2: # Case inside: [1, 50] x [20, 40]
return [out1, out2]
if in1 < out1 < in2: # left overlap and full overlap: 1) [20, 30] x [10,25] and [20, 30] x [10, 40]
@rockneurotiko
rockneurotiko / TailRecPrime.scala
Last active August 29, 2015 14:02
Eratosthenes cribe in Scala and Python using tailrec
import scala.annotation.tailrec
object criba {
def basic_get_primes(n: Int): List[Int] = {
val primes = List.range(2, n+1)
var max = Math.sqrt(n).toInt
@tailrec
def tal_rec(now: Int, prim: List[Int]): List[Int] = {
if(now >= max) prim