Created
August 20, 2013 12:49
-
-
Save Quantisan/6280926 to your computer and use it in GitHub Desktop.
Cascalog field name manipulation convenience methods
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
(ns etl.field | |
"Cascalog field name and variable functions." | |
(:refer-clojure :exclude (replace)) | |
(:use [clojure.string :only (replace)])) | |
(defn replace-field [coll match replacement] | |
(let [idx (.indexOf coll match)] | |
(assoc coll idx replacement))) | |
(defn- agg-replace [suffix] | |
(fn [coll match] | |
(replace-field coll match (str match suffix)))) | |
(defn append-each-field | |
" | |
Append 'suffix' to each of the matching field within 'coll' | |
" | |
[coll matches suffix] | |
(reduce (agg-replace suffix) coll matches)) | |
(defn remove-field [coll match] | |
(let [idx (.indexOf coll match)] | |
(if (<= 0 idx) | |
(vec (concat (subvec coll 0 idx) (subvec coll (inc idx) (count coll)))) | |
coll))) | |
(defn remove-fields [coll & matches] | |
(reduce remove-field coll matches)) | |
(defn add-field [coll field & more] | |
(apply conj coll field more)) | |
(def add-fields add-field) | |
(defn insert-field | |
"Insert new field 'x' at index 'idx' of coll." | |
[coll idx x] | |
(apply conj (subvec coll 0 idx) x (subvec coll idx))) | |
(defn insert-fields [coll idx & add] | |
(reduce #(insert-field %1 idx %2) coll (reverse add))) | |
(defn ensure-field [coll field] | |
(let [idx (.indexOf coll field)] | |
(if (<= 0 idx) | |
coll | |
(add-field coll field)))) | |
(defn to-field [^String s] | |
(let [sub (replace s #"_" "-")] | |
(str \? sub))) | |
(defn to-cascalog-fields [coll] | |
(vec (map to-field coll))) | |
(def var-type? #{:nullable :non-nullable :unground}) | |
(defn var-kwd [kwd] | |
{:pre [(var-type? kwd)]} | |
(case kwd | |
:nullable "!" | |
:non-nullable "?" | |
:unground "!!")) | |
(defn set-var-type [s prefix] | |
(apply str prefix (re-seq #"[^\?\!]+" s))) | |
(defn set-vars-type | |
" | |
Example: | |
=> (set-vars-type [\"?aa\" \"!bb\"] (var-kwd :non-nullable) \"!bb\") | |
[\"?aa\" \"?bb\"] | |
" | |
[coll prefix & matches] | |
(reduce #(replace-field %1 %2 (set-var-type %2 prefix)) coll matches)) | |
(defn contain-field? | |
" | |
Checks if `coll` contains `match` | |
" | |
[coll match] | |
((set coll) match)) | |
(defn contain-fields? | |
" | |
Checks that `coll` contains all the fields of `matches` | |
" | |
[coll & matches] | |
(not-any? nil? (map (partial contain-field? coll) matches))) | |
(defn prepend-fields | |
[coll & fields] | |
(reduce #(insert-field %1 0 %2) coll (reverse fields))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment