Skip to content

Instantly share code, notes, and snippets.

@jarble
jarble / inverse.mzn
Created February 1, 2024 21:56
Inverse of factorial function in MiniZinc
function int:factorial(int:t) =
if t = 0 then 1 else t * factorial(t-1) endif;
function int: factorial_inverse(int:n, int:factorial) =
if factorial == factorial(n) then n else factorial_inverse(n+1,factorial) endif;
function int:factorial_inverse(int:n) =
factorial_inverse(0,n);
int:a = factorial_inverse(720);
@jarble
jarble / hamming.mzn
Created September 20, 2023 22:40
Hamming distance in MiniZinc
function int: hamming_distance(array[int] of int: arr1, array[int] of int: arr2) =
sum([if arr1[i] != arr2[i] then 1 else 0 endif | i in index_set(arr1)]);
% Example usage
array[int] of int: arr1 = [107, 97, 114, 111, 108, 105, 110];
array[int] of int: arr2 = [107, 97, 116, 104, 114, 105, 110];
int: distance = hamming_distance(arr1, arr2);
output ["The Hamming distance is ", show(distance)];
@jarble
jarble / object-oriented.mzn
Last active June 22, 2022 20:13
An implementation of object-oriented classes in MiniZinc
% define a Rectangle "class"
var int: Rectangle(var int: width, var int: height) =
let {
var int: this;
constraint Type(this) = Rectangle; %define the "type" of the instance
%define some "instance methods"
constraint area(this) = width*height;
constraint perimeter(this) = (width+height)*2;
constraint width(this) = width;
constraint height(this) = height;
@jarble
jarble / three_valued_logic.pl
Created May 29, 2022 22:01
An implementation of three-valued logic in Prolog
:- initialization(main).
main :-
three_valued_logic(((true,false);unknown),Result),
writeln(Result).
three_valued_logic(true,true).
three_valued_logic(false,false).
three_valued_logic(unknown,unknown).
three_valued_logic(not(A),false) :-
three_valued_logic(A,true).
@jarble
jarble / fuzzy_anagram_search.js
Last active April 15, 2022 23:05
An algorithm to find substrings that are nearly anagrams of a search string.
function nearly_anagrams(str1,str2,max_diff){
//max_diff is the maximum difference in the number of characters.
//Example usage:
//console.log(JSON.stringify(nearly_anagrams("hello","aahelloaaahello",1)));
let str1_chars = {}; //number of chars in str1
let char_diff = {}; //number of chars in the current substring
let sum_of_diff = 0; //sum of differences between number of each character
let results = [];
for(const i of str1){
@jarble
jarble / replace_matching_sublist.erl
Created September 27, 2021 18:57
Replacing matching sublists in Erlang
-module(helloworld).
-import(lists,[append/2]).
-export([start/0]).
replace_if_match(X) ->
case X of
[A, "equals", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
[A, "is", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
const replaceOnce = require('replace-once');
var str = 'I have a cat, a cathy, and a catch.';
var find = ['cat', 'cathy', 'catch'];
var replace = ['catch', 'catch', 'cathy'];
console.log(replaceOnce(str, find, replace, 'gi'));
//=> 'I have a catch, a catchhy, and a catchch.'
@jarble
jarble / generic_example.frag
Last active January 11, 2023 21:41
Generic programming in GLSL (parametric polymorphism, template metaprogramming) https://www.reddit.com/r/glsl/comments/mmxves/generic_programming_parametric_polymorphism/
//I wish there were a better way to do this!
#define func(type,name,param1,param2,body) type name(type param1,type param2) {body}
#define generic(name,param1,param2,body) func(float,name,param1,param2,body) func(vec2,name,param1,param2,body) func(vec3,name,param1,param2,body) func(vec4,name,param1,param2,body)
//define two "generic" functions using this macro
generic(add,a,b,
return a + b;
)
generic(sub,a,b,
@jarble
jarble / search_redirector.js
Created February 23, 2020 03:39
A Greasemonkey script with a "whitelist" of keywords
// ==UserScript==
// @name Search redirector
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Anderson Green
// @match *://*/*
// @grant none
// ==/UserScript==
@jarble
jarble / type_inference.pl
Last active January 22, 2020 19:10
A simple demo of type inference in SWI-Prolog
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :- Term = (member(Z,A),append(A,B,C),dif(A,[true,false])),has_type(Term:Type,Types),writeln('Term with types to infer:'),writeln(Term),writeln('Types of variables in this term:'),writeln(Types).
greater_than(A,B) :-
A > B.
matches_any_([],B) :- false.
matches_any_([A|A1],B) :-