Skip to content

Instantly share code, notes, and snippets.

@scientific-coder
scientific-coder / fib-seq-1.clj
Created March 1, 2012 16:58
Code snippets for Duchess-fr Battle Language 2012-02-29 test it on http://tryclj.com/ !
(def fib-seq
"lazy seq of Fibonacci numbers"
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))
#include <cstdint>
#include <cstddef>
#include <iostream>
#include <iterator>
//g++ -std=c++0x balance.cxx -o balance_cxx -O4 -march=native
int main(int argc, char* argv[]) {
for(std::istream_iterator<int64_t> in(std::cin),e; in!=e && *in; ){
int64_t res(0);
for(int64_t i(0),nb(*in++); i != nb; ++i,++in)
{ res+= (i < (nb /2)) ? *in : ((i > (nb /2) ||(nb %2 == 0 )) ? -*in :0); }
@scientific-coder
scientific-coder / bench.sh
Created July 22, 2012 13:15
Optimization Contest
#!/bin/bash
DATA=../data_2000_50000.in
NB_RUN=10
for SOURCE in $(ls *.cxx);
do
EXEC=${SOURCE%.cxx}
[ ! -e ${EXEC} ] && g++-snapshot -std=c++11 ${SOURCE} -o ${EXEC} -O4 -march=native -Wno-deprecated
echo -ne ${EXEC}'\t'
for i in $(seq ${NB_RUN});
do
(defn- optimal-plan-ordered [bids]
(let [optimal-plan-after (fn [flight]
(let [earliest (+ (:DEPART flight) (:DUREE flight))
after (drop-while #(< (:DEPART %) earliest) bids)]
(if (empty? after) {:gain 0 :path '()} (optimal-plan-ordered after))))
current-plan (fn [flight]
(let [{:keys [gain path]} (optimal-plan-after flight)]
{:gain (+ (:PRIX flight) gain) :path (conj path (:VOL flight))}))]
(reduce #(if (> (:gain %2) (:gain %1)) %2 %1) (map current-plan bids))))
(ns rxjava-datomic.query
(:require [datomic.api :as d])
(:use [clojure.repl :only [pst]])
(:import [rx Observable]
datomic.Peer))
(defn query [qry & inputs]
(Observable/toObservable
(Peer/q qry (object-array inputs))))
@scientific-coder
scientific-coder / fibonacci-algebraic.clj
Last active December 18, 2015 02:19
fibonacci using algebraic properties of matrix multiplication and fast power algorithm, adapted from this *great* book : "Elements of Programming" by Alexander Stepanov and Paul McJones http://www.elementsofprogramming.com/
(defn power [a n op]
"Compute a to the (positive int) power n using operator op, using
exponentiation by squaring."
(let [ power-accumulate-positive (fn [r a n op]
(let [r (if (odd? n) (op r a) r)]
(if (= 1 n)
r
(recur r (op a a) (quot n 2) op))))]
(if (odd? n)
(let [n (quot n 2)]
@scientific-coder
scientific-coder / JIT-assembly-dump
Last active December 24, 2015 09:38
amap & areduce
Decoding compiled method 0x00007fe951138a90:
Code:
[Entry Point]
[Constants]
# {method} '<init>' '(Ljava/net/URLClassLoader;Ljava/lang/String;)V' in 'java/net/URLClassLoader$1'
# this: rsi:rsi = 'java/net/URLClassLoader$1'
# parm0: rdx:rdx = 'java/net/URLClassLoader'
# parm1: rcx:rcx = 'java/lang/String'
# [sp+0x20] (sp of caller)
0x00007fe951138bc0: mov 0x8(%rsi),%r10d
@scientific-coder
scientific-coder / transducers.cxx
Last active August 29, 2015 14:05
first stab at transducers —map, filter and flatmap— in idiomatic (‽) C++, now with FizzBuzz !
#include <array>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <sstream>
/* const correctness and pass by value vs const ref, move semantics
left as an exercise to the reader.
iterator range API prevents a flatmap as the flatmapped function
could not either return a value or a range.
@scientific-coder
scientific-coder / compose.hxx
Last active August 29, 2015 14:07
FizzBuzz
#include <functional>
// TODO : monoid
template<typename... Fn> struct composer;
template<> struct composer<> {
template<typename Data> Data operator()(Data d){ return d; }
};
template<typename F0, typename... Fn>
struct composer<F0, Fn...> : private composer<Fn...> {
F0 f;
composer(F0 f0, Fn const&... fn): composer<Fn...>(fn...), f(f0){}
@scientific-coder
scientific-coder / FizzBuzz.cxx
Created October 24, 2014 15:34
FizzBuzz in C++ with some influence ☺
#include <iostream>
#include <iterator>
#include <string>
#include <tuple>
#include <functional>
#include "transducers.hxx"
#include "tuple_reader.hxx"
// usage: seq 1 20 |./FizzBuzz 3 Fizz 5 Buzz