Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / clojure-tips.md
Created December 11, 2012 05:56
Clojure tips infrequently used

Clojure tips infrequently used

To get names of methods of a class

(map #(.getName %) (.getMethods java.io.File))
; -> ("equals" "toString" "hashCode" "compareTo" "compareTo" "getName" "length" "getParent" "isAbsolute" "getCanonicalPath" "setReadOnly" "list" "list" "delete" "getParentFile" "getPath" "getAbsolutePath" "getAbsoluteFile" "getCanonicalFile" "toURL" "toURI" "canRead" "canWrite" "exists" "isDirectory" "isFile" "isHidden" "lastModified" "createNewFile" "deleteOnExit" "listFiles" "listFiles" "listFiles" "mkdir" "mkdirs" "renameTo" "setLastModified" "setWritable" "setWritable" "setReadable" "setReadable" "setExecutable" "setExecutable" "canExecute" "listRoots" "getTotalSpace" "getFreeSpace" "getUsableSpace" "createTempFile" "createTempFile" "wait" "wait" "wait" "getClass" "notify" "notifyAll")
@kohyama
kohyama / check_inline.clj
Created December 11, 2012 03:02
Check how things are going about expansion and loading in clojure
(ns check-inline
(:gen-class))
(defmacro sqr-m [n] `(* ~n ~n))
(definline sqr-i [n] `(* ~n ~n))
(defn sqr-fwi
{:inline (fn [n] `(do (println "inline:") (* ~n ~n)))}
[n] (do (println "function:") (* n n)))
@kohyama
kohyama / README.md
Last active October 13, 2015 19:18
Project Euler Problem 10

Project Euler Problem 10 解答とコメント

#clojure 入門者向け勉強会 #mitori_clj 第二週担当分

Project Euler Problem 10
2,000,000 未満の素数の総和を求めよ. とのことです.

結果を出すために手に入るリソースを有効活用するという考えのもとでは --この問題自体の解答を述べている

@kohyama
kohyama / plexho.clj
Created November 8, 2012 16:30
Hanoi Operations
(defn hanoi-ops [s t d n]
(if (zero? n) '()
(concat
(hanoi s d t (dec n))
(list [n s d])
(hanoi t s d (dec n)))))
(hanoi-ops 'A 'B 'C 4)
; -> ([1 A B] [2 A C] [1 B C] [3 A B] [1 C A] [2 C B] [1 A B] [4 A C]
; [1 B C] [2 B A] [1 C A] [3 B C] [1 A B] [2 A C] [1 B C])
@kohyama
kohyama / synhl.scm
Created November 5, 2012 05:40
Syntax Highlighter in Gauche
(define-module synhl
(use text.tree)
(export highlight))
(select-module synhl)
;(use slib)
;(require 'trace)
(define (mk-tokenizer :rest args)
(letrec
@kohyama
kohyama / plex03.clj
Last active October 12, 2015 01:38
Exercise 03 Sudoku Level 0
(require '[clojure.set :refer (difference)])
(defn sudoku-level0 [coll]
(let [ics (vec (map #(if % #{%} (set (range 1 10))) coll))
cnb (fn [cs i]
(set
(keep #(if (= 1 (count %)) (first %))
(concat
(map first (partition-all 1 9 (drop (mod i 9) cs)))
(take 9 (drop (* 9 (quot i 9)) cs))
@kohyama
kohyama / plex02.clj
Created October 23, 2012 06:01
Excercise 02 Fibonacci Sequence
(defn fibs [a b]
(cons a (lazy-seq (fibs b (+ a b)))))
(take 100 (fibs 1N 1N))
; -> (1N 1N 2N 3N 5N 8N 13N 21N 34N 55N 89N 144N 233N 377N 610N 987N
; 1597N 2584N 4181N 6765N 10946N 17711N 28657N 46368N 75025N
; 121393N 196418N 317811N 514229N 832040N 1346269N 2178309N
; 3524578N 5702887N 9227465N 14930352N 24157817N 39088169N
; 63245986N 102334155N 165580141N 267914296N 433494437N
; 701408733N 1134903170N 1836311903N 2971215073N 4807526976N
@kohyama
kohyama / plex01.clj
Created October 23, 2012 05:37
Exercise 01 FizzBuzz
(defn fizz-buzz [n]
(map #(cond (zero? (mod % 15)) "FizzBuzz"
(zero? (mod % 5)) "Buzz"
(zero? (mod % 3)) "Fizz"
:else %)
(range 1 (inc n))))
(fizz-buzz 100)
; -> (1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"
; 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz"
@kohyama
kohyama / plex02.md
Created October 22, 2012 06:45
Exercise 02 Fibonacci Sequence - JavaScript
@kohyama
kohyama / hello.s
Created August 20, 2012 08:18
'Hello World' in MacOS X with 'as' and 'ld'
# Usage:
# Save this file as 'hello.s'.
# $ as -o hello.o hello.s
# $ ld -o hello hello.o
# $ ./hello
#
# How to call a system call
# 1) Set rax to the index of the system call.
# 2) Set rdi, rsi, rdx, ... to arguments in this order.
# 3) Call 'syscall'.