Created
July 23, 2020 08:19
-
-
Save jiro4989/1bf806b1b745a7f0f555761928f0f476 to your computer and use it in GitHub Desktop.
引数に応じて関数を切り替えるだけ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; https://github.com/jiro4989/joyn の処理をClojureで書こうとして途中で飽きた | |
| (ns joyn-clj.core | |
| (:require [clojure.string :as str]) | |
| (:require [clojure.edn :as edn])) | |
| (defn char-to-fields | |
| "1-15とかを1,2,3...15にする" | |
| [ch] | |
| (->> (str/split ch #",") | |
| (map edn/read-string) | |
| (map dec))) | |
| (defn get-str-with-char | |
| "" | |
| [s poses] | |
| (->> poses | |
| (map #(nth s %)) | |
| (apply str))) | |
| (defn cut-char | |
| "Cut line with char" | |
| [s ch] | |
| (get-str-with-char s (char-to-fields ch))) | |
| (defn cut-field | |
| "Cut line with char" | |
| [s delim field] | |
| (let [x (str/split s delim)] | |
| (if (<= (count x) field) | |
| "" | |
| (nth x field)))) | |
| (defn do-action | |
| [s param] | |
| (case (:action param) | |
| :cut (cond | |
| (< 0 (count (:ch param))) (cut-char s (:ch param)) | |
| (< 0 (:field param)) (cut-field s (:delim param) (:field param)) | |
| :else nil) | |
| :grep 2 | |
| nil)) | |
| (do-action "こんにち わーるど" | |
| {:action :cut | |
| :ch "" | |
| :field 1 | |
| :delim #" "}) | |
| (do-action "こんにち わーるど" | |
| {:action :cut | |
| :ch "1,2,4" | |
| :field 0 | |
| :delim #" "}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment