Skip to content

Instantly share code, notes, and snippets.

View susilolab's full-sized avatar

Agus Susilo susilolab

View GitHub Profile
<div id="canvas"></div>
<style>
#canvas {
width: 500px;
height: 300px;
border: 5px solid black;
position: relative;
box-sizing: content-box;
}
@susilolab
susilolab / work_queue.rs
Created August 3, 2021 02:40 — forked from NoraCodes/work_queue.rs
An example of a parallel work scheduling system using only the Rust standard library
// Here is an extremely simple version of work scheduling for multiple
// processors.
//
// The Problem:
// We have a lot of numbers that need to be math'ed. Doing this on one
// CPU core is slow. We have 4 CPU cores. We would thus like to use those
// cores to do math, because it will be a little less slow (ideally
// 4 times faster actually).
//
// The Solution:
@susilolab
susilolab / extract_tar_from_binary.ex
Created November 1, 2020 09:44 — forked from nroi/extract_tar_from_binary.ex
Decompressing a tar.gz archive in Elixir with Erlang's :erl_tar module.
@doc"""
Returns a map containing all files and their contents from the compressed tar archive.
"""
def extract_tar_from_binary(binary) do
with {:ok, files} <- :erl_tar.extract({:binary, binary}, [:memory, :compressed]) do
files
|> Enum.map(fn {filename, content} -> {to_string(filename), content} end)
|> Map.new
end
end
<?php
namespace main;
define("BACKEND_SOCKET", '<where the unix socket>');
define("BASE_URL", '<where this file located>');
function exit_500($msg) {
$msg = rtrim($msg, "\n") . "\nDebug trace:\n";
foreach(debug_backtrace() as $trace) {
@susilolab
susilolab / module_examples.ex
Created July 3, 2020 14:51
Two ways of dealing with modules
# Option 1
# Imagine that the implementations of these three functions span ~100 LOC each, including
# private functions
defmodule Users do
def create(params) do
# this and all private functions for this are like 100 LOC
end
def update(user, params) do
# this and all private functions for this are like 100 LOC
@susilolab
susilolab / borrow-example.rs
Created January 21, 2020 07:10 — forked from Aatch/borrow-example.rs
An example and explanation of how to use lifetimes and borrowing to avoid copying, while maintaining safety.
extern mod extra;
use extra::json::*;
/*
* This function manages to do absolutely no copying, which is pretty cool.
*
* "What are all those `'r`s?" you ask. Well, they're liftime parameters. They
* indicate how long something lasts (before it's freed). They can't change how
* long something lives for, they only allow you to tell the compiler stuff it
@susilolab
susilolab / exwx.exs
Created September 19, 2019 12:54 — forked from rlipscombe/exwx.exs
Using wxWidgets from Elixir
#!/usr/bin/env elixir
defmodule Canvas do
@behaviour :wx_object
@title "Canvas Example"
@size {600, 600}
def start_link() do
:wx_object.start_link(__MODULE__, [], [])
@susilolab
susilolab / webstoemp-gulpfile.js
Created September 19, 2019 04:29 — forked from jeromecoupe/webstoemp-gulpfile.js
Gulp 4 sample gulpfile.js. For a full explanation, have a look at https://www.webstoemp.com/blog/switching-to-gulp4/
"use strict";
// Load plugins
const autoprefixer = require("autoprefixer");
const browsersync = require("browser-sync").create();
const cp = require("child_process");
const cssnano = require("cssnano");
const del = require("del");
const eslint = require("gulp-eslint");
const gulp = require("gulp");
@susilolab
susilolab / nginx.conf
Created June 14, 2019 07:46 — forked from steve-ng/nginx.conf
Nginx reverse proxy wss with ssl
server {
listen 443 ssl;
server_name xxx.xx.io
ssl on;
ssl_certificate /etc/asterisk/certs/xxx.io.pem;
ssl_certificate_key /etc/asterisk/certs/xxx.io.key;
ssl_session_timeout 5m;
@susilolab
susilolab / dev_code_reload_plug.ex
Created February 26, 2019 14:58 — forked from alanpeabody/dev_code_reload_plug.ex
dev_code_reload_plug.ex
# Reload/recompile code before each request in development only using Plug & Mix.
# Assumes MyApp.Router is a plug that handles actual routing.
def MyApp.Main do
use Plug.Builder
plug :reload
plug :dispatch
def reload(conn, _opts) do
if Mix.env == :dev, do: Mix.Tasks.Compile.Elixir.run(["--ignore-module-conflict"])