Skip to content

Instantly share code, notes, and snippets.

View edlich's full-sized avatar

Edlich edlich

View GitHub Profile
@edlich
edlich / MyClojureErrors
Created January 4, 2015 17:19
All Clojure Errors I made...
- Wrote def instead of defn [1]
- Wrong indentation: Hence the function was nested instead of toplevel [1]
@edlich
edlich / spy.clj
Last active August 29, 2015 14:09
Clojure Spy Macro
(defmacro spy
[& body]
`(let [x# ~@body]
(printf "=> %s = %s\n" (first '~body) x#)
x#))
@edlich
edlich / 0_reuse_code.js
Created June 10, 2014 20:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@edlich
edlich / sport.clj
Created February 11, 2014 15:33
Fitness Tracker to JSON in Clojure
;; (C) Stefan Edlich Eclipse Public License
;; ToDo: Zone should be an int! ;; Null values should be inserted as null !
(import '(java.io File))
(def filepath "complete.json")
(def oldpath "complete-old.json")
(defn sport2json []
"Convert User Input to JSON text"
(print "Types: MULTI, RUN, BIKE, SPIN, POWER, SWIM, TT, WALK, YOGA, SEE, KITE, BODYSURF, FOOTBALL, etc.\n\n")
(let [date (do (print "Date (e.g. 19.10.2014): ") (flush)(read-line)) ;; get today if empty
@edlich
edlich / ClojureVimStateOfTheArt
Last active December 31, 2015 15:49
Current state of the art Clojure with vim description
VIM and Clojure:
The current steps might be (intended for vim beginners).
0A. Install Pathogen for easy vim installation (optional)
- https://github.com/tpope/vim-pathogen
- http://www.vim.org/scripts/script.php?script_id=2332
0B. Install new version of static runtime editing (optional, only if latest version is needed)
- https://github.com/guns/vim-clojure-static
@edlich
edlich / gist:7193080
Created October 28, 2013 08:13
Task: reverse and pad a string.
(def s "Lagergeral") ;; test this in tryclj.com line by line
(print (apply str (concat (reverse "Lagergeral") (repeat (- 20 (count s)) "#"))))
@edlich
edlich / gist:6869051
Created October 7, 2013 14:36
Great Clojure Macro description!
(C) Mikera on Stackoverflow:
http://stackoverflow.com/questions/10434638/good-examples-of-clojure-macros-usage-which-demonstrate-advantages-of-the-langua/10435150
I find macros pretty useful for defining new language features. In most languages you would need to wait for a new release of the language to get new syntax - in Lisp you can just extend the core language with macros and add the features yourself.
For example, Clojure doesn't have a imperative C-style for(i=0 ;i<10; i++) loop but you can easily add one with a macro:
(defmacro for-loop [[sym init check change :as params] & steps]
(cond
(not (vector? params))
@edlich
edlich / EdlTron2.clj
Created January 24, 2013 15:17
Second Try of a simple Tron
(defn edlTron2 [{:keys [dir togo] :or {togo 0}}]
(if (pos? togo)
{:dir dir :togo (dec togo)}
{:dir (rand-nth [:up :down :left :right])
:togo 5}))
@edlich
edlich / mycycle.clj
Created January 1, 2013 23:18
Create a cycle that will not be realized but has the index passed around...
(defn mycycle [mylist index]
(println "Habe " mylist "," index)
(println "Mycycle Element = " (nth mylist index))
(read-line)
(if (>= index (dec (count mylist)))
(mycycle mylist 0)
(mycycle mylist (inc index))))
(mycycle '(1 2 3 4 5 6) 0)
@edlich
edlich / gist:4351510
Last active December 10, 2015 00:29
Bad approach to convert a chess board to the first part of a fen string (and even not ready) Can be dramatically improved by using function compositition.
(defn pos2str [pos fenpos i-count spacecount] ;; Convert a position to the fen position (1st element)
(println ">" (apply str pos) "<")
(println ">" (apply str fenpos) "<")
(println "i-count=" i-count "spacecount" spacecount "piece(" (first pos) ") ---------------")
(read-line)
(let [piece (first pos)]
(if (empty? pos)
fenpos
(if (= i-count 7)
(if (= piece " ") ; nr=7