Skip to content

Instantly share code, notes, and snippets.

@joshnuss
joshnuss / output.md
Last active September 9, 2024 23:04
Elixir OpenSCAD Generator

Example's Output

@joshnuss
joshnuss / httpx.exs
Last active November 10, 2016 20:09
Concurrent HTTP Server in 79 LOC
# $ elixir httpx.exs
# $ curl -v localhost:8080/test
defmodule HTTPX do
defmodule Request,
do: defstruct [:verb, :path, :query, :host, :headers]
defmodule Response,
do: defstruct [body: "", content_type: "text/plain", code: 200, headers: []]
defmodule Server do
@joshnuss
joshnuss / README.md
Last active August 19, 2019 22:34
Parse & Generate query strings with Coffeescript

Parse & Generate Complex Query Strings

with Coffeescript

This code will allow you to build a JS dict/object from a query string. It supports arrays, dicts and scalars (strings)

For example, this string:

>> query = "arr[]=1&arr[]=2&q=foo&f=baz&h[a]=1&h[bc]=3"
>> queryString.parse(query)
@joshnuss
joshnuss / checkout.exs
Last active August 6, 2017 01:08
High speed e-commerce checkout using Elixir & Task.async
# Parallel Checkout
# --------------------------------------------------------------
# Example of performance gained by using a parallel checkout in an e-commerce store,
#
# to run 500 checkouts in series: time elixir checkout.exs 500 serial
# to run 500 checkouts in parallel: time elixir checkout.exs 500 parallel
#
# Typical E-commerce checkout flow uses a bunch of network bound tasks, that are generally
# computed synchronously. This wastes time and requires larger server clusters to handle peak times
#
@joshnuss
joshnuss / infinite-example.md
Last active August 29, 2015 14:22
Infinite Scrolling with Angular.js

Infinite Scrolling with Angular.js

Use a simple directive to trigger reloading when nearing bottom of page.

Template

Example of triggering next page when user scroll 300px from bottom of page.

<section ng-controller='ContactsCtrl'>
@joshnuss
joshnuss / app.js
Last active February 18, 2025 15:49
Express.js role-based permissions middleware
// the main app file
import express from "express";
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db)
import authenticate from "./authentication"; // middleware for doing authentication
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request
const app = express(),
api = express.Router();
// first middleware will setup db connection
@joshnuss
joshnuss / phoenix_channel_client.exs
Last active April 8, 2021 17:24
Example of a Phoenix channel client
# Example of Elixir code consuming a Phoenix channel
#
# 1) Join a room
# 2) Send a message
# 3) Receive a message
#
# Add `socket` to your deps in `mix.exs`
# {:socket, "~> 0.3"}
#
# To run:
defmodule Stack do
use GenServer
@name __MODULE__
def start_link,
do: GenServer.start_link(__MODULE__, [], name: @name)
def pop,
do: GenServer.call(@name, :pop)
@joshnuss
joshnuss / RAID.md
Last active September 19, 2018 15:28
Using multi_call to implement a distributed RAID1

Distributed RAID1

This is an example of adding redundancy and reliability to a storage system using Elixir.

The uptime of a system can be increased by storing the data redundantly across multiple nodes. If a node goes down, the entire system isn't effected, as long as one of the nodes remains up - the system will continue to function.

It's pretty easy to accomplish this with a GenServer, instead of using GenServer.call/3, use GenServer.multi_call/4. With multi_call, a call is sent to each node in parallel. It's an easy way to implement redundant writes, similar to RAID1.

How it works

@joshnuss
joshnuss / geometry.exs
Last active January 4, 2017 07:21
A little geometry
defmodule Line do
def slope({x1, y1}, {x2, y2}),
do: delta(y1, y2) / delta(x1, x2)
def intercept(point: {x, y}, slope: slope),
do: {0, y - (slope * x)}
def point(x: x, slope: slope, intercept: intercept),
do: {x, (slope * x) + intercept}