Skip to content

Instantly share code, notes, and snippets.

View c-garcia's full-sized avatar

cristobal garcia c-garcia

View GitHub Profile
@c-garcia
c-garcia / start-r.sh
Created August 22, 2017 03:52
How to start R in MacOS with java working properly
#!/bin/bash
export DYLD_FALLBACK_LIBRARY_PATH=/Library/Frameworks/R.framework/Resources/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/server
R
@c-garcia
c-garcia / clojure-tools.el
Last active February 14, 2017 19:54
Clojure file predicate with ert tests.
(defun clojure-file-p (file-name)
"Determines if a file-name belongs to a clojure file."
(string-match "clj[csx]?$" file-name))
(ert-deftest hola ()
(should (not (clojure-file-p "hola.cl")))
(should (not (clojure-file-p "")))
(should (not (clojure-file-p "c")))
(should (clojure-file-p "hola.clj"))
(should (clojure-file-p "hola.cljs"))
@c-garcia
c-garcia / clojure_tools.el
Created February 12, 2017 19:44
Guess the file path of an emacs buffer holding a clojure file.
(defun guess-clojure-test-file ()
"Guesses the test file path of the current buffer if it is a clojure file."
(interactive)
(let ((clojure-file-name buffer-file-name))
(with-temp-buffer
(insert clojure-file-name)
(goto-char (point-max))
(search-backward "src")
(replace-match "test")
(goto-char (point-max))
@c-garcia
c-garcia / bash-getopts-example.sh
Created January 5, 2017 08:49
bash getopts example
#!/bin/bash
# first colon turns getopts into silent mode
# that also sets OPTARG when errors happen
#
# options with arguments have a colon AFTER the
# option letter
# in the example, a requires an option, b no
while getopts ":a:b" opt; do
@c-garcia
c-garcia / features.clj
Created April 10, 2016 09:43
How to use cucumber-jvm without lein-cucumber
(ns cucutest.features
(:require [clojure.test :refer :all])
(:import [cucumber.api.cli Main]))
(deftest ^:cucumber run-cucumber
(let [cucumber-args ["--plugin" "progress" "--glue" "test/features/step_definitions" "test/features"]]
(Main/main (into-array cucumber-args))))
@c-garcia
c-garcia / ds.clj
Created December 12, 2015 06:48
Datascript sample
(ns clj-wb.ds
(:require [datascript.core :as d]))
(let [schema {:tags {:db/cardinality :db.cardinality/many}}
conn (d/create-conn schema)]
(d/transact! conn [{:db/id 1
:name "John Doe"
:status :ok
:tags ["cplusplus" "java"]}
{:db/id 2
@c-garcia
c-garcia / forward-request.clj
Last active December 2, 2015 22:23
Receives an HTTP request (a ring-map) and decides if it needs to forward the request to the real server or if it can do it by itself with the data it already has (cache).
(defn process-request
"The heart of the Application Gateway. Checks if word is not _forbidden_ or
_cached_ and if is not the case, it forwards the request to the orignal
server."
[word]
(debug "Received request for word" word)
(cond
(forbidden? word) (forbidden-response word)
(cached? word) (cached-response word)
:else (forward-request-and-cache! word (env :forward-port))))
@c-garcia
c-garcia / appfw.clj
Created December 2, 2015 21:29
Simple web application gateway
;; The Application Gateway
(defroutes appgw
(GET "/uppercase/:text" [text]
(process-request text))
(route/not-found
(-> (r/response "NOT FOUND"))))
@c-garcia
c-garcia / webapp.clj
Created December 2, 2015 21:19
A minimal webapp returning a JSON map of a word passed within the URI (http://host:port/uppercase/:word
(defroutes webapp
(GET "/uppercase/:text" [text]
(do
(debug "Received request for" text)
(-> (r/response (json/write-str {:status :ok :text (s/upper-case text)}))
(r/content-type "application/json"))))
(route/not-found
(do
(debug "Unknown route")
(-> (r/response (json/write-str {:status :err :reason :op-not-found}))
@c-garcia
c-garcia / core.clj
Last active November 9, 2015 22:44
Some experiments with HDFS MapFiles and hdfs-clj
(ns hdp-clj-wb.core
"Some tests with hadoop mapfiles. Example usage.
* Create a file with 1000 keys (from 000 to 999)
(hdp-clj-wb.core/make-map-file \"hdfs://localhost:9000/user/me/map3\" 1000)
* Get the MD5 of the key 235 of 1000
(hdp-clj-wb.core/get-md5 \"hdfs://localhost:9000/user/me/map3\" 234 1000)
* Get 4 items from the key 990 of 1000
(hdp-clj-wb.core/get-some-md5 \"hdfs://localhost:9000/user/me/map3\" 990 1000 4)
"
(:require [hdfs.core :as hdfs])