Skip to content

Instantly share code, notes, and snippets.

@guregu
guregu / channelpredicate.go
Last active August 5, 2024 19:06
Example of a trealla-prolog/go native predicate returning multiple choice points
package trealla_test
import (
"context"
"sync/atomic"
"testing"
"sync"
"github.com/trealla-prolog/go/trealla"

(Testing against trealla-go)

Previous version:

cpu: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
BenchmarkQuery
BenchmarkQuery-16           1837            678403 ns/op
BenchmarkRedo
BenchmarkRedo-16 4272 287292 ns/op
@guregu
guregu / main.go
Created December 4, 2022 19:26
text adventure generated by AI
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Room struct {
@guregu
guregu / aoc2022-1.pl
Last active December 1, 2022 16:24
Advent of Code 2022 #1 in Prolog (Trealla Prolog)
% caveat: add an extra line break to the input :-)
:- use_module(library(dcgs)).
:- use_module(library(pio)).
elves([N|Ns]) --> elf(N), elves(Ns).
elves([]) --> [].
elf(Calories) --> sum(Calories), nl.
sum(X) --> number(A), nl, sum(B), { X is A + B }.
@guregu
guregu / between.go
Last active March 15, 2022 05:47
ichiban/prolog implementation: between/3
package lib
import (
"context"
"math"
"github.com/ichiban/prolog/engine"
"github.com/guregu/predicates/internal"
)
@guregu
guregu / mail.go
Created November 4, 2020 14:26
minimal SES mailer
package email
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
const noreply = " <noreply@example.com>"
@guregu
guregu / telegram.go
Created November 3, 2020 14:50
telegram message sending and webhook
package mail
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
var TelegramAuthToken string // set this
@guregu
guregu / benchmarks.txt
Created April 25, 2020 10:54
Benchmarks for guregu/null v4
# Benchmarks for v4
BenchmarkIntUnmarshalJSON-8 4329932 268 ns/op 184 B/op 2 allocs/op
BenchmarkIntStringUnmarshalJSON-8 4859449 248 ns/op 184 B/op 2 allocs/op
BenchmarkNullIntUnmarshalJSON-8 183305532 6.46 ns/op 0 B/op 0 allocs/op
BenchmarkStringUnmarshalJSON-8 4850442 241 ns/op 181 B/op 2 allocs/op
BenchmarkNullStringUnmarshalJSON-8 184386210 6.46 ns/op 0 B/op 0 allocs/op
# Benchmarks for v3
BenchmarkIntUnmarshalJSON-8 1756759 682 ns/op 392 B/op 6 allocs/op
BenchmarkIntStringUnmarshalJSON-8 3163022 380 ns/op 216 B/op 4 allocs/op
@guregu
guregu / fix-image-maps.js
Created October 17, 2017 14:40
Responsive image maps (vanilla JS)
function fixImageMaps(force) {
var imgs = document.querySelectorAll("img[usemap]");
[].forEach.call(imgs, function(img) {
if (!img.naturalHeight) { return; }
var h = img.height / img.naturalHeight;
var w = img.width / img.naturalWidth;
var map = document.getElementsByName(img.useMap.slice(1))[0];
if (!map) { return; }
for (var i = 0; i < map.children.length; i++) {
var area = map.children[i];
@guregu
guregu / main.go
Created February 12, 2016 23:19
Compsing kami
package main
import (
"fmt"
"net/http"
"github.com/guregu/kami"
"golang.org/x/net/context"
)