Skip to content

Instantly share code, notes, and snippets.

View whamtet's full-sized avatar

Matthew Molloy whamtet

View GitHub Profile
@whamtet
whamtet / clojure-gems.clj
Last active March 31, 2016 14:40
Clojure Gems
(ns gems)
(def unique-by
"inverts the range of f"
[f s]
(vals (zipmap (map f s) s)))
(defn decompose-map
"nested map structure -> list of 'address vectors'"
([m] (decompose-map [] m))
(import java.awt.datatransfer.StringSelection)
(import java.awt.Toolkit)
(import java.awt.datatransfer.DataFlavor)
(defn to-clipboard [s]
(let [
selection (StringSelection. s)
clipboard (.getSystemClipboard (Toolkit/getDefaultToolkit))]
(.setContents clipboard selection selection)))
(import System.Windows.Forms.Clipboard)
(Clipboard/SetText "hi")
(require '[excel-repl.interop :as interop])
(require '[clojure.string :as str])
(defn parse [s]
(if (string? s) (-> s (str/replace " " "") Double/Parse)))
(def our-data (map first (interop/get-values "Sheet2" "S1:S1000")))
(def our-members (filter number? our-data))
(defonce persisted-frames (atom {}))
(defn persist [k v]
(swap! persisted-frames assoc k v)
v)
(defn split-until [f s]
(let [[a b] (split-with #(not (f %)) s)]
[(concat a (take 1 b)) (rest b)]))
import clojure.lang.IFn
import org.apache.spark.sql.Row
import clojure.java.api.Clojure
import org.apache.spark.sql.types.StructType
def map_clojure(df: DataFrame, source: String, schema: StructType) = {
var clojure_compiled: IFn = null
val rdd = df.map({row =>
if (clojure_compiled == null) {
clojure_compiled = Clojure.`var`("clojure.core", "load-string").invoke(source).asInstanceOf[IFn]
import org.apache.spark.sql.{SQLContext, DataFrame}
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.execution.{ExplainCommand, QueryExecution}
import org.apache.spark.sql.catalyst.InternalRow
//Sorry about the clojure, we need it to sidestep the Scala typesystem
import clojure.java.api.Clojure
import clojure.lang.IFn
class BetterDataFrame(override val sqlContext: SQLContext, override val logicalPlan: LogicalPlan) extends DataFrame(sqlContext, logicalPlan) {
(def assembly-path "C:\\Users\\cplmam\\Downloads\\microsoft.office.interop.outlook.15.0.4797.1003\\lib\\net20\\Microsoft.Office.Interop.Outlook.dll")
(assembly-load-from assembly-path)
(import Microsoft.CSharp.CSharpCodeProvider)
(import System.CodeDom.Compiler.CompilerParameters)
(def provider (CSharpCodeProvider.))
(def assemblies (into-array [assembly-path]))
(def compilerparams (CompilerParameters. assemblies))
(.set_GenerateExecutable compilerparams false)
private static load_string = clojure.java.api.Clojure.var('clojure.core', 'load-string')
private static localization = load_string.invoke('''
(require '[clojure.java.io :as io])
(defn cljs-resource [path] ;does this work in production?
(io/resource (str "META-INF/assets/taipan_reagent/src/" path)))
(defn needed-messages [path]
(distinct (map second (re-seq #"\\(messages \\"(\\w+?)\\"\\)" (slurp (cljs-resource path))))))
(ns render.format)
(defn standardize [form]
(cond
(vector? form)
(let [[k m & rest] form]
(if (map? m)
(vec (list* k m (map standardize rest)))
(vec (list* k {} (standardize m) (map standardize rest)))))
(seq? form)