-
-
Save pqr/921aa05195920dc03ec2 to your computer and use it in GitHub Desktop.
Clojure version of http://pqr7.wordpress.com/2014/09/03/haskell-vs-php/
This file contains 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
(require '[leiningen.exec :as le]) | |
(ns foo | |
(:require [clojure.string :as str])) | |
(def source-text "https://gist.githubusercontent.com/pqr/0cf155de6b8f95c3c400/raw/56fc9aa6c06bf3e55a2a02d3e4a857580feff2bc/haskell_vs_php_example.txt") | |
(defn get-lines [text] (map str/trim (str/split-lines text))) | |
(defn drop-header-line [lines] | |
(if (re-find #"Container No\\." (first lines)) | |
(rest lines) | |
lines)) | |
(defn drop-flag-line [lines] | |
(cond | |
(<= (count lines) 20) lines | |
(re-find #"Flag\\:" (nth lines 20)) (concat (take 20 lines) (drop 21 lines)) | |
:else lines)) | |
(defn lines-without-header-and-flag [lines] (-> lines drop-header-line drop-flag-line)) | |
(defn split-per-block | |
([lines] | |
(split-per-block [] lines)) | |
([blocks lines] | |
(cond | |
(< (count lines) 20) blocks | |
(>= (count (lines-without-header-and-flag lines)) 20) (let [first-20-lines (take 20 (lines-without-header-and-flag lines)) | |
other-lines (drop 21 (lines-without-header-and-flag lines)) | |
new-found-blocks (concat blocks first-20-lines)] | |
(split-per-block new-found-blocks other-lines)) | |
:else blocks))) | |
(defn parse [text] (-> text | |
get-lines | |
split-per-block)) | |
(println "Count: " (count (parse (slurp source-text)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment