Skip to content

Instantly share code, notes, and snippets.

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

Davide Cavaliere davidecavaliere

🏠
Working from home
View GitHub Profile
@davidecavaliere
davidecavaliere / gist:16b45bd596fe4b2aac68
Created May 9, 2014 15:16
Json Formatter for Gedit
#! /usr/bin/env python
import json
import sys
j = json.load(sys.stdin)
print json.dumps(j, sort_keys=True, indent=2)
@davidecavaliere
davidecavaliere / .gitconfig
Created August 6, 2014 10:44
.gitconfig - aliases
[alias]
today = log --since=midnight --author='Davide Cavaliere' --oneline
history = log --graph --pretty=format:'%Cred%h%Creset %d -%Cblue %s%Creset %Cgreen(%ar) %Cblue<%an>%Creset' --abbrev-commit --date=relative
ci = commit
st = status
@davidecavaliere
davidecavaliere / gist:52ae9037c085d5f6bdbd
Created October 15, 2014 10:01
non blocking i/o php socket
$urls = ['www.amazon.com',...];
foreach ($url as $ip => $url) {
// create a stream that returns immediately
// STREAM_CLIENT_CONNECT _MUST_ be passed
// STREAM_CLIENT_ASYNC_CONNECT says create connection
// asyncrhronously
$socket = stream_socket_client("tcp://$ip:80", $errno, $errstr, 0,
STREAM_CLIENT_CONNECT, STREAM_CLIENT_ASYNC_CONNECT);
var start = process.hrtime();
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(X, A) {
// write your code in JavaScript (Node.js 0.12)
var isEven = A.length % 2 === 0;
import itertools;
permutations = itertools.permutations([100,250,1000, 1200]);
times = []
for it in permutations :
print it
presum = 0;
roundTrip = []
/*
For more info please head to https://github.com/davidecavaliere/flatten
*/
function* flatten([head, ...tail]) {
if (Array.isArray(head)) {
yield* flatten(head);
} else {
yield head;
@Injectable()
export class UserService {
constructor(private _client: HttpClient) {}
public findAll(id: number) {
return this._client.get(`https://reqres.in/api/users?page=${id}`);
}
}
@Injectable()
export class UserService {
private cached$: ReplaySubject<any> = new ReplaySubject(1, 2500);
constructor(private _client: HttpClient) {}
public findAll(id): Observable<any> {
const req = this._client.get(`https://reqres.in/api/users?page=${id}`).pipe(
tap((data) => {
this.cached$.next(data);
export interface CacheOptions {
ttl: number;
}
export function Cache(options: CacheOptions) {
return (target: any, propertyKey: string, descriptor) => {
const originalFunction = descriptor.value;
target[`${propertyKey}_cached`] = new ReplaySubject(1, options.ttl);
descriptor.value = function(…args) {
const req = originalFunction.apply(this, args).pipe(
@Injectable()
export class UserService {
constructor(private _client: HttpClient) {}
@Cache({
ttl: 2500
})
public findAll(id): Observable<any> {
return this._client.get(`https://reqres.in/api/users?page=${id}`)
}