Skip to content

Instantly share code, notes, and snippets.

View knjname's full-sized avatar
๐Ÿ’ญ
๐Ÿ™

knjname knjname

๐Ÿ’ญ
๐Ÿ™
View GitHub Profile
;; simple
(defn factorial [n]
(if (zero? n)
1
(* n (factorial (dec n)))))
;; tail recursion
(defn factorial
([n] (factorial n 1))
([n sum]
@knjname
knjname / .gitignore
Last active December 19, 2015 12:59
Incanter Example
/target
/lib
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
.lein-deps-sum
.lein-failures
(declare ^:dynamic *asdf*)
(let
[b (binding [*asdf* 10]
(for [i (range 0 3)]
(* *asdf* i)))]
b)
;;; => java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to java.lang.Number
(let
@knjname
knjname / named-pipe.sh
Last active December 20, 2015 17:09
ๅๅ‰ไป˜ใใƒ‘ใ‚คใƒ—ใ‚’้ฉๅฝ“ใซไฝฟใฃใฆใฟใ‚ˆใ†๏ผ
#!/bin/bash
# named pipe ็”Ÿๆˆ๏ผ
mkfifo hoge_pipe
# ๅฎŸ้š›ใซใใฎใƒ•ใ‚กใ‚คใƒซใŒๅญ˜ๅœจใ—ใฆใ„ใ‚‹
ls -l hoge_pipe
# named pipe ใ‹ใ‚‰ใฎๅ…ฅๅŠ›ใฎ่กŒๆ•ฐใ‚’ๆธฌใ‚‹ใ‚ˆใ†ใซใ‚ปใƒƒใƒ†ใ‚ฃใƒณใ‚ฐ
wc -l < hoge_pipe &
#!/bin/bash
# bash executes this incorrectly.
# On the other hand zsh executes this correctly.
# Because bash spawns a sub-shell inside while-do loop meanwhile zsh doesn't it.
declare -i sum
seq 0 10 | while read num ; do
let sum+=$num
done
#!/bin/bash
foldl(){
local func="$1"
local prev="$2"
while read cur ; do
prev="$(eval $func "$prev" "$cur")"
done
echo $prev
}
#!/bin/bash
seq 0 10 | {
declare -i sum
while read num ; do
let sum+=$num
done
# bash => 55 (^^)v
# zsh => 55
;;; call/cc ใ‚’็†่งฃใ—ใ‚ˆใ†๏ผ
;; ไธ€่ฆ‹ใ™ใ‚‹ใจๆ™ฎ้€šใซใ‹ใ‘ใฌใ‘ใฆใ„ใ‚‹
(call/cc (lambda (c) "Just invoke")) ; "Just invoke"
(define boundval (call/cc (lambda (c) "Just invoke")))
boundval ; "Just invoke
;; ใ ใŒโ€ฆ
(defun dividable (n m)
(= (mod n m) 0))
(dividable 6 3) ; => t
(dividable 6 4) ; => nil
(defun euler-1 (begin end)
(loop for i from begin to (1- end)
sum (if (or (dividable i 3) (dividable i 5))
(defun parent-directory (dir)
(unless (equal "/" dir)
(file-name-directory (directory-file-name dir))))
(defun find-nearest-file (dir-path filename)
(let ((file (concat dir-path filename))
(parent (parent-directory (expand-file-name dir-path))))
(if (file-exists-p file)
file
(when parent