Skip to content

Instantly share code, notes, and snippets.

View omasanori's full-sized avatar

Masanori Ogino omasanori

View GitHub Profile
@omasanori
omasanori / prime.clj
Created September 28, 2011 22:10
A hobby implementation to calculate prime numbers.
(defn prime?
"Returns true if x is prime number, false otherwise."
[x]
(not-any? zero?
(map #(rem x %)
(range 2 (inc (/ x 2))))))
(defn prime-numbers
"Returns a lazy seq of prime numbers."
[]
@omasanori
omasanori / leap_year.clj
Created September 28, 2011 22:24
A hobby implementation to calculate leap years.
(defn leap-year?
"Returns true if x is leap year, false otherwise."
[x]
(or (zero? (rem x 400))
(and (zero? (rem x 4))
(not (zero? (rem x 100))))))
(defn leap-years
"Returns a lazy seq of leap years."
[]
@omasanori
omasanori / lcg.clj
Created September 29, 2011 05:29
A hobby implementation of linearcongruential generators.
(defn lcg
"Returns a lazy seq of random numbers using linear congruential
generators (LCGs)."
[seed a b m]
(map #(float (/ %))
(drop 1
(iterate (fn [x] (mod (+ (* a x) b) m))
seed))))
(defn lcg-average
@omasanori
omasanori / prime_numbers.clj
Created September 30, 2011 03:00 — forked from mnzk/gist:1250574
Eratosthenes's infinite sieve
(defn- diff-seq
"Returns a lazy seq of numbers in s1 but not in s2.
Both of s1 and s2 must be increasing monotonically."
[s1 s2]
(when-let [x1 (first s1)]
(if-let [x2 (first s2)]
(cond
(= x1 x2) (recur (rest s1) (rest s2))
(> x1 x2) (recur s1 (drop-while (partial > x1) s2))
(< x1 x2) (let [[s11 s12] (split-with (partial > x2) s1)]
@omasanori
omasanori / generate_boundary.clj
Created December 15, 2011 12:14
A small library to generate boundaries for multipart/form-data and so on.
(ns generate-boundary)
(defn- char-range
"Returns a lazy seq of characters between `start` and `end`.
Unlike clojure.core/range, `end` is included in the results.
e.g. (= \9 (last (char-range \0 \9)))"
[start end]
(map char (range (int start) (inc (int end)))))
(def ^:dynamic *characters*
@omasanori
omasanori / bf.clj
Created December 19, 2011 07:53
A Brainfuck interpreter written in Clojure
;;; bf.clj --- A Brainfuck interpreter written in Clojure
;; The idea to implement loop instructions without waste of stack
;; is borrowed from a interpreter by Blake Williams, available at
;; https://github.com/BlakeWilliams/Clojure-Brainfuck
;;; Data Constructors and Manipulators
;; TODO(omasanori): Consider writing a macro to remove duplication of code.
(defn valid-address?
"Returns true if `address` is valid, otherwise false."
[address]
;;; See also
;; http://d.hatena.ne.jp/mkut/20111226/1324907006
;; http://blog.livedoor.jp/dankogai/archives/51763038.html
;; My straightforward but naive solution.
(defn naive-map-between
[f coll]
(lazy-seq
(let [[x y] coll]
(when (and x y)
@omasanori
omasanori / garbage-collection-advent-calendar-2012.mkd
Created December 20, 2012 05:42
20th article on Garbage Collection Advent Calendar 2012

PyPyのGCに挑戦した……けど……な話

これは[Garbage Collection Advent Calendar 2012][gcac2012]の20日目の文書として @omasanori がPyPyのGCを解説する……はずでしたが、期日までに理解しきれませんでした。すみません。

そこで、ここでは私が現時点でどこまで理解できたか、これからどこを読んでいくか、という途中経過を記すことにします。GC初心者視点なのでGC上級者には物足りない文章になっていると思います。

はじめに

[PyPy][pypy]はRestricted Python (RPython)と呼ばれるPythonのサブセットを中心としたインタプリタ実装ツールキットと、それを利用したPythonの処理系です。Pythonの処理系としての印象が強いですが、RPythonとツールキットの部分は他のインタプリタを実装するために利用することも可能です。

@omasanori
omasanori / gist:6137580
Created August 2, 2013 04:52
an error log when I tried to build extra::unicode.
cfg: build triple x86_64-unknown-linux-gnu
cfg: host triples x86_64-unknown-linux-gnu
cfg: target triples x86_64-unknown-linux-gnu
cfg: host for x86_64-unknown-linux-gnu is x86_64
cfg: os for x86_64-unknown-linux-gnu is unknown-linux-gnu
cfg: using gcc
cfg: no node found, omitting docs
cfg: no llnextgen found, omitting grammar-verification
compile_and_link: x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/bin/rustc
error: linking with `cc` failed with code 1
fn seq_lt<A: Eq + Ord, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return false,
(None, _ ) => return true,
(_, None) => return false,
(Some(x), Some(y)) => if !x.eq(&y) { return x.lt(&y) }
}
}
}