Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / awtp.clj
Last active October 19, 2017 08:55
お釣りの要らない全ての支払い方
(require '[clojure.test :refer (with-test are run-tests)])
(with-test
(defn awtp [cs a]
((fn f [[h & r] a]
(cond
(zero? a) #{{}}
(nil? h) #{}
:else (set
(mapcat (fn [c]
@kohyama
kohyama / str-drop-while-same.clj
Last active October 19, 2017 08:55
文字列を先頭から見て同じところまで除去 in Clojure
(require '[clojure.test :refer (with-test are run-tests)])
(with-test
(defn str-drop-while-same [& args]
(if (some nil? args) args
(->> (map #(concat % (repeat nil)) args)
(apply map vector)
(drop-while #(apply = %))
(take-while #(some identity %))
(apply map str))))
@kohyama
kohyama / FAM.md
Last active January 28, 2024 11:51
ファンクタ, アプリカティブ, モナド

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

はじめに

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

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

@kohyama
kohyama / puyo.clj
Last active October 19, 2017 06:08
ぷよぷよ連鎖 in Clojure
;;; Copyright (c) 2013 Yoshinori Kohyama. Distributed under the BSD 3-Clause License.
(ns puyo
(:require [clojure.test :refer (with-test run-tests are)]
[clojure.set :refer (union)]
[clojure.string :as string]))
(with-test
(defn- fall-one [b s]
(->> (reverse b)
(apply map vector)
#include <stdio.h>
#include <stdlib.h>
#define u16 unsigned short
#define u8 unsigned char
static u16
crc16(u8 *p, int n)
{
static const u16 wCRCTable[] = {
@kohyama
kohyama / pep018.clj
Last active December 10, 2015 18:09
Project Euler Problem 18
(require '[clojure.test :refer (is)])
(require '[clojure.string :as s])
(def input "
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
@kohyama
kohyama / pep019.clj
Last active December 10, 2015 18:08
Project Euler Problem 19
;; 閏年かどうか
(defn leap? [year]
(and (zero? (mod year 4))
(or (pos? (mod year 100))
(zero? (mod year 400)))))
;; 西暦で与えられた年の各月の日数のリスト
(defn numbers-of-days [year]
(if (leap? year)
;;Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
@kohyama
kohyama / pep011.clj
Last active December 10, 2015 04:28
Project Euler Problem 11
(require '[clojure.string :as s])
(require '[clojure.test :refer (deftest run-tests is)])
(defn trans [[& rows]] (apply (partial map list) rows))
(defn flip [mat] (map reverse mat))
(defn rot45 [mat]
(let [w (count (first mat))
h (count mat)
@kohyama
kohyama / pep024+.clj
Last active December 10, 2015 02:58
Project Euler Problem 24
(require '[clojure.test :refer (is)])
(defn pms
"Permutations lexicographic by index."
[[a & [b & [c & _]] :as ls]]
(cond
(nil? a) '()
(nil? b) `(~a)
(nil? c) `((~a ~b) (~b ~a))
:else (mapcat (fn [h] (map (fn [r] (cons h r))
@kohyama
kohyama / pep017.clj
Created December 19, 2012 10:19
Project Euler Problem 17
(require '[clojure.test :refer (is)])
(defn in-words
"Represents the given number in English words without spaces nor hyphens.
This works with a number in the range from 1 to 1000"
[n]
(cond
(< n 20)
(["" "one" "two" "three" "four" "five" "six" "seven" "eight"
"nine" "ten" "eleven" "twelve" "thirteen" "fourteen"