Skip to content

Instantly share code, notes, and snippets.

View hdvianna's full-sized avatar
🎯
Focusing

hdvianna hdvianna

🎯
Focusing
View GitHub Profile
@hdvianna
hdvianna / closure-test.js
Created April 2, 2019 19:47
JS Closure Examples
/**
* Bind com let i = 0
* Bind com let i = 1
* Bind com let i = 2
* Bind com let i = 3
* Bind com let i = 4
*/
for(let i = 0; i < 5; i++) {
setTimeout(function(){
console.log('Bind com let i = '.concat(i));
@hdvianna
hdvianna / compare-files.php
Created April 2, 2019 19:48
PHP Dummy File Comparator
<?php
define("STOP_WORDS", ["the","of","in","and","a","to","on","by","when","or","with","that","this","also","from","an","it","as","these","."]);
function iteratesWords($lineA, $lineB) {
$wordsA = preg_split("/[\s\.\,\?\;\(\)\-\%\{\}]/", $lineA);
$wordsB = preg_split("/[\s\.\,\?\;\(\)\-\%\{\}]/", $lineB);
$words = [];
foreach($wordsA as $wordA) {
foreach($wordsB as $wordB) {
@hdvianna
hdvianna / spiral.js
Created April 2, 2019 19:50
JS Spiral Matrix Iterator
function border(matrix, row, col, len) {
var items=[];
var endCol = col - 1;
var endRow = row;
//top
for(;col < len; col++) {
items.push(matrix[row][col]);
@hdvianna
hdvianna / deep-cloning.js
Created May 28, 2019 19:41
A pattern for deep cloning objects in javscript
(function CloneTest() {
const factory = (function () {
let factory = {
createShallow: function() {
return Object.create({
p1: "",
p2: "",
deepProperty: factory.createDeep(),
<?php
class ParentClass {
private $descendant;
public function setDescendant($descendant) {
$this->descendant = $descendant;
return $this;
}
public function showDescendantValue() {
@hdvianna
hdvianna / function-chaining.js
Created June 15, 2019 16:38
Function chaining in javascript
function chain(c, v, f) {
if (arguments.length < 3) {
f = v; v = c; c = null;
}
var result = f.call(null, c, v);
return {
chain: chain.bind(null, result),
result: result
}
}
@hdvianna
hdvianna / blocking-inserts.php
Created September 21, 2019 19:10
Asynchronous Inserts with AMP
<?php
$init = microtime(true);
$filePath = $argv[1];
$fileHandler = fopen($filePath, 'r');
$lineNumber = 0;
while (($lineString = fgets($fileHandler)) !== false) {
$parameters = [$lineString, $lineNumber];
$pdo = new PDO("mysql:dbname=hello_streams", "root");
$statement = $pdo->prepare("INSERT INTO files_line(line_string, line_number) VALUES (?,?)");
@hdvianna
hdvianna / hello-parallel.php
Created January 16, 2020 18:25
Producer-Consumer algorithm example using PHP's Parallel extension
<?php
use parallel\{Runtime, Channel};
main($argv);
function main(array $argv)
{
if (count($argv) !== 3) {
echo "Type: hello-parallel.php <number-of-tasks> <maximum-time-of-sleep (in seconds)>" . PHP_EOL;
@hdvianna
hdvianna / docker-compose_elastic_kibana.yml
Last active April 27, 2020 23:23
Elastic + Kibana docker-compose.yml
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02
@hdvianna
hdvianna / async-function-queue.js
Last active July 4, 2020 20:33
JS Async Function Queue
function f1(next) {
setTimeout(function() {
console.log("1");
next.dequeue();
}, 500);
}
function f2(next) {
setTimeout(function() {
console.log("2");