Skip to content

Instantly share code, notes, and snippets.

@Qqwy
Qqwy / benchmarks.exs
Last active September 7, 2023 11:29
Elixir benchmark of different implementations of the 'subarray sum' problem
Mix.install([
:benchee,
{:okasaki, "~> 1.0"}, # <- used by Qqwy's Okasaki solution
:nx, # <- used by Qqwy's Nx solution
{:exla, "~> 0.5"}, # <- used by Qqwy's Nx solution
],
config: [nx: [default_backend: EXLA.Backend]] # <- used by Qqwy's Nx solution
)
inputs = %{
@Qqwy
Qqwy / gen_err.rs
Created August 1, 2023 18:10
Generic Error handling using anonymous enums for the error cases. Rust alternative to PureScript's 'VEither'/'VExceptT'.
#![feature(type_name_of_val)]
// Depends on the frunk library, v0.4.1
use frunk::coproduct::{Coproduct, CNil, CoprodUninjector, CoprodInjector};
pub trait Variant {
fn default<A>(self, val: A) -> A
where Self: Sized
{
use backdrop::BackdropStrategy;
use stable_deref_trait::StableDeref;
use std::sync::Arc;
use streaming_iterator::StreamingIterator;
/// A struct that wraps any `impl StreamingIterator<item = T>`
/// together with its data source (where it borrows the references to T from).
///
/// The data source will be dropped once iteration is finished.
/// To make this safe, the data source will need to be in a stable location in memory.
@Qqwy
Qqwy / DAG-based-AItomator.md
Last active May 25, 2023 09:09
GPT-3.5 input to help with José Valim's "Skynet" / "AItomator" streams.

Good day! I will ask you to perform a task. Your job is to come up with a directed acyclic graph (DAG) of simple subtasks to accomplish the task. The output of the last step of the DAG should be the answer of the task.

To help you, here is a list of tools you can use.

  • date_from_today: Given a number of days (positive or negative) as input, it returns the date of today offset by that amount of days as output.
  • date_diff: Given a yyyy-mm-dd date, a second yyyy-mm-dd date, and days/weeks/months/years (as string) as inputs, it returns an integer representing the difference as output.
  • location_to_gps: Given a city name as input, returns GPS coordinates as output.
  • temperature_api: Given a yyyy-mm-dd date and GPS coordinates as inputs, it returns the forecasted or measured temperature in Celsius as output.
  • fashion_suggestion: Given a temperature in Celius as input, it recommends fashionable clothes to wear based on the temperature as output.
@Qqwy
Qqwy / index.html
Created May 18, 2023 11:05
Comparison of 32-bit multiplication in JS (http://jsbench.github.io/#23281fd842768138711bcd57e7b2a80a) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Comparison of 32-bit multiplication in JS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@Qqwy
Qqwy / refined_assertions.rb
Created May 1, 2022 18:54
Refined Minitest Assertions
class RefinedAssertions
@mutex = Mutex.new
def self.set_last_operator(operator, lhs, rhs, res)
@mutex.synchronize do
@set = true
@result = [operator, lhs, rhs, res]
end
@Qqwy
Qqwy / doctest-minitest.rb
Created March 9, 2022 23:38
Example of how to integrate doctests with Ruby Minitest
# Example of how to integrate doctests with Ruby Minitest
#
# Besides depending on `minitest`,
# this functionality depends on the `doctest-core` gem, c.f. https://www.rubydoc.info/gems/doctest-core/
class YourAppName::TestCase < MiniTest::Test
# To be used inside the body of a tests-class
# It will automatically create test cases for all
# 'documentation test' snippets that exist in the comments
defmodule Benchmarks do
@warmup 0.05
@time 0.05
@memory_time 0.5
@parallel 1
@inputs (
(0..20)
|> Enum.map(fn size -> {to_string(size), (1..size) |> Enum.shuffle |> List.to_tuple} end)
)
*> 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.
@Qqwy
Qqwy / caesar_cipher.pl
Created November 9, 2020 21:53
A simple substitution (Caesar/Vernam) cypher, implemented in Prolog. Beginner code; can probably be significantly improved
-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)).