Skip to content

Instantly share code, notes, and snippets.

View elazar's full-sized avatar

Matthew Turland elazar

View GitHub Profile
@elazar
elazar / error.php
Created January 29, 2021 19:17
Psalm Error Example
<?php
// This version replaces the redundant code with a recursive call, but emits these errors:
//
// ERROR: InvalidReturnType - src/Filter/FilterTransformer.php:111:16 - The declared return type 'Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float' for Kiosk\Filter\FilterTransformer::getPrice is incorrect, got 'Kiosk\Filter\PriceRange|array<int|string(max)|string(min), Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float>|float' (see https://psalm.dev/011)
// * @return float|float[]|PriceRange|PriceRange[]
//
// ERROR: InvalidReturnStatement - src/Filter/FilterTransformer.php:130:16 - The inferred type 'array<int|string(max)|string(min), Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float>' does not match the declared return type 'Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float' for Kiosk\Filter\FilterTransformer::getPrice (see https://psalm.dev/128)
// return array_map(
// [$t
@elazar
elazar / README.md
Last active June 12, 2020 20:29
Leaving Facebook
@elazar
elazar / README.md
Last active June 24, 2020 22:33
Vampire Chronicles
@elazar
elazar / conway.php
Created May 17, 2020 12:26
Conway's Game of Life
<?php
$height = 20;
$width = 40;
$live_seeds = 20;
$delay = 1000000;
$empty_grid = fn(): array => array_fill(0, $height, array_fill(0, $width, false));
$random_percent = fn(): float => rand(0, 100) / 100;
@elazar
elazar / queens.php
Last active October 5, 2018 17:00
Eight Queens Problem
<?php
/**
* Sample output:
*
* $ time php queens.php
* [ ][ ][ ][ ][ ][X][ ][ ]
* [ ][ ][X][ ][ ][ ][ ][ ]
* [ ][ ][ ][ ][ ][ ][X][ ]
* [ ][X][ ][ ][ ][ ][ ][ ]
@elazar
elazar / jefferson.php
Last active January 12, 2018 03:34
Jefferson cipher
<?php
function jefferson(string $message)
{
$letters = array_merge(range('a', 'z'), ['_']);
$numbers = range(1, 26);
$cipher = [];
foreach ($numbers as $number) {
$cipher[$number] = [];
foreach ($letters as $index => $letter) {
@elazar
elazar / .zshrc
Last active February 20, 2020 23:19
git aliases
alias ga="git add"
alias gai="git add --interactive"
alias gb="git branch"
alias gbd="git branch -D"
alias gc="git commit"
alias gca="git commit --amend"
alias gcm="git commit -m"
alias gco="git checkout"
alias gcob="git checkout -b"
alias gd="git diff"
@elazar
elazar / Dockerfile
Created November 10, 2015 03:21
Fireplace Dockerfile
FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -y update && apt-get -y install python3 python3-pip git
RUN pip3 install virtualenv
RUN virtualenv ~/venv
RUN ~/venv/bin/pip install -e git+https://github.com/jleclanche/fireplace.git#egg=fireplace
RUN . ~/venv/bin/activate ; cd ~/venv/src/fireplace && pip3 install -r requirements.txt && ./bootstrap.sh && ./setup.py install && py.test
RUN echo '. ~/venv/bin/activate ; cd ~' >> ~/.bashrc
@elazar
elazar / dbutil.md
Last active August 29, 2015 14:02
Database utility I want

Let's imagine that you've got information for accessing several different databases you have to work with on a regular basis. Servers (e.g. mysql, pgsql, etc.), hostnames, ports, usernames, password files, database names, etc.

Let's also imagine that there are several programs you use on these databases regularly. If you use MySQL, this might include the mysql CLI client, the mysqldump utility, and liquibase for versioning.

Manually typing these commands together with this database information is tedious. Managing shorthand shell functions for the same purpose also seems suboptimal.

What I want is a wrapper utility of sorts to which I can specify the program I want to run and the name of (or potentially a shorthand alias for) the database I want to run it on, and have it pull all other information from a configuration file I create to derive and run the resulting CLI command.

Here's a hypothetical example of such a file:

@elazar
elazar / PasswordGenerator.php
Last active January 27, 2023 15:20
Password Iterator vs GeneratorOriginal commented password iterator implementation: https://gist.github.com/elazar/1342491
<?php
function PasswordGenerator($min = 1, $max = null)
{
$ord = range(32, 126);
$chr = array_map('chr', $ord);
$first = reset($chr);
$last = end($chr);
$length = $min;
$password = array_fill(0, $length, $first);
$stop = array_fill(0, $length, $last);