Skip to content

Instantly share code, notes, and snippets.

@Qqwy
Qqwy / caesar_cipher.pl
Created Nov 9, 2020
A simple substitution (Caesar/Vernam) cypher, implemented in Prolog. Beginner code; can probably be significantly improved
View caesar_cipher.pl
-module(caesar_cipher, [
shift_plaintext_cyphertext/3,
shift_plainchar_cypherchar/3,
shift_plainletter_cypherletter/3
]).
:- use_module(library(reif)).
:- use_module(library(clpz)).
:- use_module(library(lists)).
@Qqwy
Qqwy / Example.ex
Created Jul 11, 2020
Elixir 'safe macro override' example implementation
View Example.ex
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]}
@Qqwy
Qqwy / addition.beam_disasm.ex
Last active Jul 8, 2020
An example of what BEAM code ends up being generated for a simple TypeCheck spec.
View addition.beam_disasm.ex
{: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 / capturepipe3.ex
Last active Jun 24, 2020
Elixir example of a pipe-operator that uses some simple metaprogramming to allow piping into capture-syntax. Version with slightly more verbose `&`-prefixed syntax. Implementation that rewrites pipes nested inside &.
View capturepipe3.ex
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 Jun 24, 2020
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.
View capturepipe2.ex
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 / Capturepipe.ex
Last active Jun 21, 2020
Elixir example of a pipe-operator that uses some simple metaprogramming to allow piping into capture-syntax.
View Capturepipe.ex
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.
View is_leap_year.cc
#include <iostream>
#include <vector>
auto continuation = [](auto && val){return [=](auto && cont) { return cont(val);};};
auto style = [](const auto &val) { return val; };
auto mod = [](auto &&modv) { return [=](auto &&truecont){ return [=](auto &&falsecont) { return [=](auto &&val) { if (val % modv) { return truecont(val); } else { return falsecont(val); }; }; }; }; };
auto passing = [](auto &&val) { return [=](auto &&cont){ return [=](auto &&) {return cont(val);};};};
bool isLeapYear(int year){
return (continuation)
View IndexedArrowExample.hs
{-# LANGUAGE DataKinds, KindSignatures, TypeFamilies, PolyKinds, GADTs #-}
module Example where
import qualified Control.Arrow
import qualified Prelude
import Prelude hiding (id, (.))
import qualified Control.Category
-- | Example type: A Qfun is a function with a Quality: Either 'Good' or 'Bad'.
@Qqwy
Qqwy / classical_star_phase.c
Last active Nov 27, 2018
Bytebeat implementation of a classical piece of music, using a single sinusoid and comparing the naive, discontinuous signal with an improved, phase-accumulation based version.
View classical_star_phase.c
/*
* Date: 2018-11-26
* Author of this C code: Wiebe-Marten Wijnja (Qqwy).
* Author of original JavaScript-version: Unknown.
*
* This version uses phase-accumulation to ensure that the signal remains continuous throughout,
* and that there are no nasty clicks when we change to the next note.
* You can add the directive `DISCONTINUOUS VERSION` (using e.g. the `-D` flag of `gcc` and `clang`)
* to compile it as the original version, that bases its samples only directly on the current sample,
* and therefore includes clicks whenever the note changes.
@Qqwy
Qqwy / YCombinator.ex
Last active Oct 21, 2018
Explanation of using the Y-Combinator and wrapping functions in Elixir to write recursive functions and wrap them with extra functionality
View YCombinator.ex
defmodule YCombinator do
@moduledoc """
Some functions explaining the Y-Combinator.
Can definitely be improved upon, but this is as much as time allowed me this evening.
"""
def factorial(n) do
y_combinator(&almost_factorial/1).(n)