Skip to content

Instantly share code, notes, and snippets.

View bgnori's full-sized avatar

Noriyuki Hosaka bgnori

  • Itabashi, Tokyo, Japan
View GitHub Profile
(define xs (loop [i 0 xs '()]
(if (< i 3)
(recur (+ i 1) (cons (fn [x] i) xs))
xs)))
(println "defined.")
((car xs) 'a)
((cadr xs) 'a)
((caddr xs) 'a)
@bgnori
bgnori / map.clj
Created September 27, 2014 11:37
brmでmapを作ってみた. うごくが, これはとても書いてはいられないコード.
(define my-map-type (. reflect MapOf
(. reflect TypeByKind (. reflect Int))
(. reflect TypeByKind (. reflect String))))
(println my-map-type)
(define my-map (. reflect MakeMap my-map-type))
(. my-map SetMapIndex (. reflect ValueOf 1) (. reflect ValueOf "abc"))
(. my-map SetMapIndex (. reflect ValueOf 2) (. reflect ValueOf "def"))
@bgnori
bgnori / select.clj
Created September 15, 2014 00:33
select/chanを使ったコードの仕上がりイメージ. (read taskquit)とか大丈夫なのか??
:
; http://mattn.kaoriya.net/software/lang/go/20131112132831.htm
;
(begin
(define task (make-chan-string))
(define taskquit (make-chan-bool))
(define workerquit (make-chan-bool))
(go (fn []
(loop []
@bgnori
bgnori / go.clj
Created September 14, 2014 14:59
ちゃんと並列に実行されているようだ. cljが文法的に一番近いのでfile typeをcljにしている.
(GOMAXPROCS 4)
(defn greet [n msg]
(loop [i 0]
(if (< i n)
(begin
(println msg)
(recur (+ i 1)))
(println "done"))))
@bgnori
bgnori / defer-sample.brm
Created September 14, 2014 13:18
deferは関数を2つ渡すと他方をwrapして返すという感じ.
in: (define foo
    (defer
      (fn [] (println defer is cool!))
      (fn [x]
       (begin
        (println do something)
        (println x)
        (* 3 x)))))
--> foo
in: (foo 42)
@bgnori
bgnori / foo.py
Created August 25, 2014 00:54
PE40 変形版 pythonでのsolution. generatorを使ってlazyにやるためのパーツ
class G:
def __init__(self):
self.stack = []
def __iter__(self):
return reversed(self.stack)
def push(self, v):
@bgnori
bgnori / gist:97c33e45e820ad7136c4
Created August 24, 2014 23:56
Project Euter 40の変形. 1..9 10..99 100..999 ..を桁毎につなげる代わりに 9..1 99..10 999...100 .. をつなげる ここからlazyにしてtake n drop nする
(define (f n)
(define (downto end i)
(if (<= end i)
(cons i (downto end (- i 1)))
'()))
(downto (expt 10 (- n 1)) (- (expt 10 n) 1)))
;;(fold-right append '() (map f (list 1 2 3)))
(define (flatten xs) (fold-right append '() xs))
@bgnori
bgnori / huffman.go
Last active August 29, 2015 14:05
http://sicp.iijlab.net/fulltext/x234.html 復号のところまで. 複数値を返すことができるのとchannelはいいが, なんかなぁ...
package huffman
type Tree interface {
Weight() int
Symbols() []byte
DecodeBit(bits []int, pos int) (byte, bool, Tree)
Encode(c byte, out chan int)
}
package main
import (
"fmt"
)
func oddsum(xs []int) int {
sum := 0
for i, v := range xs {
if i % 2 == 0 {
package main
import (
"fmt"
)
func drop(xs []int, n int) []int {
ys := make([]int, len(xs)-1)
for i:=0 ; i < n ; i++{