Skip to content

Instantly share code, notes, and snippets.

@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
@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))).
@niku
niku / .README.md
Last active February 17, 2024 20:48
a sample repository of prop_check with test-unit

This repository is working sample of prop_check with test-unit.

$ ruby run_test.rb
Loaded suite 6dec8e397943cdc372b969fff26299e7
Started
E
================================================================================================================================================
Error: test_that_it_returns_an_integer_for_any_input(TestNaiveAverage):
 ZeroDivisionError:
@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]}
@Janiczek
Janiczek / Main.elm
Last active September 11, 2023 00:27
Free Monad + Interpreter in Elm
module Main exposing (..)
{-| Free monad + interpreter in Elm
-}
import Dict exposing (Dict)
{-| Dict String Int-like structure
@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 = %{
@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
############################################################################
@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
@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.
*> 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.