Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mndrix's full-sized avatar

Michael Hendricks mndrix

View GitHub Profile
@mndrix
mndrix / under_camel.prolog
Last active August 29, 2015 13:55
Prolog relation between camel-case names and underscore-separated names
:- use_module(library(clpfd)).
:- use_module(library(delay)).
%% under_camel(?Underscore:atom, ?CamelCase:atom) is det.
%
% For example, `under_camel(hello_world, 'HelloWorld')`. Works in both
% directions.
under_camel(U,C) :-
when((ground(U0);ground(U)),prepend_underscore(U, U0)),
delay(atom_codes(U0,U0Codes)),
@mndrix
mndrix / focused-iterm2-tab.scpt
Last active August 29, 2015 14:00
AppleScript to find the currently focused iTerm2 tab
tell application "iTerm"
return name of current session of current terminal
end tell
@mndrix
mndrix / focused-chrome-tab.scpt
Created April 22, 2014 18:50
AppleScript to find the currently focused Google Chrome tab
-- run with `osascript -s s focused-chrome-tab.scpt` to get machine-readable output
tell application "Google Chrome"
set active_window to first window
set i to active tab index of active_window
set active_tab to item i of tabs of active_window
set x to {URL:URL of active_tab, title:title of active_tab}
return x
end tell
@mndrix
mndrix / benchmark.prolog
Created May 7, 2014 15:25
Database predicate benchmarks (SWI Prolog)
:- dynamic asserted_a/1, asserted_z/1.
:- b_setval(global_variable, []).
count(1_000_000).
my_asserta(N) :- asserta(asserted_a(N)).
my_assertz(N) :- assertz(asserted_z(N)).
my_recorda(N) :- recorda(recorded_a, N).
my_recordz(N) :- recordz(recorded_z, N).
my_flag(N) :- flag(some_flag, _, N).
@mndrix
mndrix / bar.prolog
Created May 16, 2014 21:55
Experiments with Prolog imports and exports from modules
:- module(bar, [ bar_a/0 ]).
do_import(TargetModule) :-
%TargetModule:use_module(bar, [bar_a/0]).
%TargetModule:import(bar:bar_a/0).
TargetModule:assert((bar_a :- writeln("Asserted bar_a/0"))).
bar_a :-
writeln("Inside bar_a/0").
@mndrix
mndrix / bench.prolog
Last active August 29, 2015 14:01
Sketch of a benchmark library
:- module(bench, [call_ns/1,call_ns/2,compare/1]).
:- use_module(library(sweet)).
compare(Goals) :-
maplist(run_comparison,Goals,Timings),
keysort(Timings,Sorted),
format("name\tns/op~n",[]),
maplist(show_timing,Sorted).
@mndrix
mndrix / binary-simple.prolog
Last active August 29, 2015 14:06
Simplification and binarization of Prolog clauses
% Simplification and binarization of Prolog clauses.
%
% This code was my note paper while reading
% "The BinProlog Experience: Architecture and
% Implementation Choices for Continuation Passing
% Prolog and First-Class Logic Engines" by Paul Tarau.
%
% I find it especially pleasing that binary logic programs
% require only about half as many WAM instructions as
% traditional Prolog. This should make it easy to build
@mndrix
mndrix / rules.pl
Last active August 29, 2015 14:20
Qualifications for foreign earned income exclusion
% Requirements, using Prolog syntax, to qualify for the foreign earned income exclusion
% See http://www.irs.gov/Individuals/International-Taxpayers/Foreign-Earned-Income-Exclusion
% facts which act as parameters to the loophole solver
:- dynamic
us_citizen/0,
us_resident_alien/0.
% See http://www.irs.gov/Individuals/International-Taxpayers/Foreign-Earned-Income-Exclusion-Can-I-Claim-the-Exclusion-or-Deduction
qualify :-
@mndrix
mndrix / roman.pl
Created July 10, 2015 20:49
Roman numerals in Prolog
:- use_module(library(clpfd)).
dcg(Arabic) -->
{ Arabic in 1..3999 },
roman(Arabic),
!.
roman(Total) -->
{ Rest #>= 0 },
{ Total #= Value + Rest },
@mndrix
mndrix / upc.hs
Created February 21, 2009 00:08
UPC validation in Haskell
module UPC (is_upc) where
import Data.Char (digitToInt)
-- Is this string a valid Universal Product Code?
is_upc :: String -> Bool
is_upc u = ( length u == 12 ) && ( upc_checksum u `mod` 10 == 0 )
-- Calculate the UPC checksum using the algorithm from http://xrl.us/omxh8
upc_checksum :: String -> Int