Skip to content

Instantly share code, notes, and snippets.

View st's full-sized avatar

Stephane Tavera st

  • Geneva, Switzerland
View GitHub Profile
(def rules {7 "Qix" 5 "Bar" 3 "Foo"})
(defn div [[n s]]
(fn [[x accu]]
[x (str accu (when (= 0 (rem x n)) s))]))
(defn to-ints [x]
(map #(- (int %) 48) (seq (str x))))
(defn has [[x accu]]
@st
st / monty_hall.rb
Created February 16, 2012 13:25
Monty Hall Problem
@wins_sticky = 0
@wins_change = 0
@nb_parties = 1000
@possibles = [0, 1, 2]
def play
winner = rand(3)
first_choose = rand(3)
others = @possibles - [first_choose]
discarded = others.include?(winner) ? (others - [winner]).first : others[rand(2)]
@st
st / gist:2850385
Created June 1, 2012 08:37
leiningen problem
$ java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-11M3635)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-415, mixed mode)
$ lein2 -v
Leiningen 2.0.0-preview4 on Java 1.6.0_31 Java HotSpot(TM) 64-Bit Server VM
$ lein2 new project
Generating a project called project based on the 'default' template.
; Original problem here : http://www.4clojure.com/problem/144
; This first solution works but is incomplete as fs needs to be repeated.
(defn osci-incomplete [v & fs]
(reduce (fn [accu elt] (conj accu (elt (last accu))))
[v] fs))
; Problem is that cycling fs leads to timeout.
@st
st / gist:5a030558b41cdce3e3c6
Created December 19, 2014 08:47
git update (CVE-2014-9390)
# on mac os X
# in case, your git is the "apple" version
sudo mv /usr/bin/git /usr/bin/git-apple
brew install git
brew update && brew upgrade git
sudo ln -s /usr/local/bin/git /usr/bin/git
# in case, it's already managed by brew
brew update && brew upgrade git
@st
st / gist:c68ca56f796d04dee152
Last active August 29, 2015 14:13
Issue with pooled connection in Korma with Java 1.8 (mac os x yosemite)

Minimal setup to reproduce the problem. Note that that with Java 1.7 this works fine.

  $ java -version
  java version "1.8.0_40-ea"
  Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b15)
  Java HotSpot(TM) 64-Bit Server VM (build 25.40-b18, mixed mode)
@st
st / gist:0c8512f916b944a3a23d
Created February 22, 2015 18:03
Planète bleue scrapping
require 'nokogiri'
require 'open-uri'
@plays = {}
def extract_plays(url, i)
doc = Nokogiri::HTML(open(url))
artist_names = doc.xpath("//a[contains(@href,'artist')]").collect {|node| node.text.strip}
@st
st / walk this way
Last active August 16, 2017 16:10
useful for interop with "java" maps
(defn clojurify
[exp]
(cond
(instance? java.util.Map exp) (into {} (for [[k v] exp] [(keyword k) v]))
(instance? java.util.List exp) (into [] exp)
:else exp))
(defn walk-to-clojure-map
[java-map]
(clojure.walk/prewalk clojurify java-map))

Your very first steps with Clojure

;; $> lein repl into-1 ;; ;; This will create a Clojure project (a folder with a common structure) named 'into-1' ;; $>

1

@st
st / gist:d52ae86b8ddb83a6f6b4
Created July 6, 2015 16:43
hmac verify with String signature
(-> (hmac/hash "foo bar" "mysecretkey" :sha256)
(codecs/bytes->hex))
=> "61849448bdbb67b39d609471eead667e65b0d1b9e01b1c3bf7aa56b83e9c8083"
;; so far, so good
(hmac/verify "foo bar" "61849448bdbb67b39d609471eead667e65b0d1b9e01b1c3bf7aa56b83e9c8083" "mysecretkey" :sha256)
=> false
;; should be true, no?
(def bytes-hash (hmac/hash "foo bar" "mysecretkey" :sha256))