Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / FAM.md
Last active January 28, 2024 11:51
ファンクタ, アプリカティブ, モナド

ファンクタ, アプリカティブ, モナド

はじめに

「そもそも概念が分からない」という方に向けた説明です.
簡略化のため大幅に説明を省略しています. ご容赦ください.
誤りは御指摘いただければ幸いです.

「ファンクタ」, 「アプリカティブ」, 「モナド」 などは Haskell に限定された概念・用語ではありませんが,

@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 / stm.md
Last active February 9, 2023 05:35
Clojure ref, atom, agent の要約
@kohyama
kohyama / primes.clj
Created September 28, 2015 05:55
Prime numbers in Clojure
(def prime-numbers
((fn f [x]
(cons x
(lazy-seq
(f (first
(drop-while
(fn [n]
(some #(zero? (mod n %))
(take-while #(<= (* % %) n) prime-numbers)))
(iterate inc (inc x))))))))
@kohyama
kohyama / factorize.clj
Last active May 18, 2022 02:20
Prime Factorization in Clojure
(defn factorize [n]
((fn f [n [h & r :as ps]]
(cond (< n 2) '()
(zero? (mod n h)) (cons h (lazy-seq (f (quot n h) ps)))
:else (recur n r)))
n prime-numbers)))
@kohyama
kohyama / README.md
Last active February 6, 2021 12:41
A minimum setting to use browser REPL of ClojureScript

A minimum setting to use browser REPL of ClojureScript

Assumed that you have set leiningen up and can use it.

1. Prepare files

Copy project.clj, repl-test.cljs and brepl-test.html from this gist or git clone this gist and move or copy repl-test.cljs under src directory.

$ git clone https://gist.github.com/6183122.git
$ cd 6183122/
@kohyama
kohyama / README.md
Last active March 3, 2018 20:44
Example to operate DOM in ClojureScript via Browser REPL
@kohyama
kohyama / eeos.md
Last active October 23, 2017 02:41
外部から文字列で与えた式を, 内部に出て来る変数を実行時に決まる値で束縛した状態で評価したい. I want evaluate a expression which comes from outside as a string with some variables binded to values determined at runtime.
(def s "(- a b)"))

のような文字列があるとき, 実行時に決まる値で a や b を束縛して, この式を評価したいとする.

(let [a 4 b 3] (eval (read-string s)))
@kohyama
kohyama / tails.clj
Last active October 19, 2017 09:05
get a seq of tail seqs for a seq, not including '()
(require '[clojure.test :refer (with-test is run-tests)])
(with-test
(defn tails [coll]
(take-while seq (iterate rest coll)))
(is (= (tails '(:a :b :c))
'((:a :b :c) (:b :c) (:c)))))
@kohyama
kohyama / map-between.clj
Last active October 19, 2017 09:05
apply a function to an element and the next element of it in a sequence
(require '[clojure.test :refer (with-test are run-tests)])
(with-test
(defn map-between [f s]
(map (fn [[a b]] (f a b)) (partition 2 1 s)))
(are [f s _ r] (= (map-between f s) r)
#(- %2 %1) '() -> '()
#(- %2 %1) '(0) -> '()
#(- %2 %1) '(0 1) -> '(1)