Skip to content

Instantly share code, notes, and snippets.

Avatar

Olle Härstedt olleharstedt

View GitHub Profile
@olleharstedt
olleharstedt / nbody.c
Created January 22, 2023 23:30
PHP+C polyglot nbody benchmark
View nbody.c
//<?php echo "\x08\x08"; ob_start(); ?>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <glib.h>
#include <math.h>
#include <phollylib.c>
#if __PHP__//<?php
class GString { public $str; public function __construct($str) { $this->str = $str; } }
function g_string_new(string $str) { return new GString($str); }
View nbody.php
<?php // @pholyglot
class Body
{
public float $x;
public float $y;
public float $z;
public float $vx;
public float $vy;
public float $vz;
@olleharstedt
olleharstedt / tmp6.php
Created March 2, 2022 23:41
One universal dry-run mock-spy AST evaluator to rule them all
View tmp6.php
<?php
namespace Tmp6;
use InvalidArgumentException;
interface Node
{
}
@olleharstedt
olleharstedt / edsl.php
Last active October 7, 2022 21:49
EDSL in PHP
View edsl.php
<?php
/**
* EDSL = embedded domain-specific language
* The domain in this example is "side-effects", anything writing/reading to io, database, file, etc
*
* Using this EDSL, you neither need DI nor mocking to make the code testable. You can dry-run the particular
* parts needed of the "effectful" logic.
*
* Inspired by tagless-final pattern from FP. https://okmij.org/ftp/tagless-final/course/optimizations.html#primer
View que.md

Pre-req: Functional core, imperative shell

Pre-req: Side-effects, purity, referential transparency

  • The functional core is more testable and more composable than the imperative shell or effectful code
  • Extending the functional core means making a bigger part of the software pure
  • Side-effects can in some cases easily be lifted out from a function, creating a more composable and testable unit
  • But sometimes, side-effects are tangled in business logic
  • Some side-effects can be delayed until end of request (e.g., updating user's name in database; exception at failure can still be thrown)
  • Example: Create dummy users
@olleharstedt
olleharstedt / benchmark.js
Last active December 30, 2020 22:38
Clone benchmark
View benchmark.js
class Query {
constructor() {
this.select = '';
this.from = '';
this.where = '';
}
}
for (let i = 0; i < 1000000; i++) {
const q = new Query();
@olleharstedt
olleharstedt / amp_io.php
Created August 21, 2020 19:55
Mocking in Amphp, 2
View amp_io.php
<?php
require __DIR__ . '/../vendor/autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\HttpClient;
use Amp\Loop;
use Amp\ByteStream\ClosedException;
use Amp\File\File;
@olleharstedt
olleharstedt / WriteIO
Created August 21, 2020 18:52
Amphp and mocking
View WriteIO
<?php
require __DIR__ . '/../vendor/autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Loop;
use Amp\ByteStream\ClosedException;
use Amp\File;
@olleharstedt
olleharstedt / tmp.php
Created September 5, 2019 19:52
socket to browser
View tmp.php
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_bind($sock, '127.0.0.10', 888) === false) {
die('cannot bind' . PHP_EOL);
View tmp.php
<?php
$command = sprintf(
'echo -e "%s" | nc -U /var/run/docker.sock',
"GET /info HTTP/1.1\r\nHost: localhost\r\n\r\n"
);
echo '<pre>'; var_dump($command); echo '</pre>';
var_dump(exec($command));