Skip to content

Instantly share code, notes, and snippets.

View tautologico's full-sized avatar

Andrei Formiga tautologico

  • UFPB
  • João Pessoa, Paraíba
View GitHub Profile
@tautologico
tautologico / intlist.rs
Last active December 26, 2015 01:19
Functional-style integer lists in rust
enum IntList {
Empty,
Cons(int, @IntList)
}
@tautologico
tautologico / gist:7002435
Last active December 25, 2015 15:59
monads in rust (1st attempt)
trait Monad<A> {
//fn bind<B>(&self, &fn (b: B) -> Self) -> Self;
fn ret(a: A) -> Self;
}
enum Zoption<T> {
ZSome(T),
ZNone
}
@tautologico
tautologico / bitvec.c
Created July 19, 2013 18:54
Simple bit vector for exploring all complementary sets to cover a set of indexes.
// bitvec.c
// Bit vector com ate 64 elementos
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_SIZE 64
typedef struct tagBitVector {
@tautologico
tautologico / slr.jl
Created July 8, 2013 17:11
Simple linear regression
using DataFrames
datafile = "data1.csv"
function coeff(data::AbstractDataFrame)
n = nrow(data)
A = sum(data["pop"])
Y = sum(data["profit"])
ya = sum(data["pop"] .* data["profit"])
a2 = sum(data["pop"] .^ 2)
@tautologico
tautologico / iris.jl
Created June 17, 2013 19:28
Classificação do conjunto Iris usando knn em Julia.
#
# iris.jl
# Classificação no conjunto de dados Iris usando knn
#
# Andrei Formiga, 2013-06-17
#
using DataFrames
train_file = "iris.train.csv"
// multiple indexing for matrices
struct Matrix {
n: int,
m: int,
data: ~[int]
}
impl Index<[int, ..2], int> for Matrix {
@tautologico
tautologico / mcpi.ml
Created March 14, 2013 21:39
Estimating pi by simulation of Buffon's needle. Pure functional version in OCaml.
(* estimating pi by monte carlo and buffon's needle *)
let mc_pi ~repeats =
let halfpi = asin 1.0 in
let rec loop i crosses =
if i = 0 then crosses
else
let x = Random.float 1.0 in
let theta = Random.float halfpi in
if x <= (0.5 *. (sin theta)) then loop (i-1) (crosses+1)
else loop (i-1) crosses in
@tautologico
tautologico / mcpi_iter.jl
Created March 14, 2013 21:38
Estimate pi by simulation of Buffon's needle. Iterative version.
# Estimate pi simulating Buffon's needle experiment
function mc_pi_iter(repeats::Int)
halfpi = asin(1)
crosses = 0
for i in 1:repeats
x = rand()
theta = halfpi * rand()
if (x <= 0.5 * sin(theta))
crosses = crosses + 1
end
@tautologico
tautologico / mcpi.jl
Created March 14, 2013 21:32
Estimating pi by simulation of the Buffon's needle experiment. Vectorized version.
# Estimate pi simulating the Buffon's needle experiment
function mc_pi(repeats::Int)
halfpi = asin(1)
x = rand(repeats)
theta = halfpi * rand(repeats)
crosses = sum( x .<= 0.5 * sin(theta) )
repeats / crosses
end
@tautologico
tautologico / groupbins.jl
Created October 21, 2012 01:06
Grouping a dataframe by binning a numeric column
function find_bin(x::Float64, limits::Vector{Float64})
bin = length(limits) + 1
for i in 1:length(limits)
if x < limits[i]
bin = i
break
end
end
bin
end