Skip to content

Instantly share code, notes, and snippets.

View olliefr's full-sized avatar
🌩️

Oliver Frolovs olliefr

🌩️
View GitHub Profile
@olliefr
olliefr / description.md
Last active August 29, 2015 14:02
On the subject of Toroidal Planets, Sharks and Fish.

Background

I am writing a predator-prey simulator (for no good reason) and I have hit the wall with functional design. The simulator is that of a grid on which the fish and the sharks go about their business, the fish moves, grows and procreates and so do the sharks. The sharks also hunt and eat the fish. The time is discrete (in chronons) so it is a discrete-time multi-agent simulation. I currently have three data types -- World, Fish, and Shark.

The implementation

The simple way to make the World go round is described in the book as following (in my own words). Each turn, iterate over the list of agents and apply sense/act function which in turn will return a revised world, something like let world' = List.fold (World.fish world) ~init:world ~f:Fish.act. I have attached my code which (almost) implements this strategy for Fish in a somewhat ugly way.

The problem

This approach to allocating resources is biased, because the first fish in the list has more choices than the second by virt

@olliefr
olliefr / config-log
Created May 1, 2012 12:28
brew install qemu 1.0.1 config.log
/usr/bin/clang -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -c -o /var/folders/l0/2qhglx294pd92cqqf345t0_w0000gp/T//qemu-conf-1911-91631-31377.o /var/folders/l0/2qhglx294pd92cqqf345t0_w0000gp/T//qemu-conf-27552-91631-21858.c
/var/folders/l0/2qhglx294pd92cqqf345t0_w0000gp/T//qemu-conf-27552-91631-21858.c:2:2: error: #error Not defined
#error Not defined
^
1 error generated.
/usr/bin/clang -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -c -o /var/folders/l0/2qhglx294pd92cqqf345t0_w0000gp/T//qemu-conf-1911-91631-31377.o /var/folders/l0/2qhglx294pd92cqqf345t0_w0000gp/T//qemu-conf-27552-91631-21858.c
/usr/bin/clang -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -W
@olliefr
olliefr / qemu-error
Created May 1, 2012 12:26
brew install qemu 1.0.1 error log
$ brew install qemu
==> Downloading http://wiki.qemu.org/download/qemu-1.0.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/qemu-1.0.1.tar.gz
==> Downloading patches
######################################################################## 100.0%
######################################################################## 100.0%
==> Patching
patching file configure
Hunk #1 succeeded at 397 (offset 80 lines).
patching file fpu/softfloat.h
@olliefr
olliefr / gist:7752794
Last active December 30, 2015 00:59
On the subject of error-aware types in OCaml and user-friendly error messages.

Error-aware types and user-friendly error messages in OCaml

I'm reading Real World Ocaml and i have problems understanding how to present detailed yet uncluttered error messages to the user. I really like the idea of error-aware return types but getting the desired result out of Core seems to be impossible. I am probably doing something wrong. The examples in the book that i had seen so far look similar to the following one in a sense that they detect an exception and report "unexpected failure" or some such.

(* part 1 *)
(* Execute the DuckDuckGo search *)
let get_definition ~server word =
  try_with (fun () ->
 Cohttp_async.Client.get (query_uri ~server word)
@olliefr
olliefr / binomial-sum.R
Last active May 19, 2016 19:10
Brain teaser - a simulation approach.
trials <- 1000000
counter <- 0
for (i in 1:trials) {
x <- sample(0:1, size = 400, replace = TRUE)
y <- sum(x)
if (y >= 220) counter = counter + 1
}
print(round(counter / trials, digits=2))
@olliefr
olliefr / binomial-sum-2.R
Last active May 20, 2016 11:33
Brain teaser - summing the PMFs approach.
n <- 400 # number of tosses in a single trial
m <- 220 # threshold of interest
s <- 0 # the sum of binomial coefficients
for (k in m:n) {
s = s + choose(n,k)
}
p = s / 2^n
print(round(p, digits=2))
@olliefr
olliefr / binomial-sum-3.R
Last active May 20, 2016 13:19
Brain teaser - using the Central Limit Theorem.
m <- 400 * 0.5 #mean
v <- 400 * 0.25 #variance
s <- sqrt(v) #stdev
p <- 1 - pnorm(219, m, s)
print(round(p, digits=2))
@olliefr
olliefr / binomial-sum.cc
Last active May 20, 2016 15:17
Brain teaser - a simulation approach (C++).
// c++ -std=c++11 -Wall binomial-sum.cc -o binomial-sum
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
using namespace std;
@olliefr
olliefr / genlattice.R
Last active May 25, 2016 15:00
Generate a binomial lattice for a given up, down, start value and number of steps. A function by Rory Winston.
# Generate a binomial lattice
# for a given up, down, start value and number of steps
# This function was borrowed Rory Winston:
# http://www.theresearchkitchen.com/archives/738
genlattice <- function(X0=100, u=1.1, d=.75, N=5) {
X <- c()
X[1] <- X0
count <- 2
for (i in 1:N) {
@olliefr
olliefr / dotlattice.R
Created May 25, 2016 15:01
Generate a graph specification that can be fed into graphviz. A function by Rory Winston.
# generate a graph specification that can be fed into graphviz
# This function was borrowed Rory Winston:
# http://www.theresearchkitchen.com/archives/738
dotlattice <- function(S, labels=FALSE) {
shape <- ifelse(labels == TRUE, "plaintext", "point")
cat("digraph G {", "\n", sep="")
cat("node[shape=",shape,", samehead, sametail];","\n", sep="")
cat("rankdir=LR;","\n")