Skip to content

Instantly share code, notes, and snippets.

@kipcole9
kipcole9 / decode.ex
Created January 2, 2020 13:05
Decode a float in Elixir
defmodule Decode do
@moduledoc """
Extracted from https://github.com/ewildgoose/elixir-float_pp
"""
use Bitwise
@float_bias 1022
############################################################################
defmodule SteveTrollsElixir.Private do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@on_definition {unquote(__MODULE__), :on_priv_def}
end
end
defmacro private() do
quote do
@Qqwy
Qqwy / Capturepipe.ex
Last active January 21, 2022 12:55
Elixir example of a pipe-operator that uses some simple metaprogramming to allow piping into capture-syntax.
defmodule Capturepipe do
@doc """
A pipe-operator that extends the normal pipe
in one tiny way:
It allows the syntax of having a bare `&1` capture
to exist inside a datastructure as one of the pipe results.
This is useful to insert the pipe's results into a datastructure
such as a tuple.
@Qqwy
Qqwy / capturepipe2.ex
Last active June 24, 2020 15:38
Elixir example of a pipe-operator that uses some simple metaprogramming to allow piping into capture-syntax. Version with slightly more verbose `&`-prefixed syntax. Naive implementation that does not allow using multiple captures in the same pipeline.
defmodule Capturepipe do
@doc """
A pipe-operator that extends the normal pipe
in one tiny way:
It allows the syntax of having a bare `&1` capture
to exist inside a datastructure as one of the pipe results.
This is useful to insert the pipe's results into a datastructure
such as a tuple.
@eksperimental
eksperimental / typespec_example.ex
Last active July 2, 2020 18:36
Broken types in Elixir Issue #10140
# Downloaded from: https://gist.github.com/eksperimental/55f1e207ab5878a668668546f57a3f90
#
# Lists all the types that have the problem reported in
# https://github.com/elixir-lang/elixir/issues/10140
# create an Elixir project, and save this file in lib/typespec_example.ex
# Start IEx with: iex -S mix
# then run: TypespecExample.types()
# and paste the output into the shell.
defmodule TypespecExample do
@Qqwy
Qqwy / addition.beam_disasm.ex
Last active July 8, 2020 14:34
An example of what BEAM code ends up being generated for a simple TypeCheck spec.
{:beam_file, Addition,
[
{:__info__, 1, 2},
{:"__type_check_spec_for_add/2__", 0, 18},
{:add, 2, 8},
{:baseline_add, 2, 16},
{:module_info, 0, 20},
{:module_info, 1, 22}
], [vsn: [337339698024769425821845159222917769638]],
[
@Qqwy
Qqwy / Example.ex
Created July 11, 2020 21:51
Elixir 'safe macro override' example implementation
defmodule Example do
use OverrideExample1
use OverrideExample2
@a 1
@b 2
end
# Prints at compile-time:
#
# yaay: {:a, [line: 4], [1]}
# wooh: {:a, [line: 4, context: OverrideExample2], [1]}
@aphyr
aphyr / minikanren.pl
Created October 12, 2020 22:10
Minikanren in Lisp in Prolog
:- use_module(library(pairs)).
:- use_module(library(reif)).
not_in_list(K, L) :-
if_((L = []),
true,
([X | More] = L,
dif(K, X),
not_in_list(K, More))).
*> Compile using GNU Cobol, e.g.: `cobc -x gcd_maxmin.cob`
IDENTIFICATION DIVISION.
PROGRAM-ID. gcd-maxmin.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMS.
05 NUMS-ELEMS PIC 9(9)
OCCURS 0 TO 9999 TIMES DEPENDING ON NUMS-LEN.
77 NUMS-IDX PIC 9(9) COMP.
@akoutmos
akoutmos / json
Created January 30, 2022 04:15
Pretty print JSON passed via STDIN using Elixir shell script
#! /usr/bin/env elixir
# Install required deps
Mix.install([:jason])
# The pretty printing module
defmodule JsonPrettyPrinter do
def get_stdin_data do
# Fetch data from STDIN and decode JSON
:stdio