Skip to content

Instantly share code, notes, and snippets.

(defn maybe-comp
"Composes functions, maybe :). The returned fn returns nil if anyone does so."
([f] f)
([f g]
(fn
([] (when-let [a (g)]
(f a)))
([x & args] (when-let [a (apply g x args)]
(f a)))))
([f g & fs]
@conf8o
conf8o / with_read_lines.clj
Last active December 1, 2020 08:38
A macro evaluates body, like with-open. Names are bound to line-seqs of readers.
(defmacro with-read-lines [reader bindings & body]
(cond
(= (count bindings) 0) `(do ~@body)
(symbol? (bindings 0)) `(let* [rdr# (~reader ~(bindings 1))
~(bindings 0) (line-seq rdr#)]
(try
(with-read-lines ~reader ~(subvec bindings 2) ~@body)
(finally
(. rdr# close))))
:else (throw (IllegalArgumentException.
@conf8o
conf8o / excel_table.clj
Last active March 25, 2021 02:17
Excelワークブックのシートの列を取得
(ns micro-tools.excel
(:require [dk.ative.docjure.spreadsheet :as spreadsheet])
(:import [org.apache.poi.xssf.usermodel XSSFWorkbook XSSFSheet]
[java.lang String]))
(defrecord TableInfo [mapping sheet-name])
(defmulti table (fn [info obj] (class obj)))
(defmethod table XSSFSheet [info sheet]
(->> sheet
@conf8o
conf8o / task.scm
Last active June 1, 2021 09:16
競プロデッキ in Scheme
(use gauche.lazy)
(use gauche.collection)
(use util.match)
(use math.prime)
; bit
(define << ash)
(define & logand)
(define lor logior)
(define xor logxor)
@conf8o
conf8o / file_size_checker.clj
Last active April 8, 2021 02:02
サイズの大きなファイルをチェックするためのツール
(require '[clojure.java.io :as io])
(import '[java.io IOException])
(def base-folder (slurp "scripts/file_size_checker.input.txt"))
(def output-file "scripts/file_size_checker.output.txt")
(defn large-files [folder]
(sort-by #(.length %) >
(->> folder
file-seq
@conf8o
conf8o / inputloop.py
Created May 7, 2021 06:10
ユーザ入力ループ
def input_loop(question, quit):
while not quit(ans := input(question)):
yield ans
for ans in input_loop("Yo? >>", lambda x: x == "quit"):
print("Your input: ", ans)
@conf8o
conf8o / 0.curriedope.py
Last active May 24, 2021 07:18
カリー化演算子(lambdaの仕様理解の旅)
import operator as ope
from functools import partial
lt, le, eq, ne, ge, gt = [(lambda x: partial(o, x)) for o in [ope.lt, ope.le, ope.eq, ope.ne, ope.ge, ope.gt]]
test = [f for f in [lt(1), le(1), eq(1), ne(1), ge(1), gt(1)]]
print(*test, sep="\n")
@conf8o
conf8o / solve.scm
Last active June 1, 2021 09:19
競プロ用入力マクロ
; input
(define (my-read)
(let ([a (read)])
(if (symbol? a)
(symbol->string a)
a)))
(define (my-read-line)
(let ([line (read-line)])
(if (string=? "" line)
(my-read-line)
@conf8o
conf8o / HashedPotatoField.swift
Created June 17, 2021 06:45
ハッシュテーブルの実装
protocol Potato {
func hash() -> Int
}
extension Potato where Self: Hashable {
func hash() -> Int {
return self.hashValue
}
}
@conf8o
conf8o / SwiftAST.swift
Last active June 23, 2021 02:21
SwiftでつくるAST
enum Val {
case int(Int)
case bool(Bool)
case symbol(String)
}
enum Ope {
case add
case mul
case lt