Created
March 30, 2013 00:14
-
-
Save clojens/5274589 to your computer and use it in GitHub Desktop.
Most useful Clojure one-liners for reference, sanity and general information need of beginning developers.
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
;; Note: for the best result/experience you should download LightTable at www.lighttable.com | |
;; Open the program, press [^-Space] (Control-Space) and type "Insta" then click/press [Return] to open one and paste this in | |
;; Macro to define/create namespace and bind neatly only the required functions (explicit use of :refer key) | |
(ns clj-no.de | |
^{:doc "Namespace and directives for easy inspection of symbols, hash-maps, namespaces and so on." | |
:author "Rob Jentzema" | |
:version "0.01"} | |
;; These provide us with easy access to some very useful functions | |
(:require [clojure.repl :as repl :refer [doc source meta dir]] | |
[clojure.pprint :as pprint :refer [pprint print-table]] | |
[reflect :as reflect :refers [r]])) | |
;; Now use as you see fit. (println) (pprint) (print-table) will show in the green/blue bottom window that you need to expand. | |
;; Automatic promotion in Clojure is possible and desirable since the primary focus is first correctness of numerical values and then raw speed. This *will always* happen so keep it in mind and accommodate this certainty in your code. | |
(def clueless 9) | |
(class clueless) ; it used to be inteyer but now 9 is | |
(class (+ clueless 9000000000000000000)) | |
(class (+ clueless 90000000000000000000)) ; add and remove zeros | |
(comment "In this instarepl, notice the datatype change from Long to BigInt and back") | |
(class (+ clueless 9.0)) ; precision | |
;; Underflow is the inverse of overflow and equally dangerous, it occurs in floating points | |
(float 0.00000000000000000000000000000000000000000000000000000000000000000000000000001) | |
;=> 0.0 | |
1.0+E430 | |
;=> 0.0 | |
;; Be wary of rounding errors! One way to introduce them is by working with doubles and floats | |
;; Some meta stuff | |
(source meta) | |
(doc find-doc) | |
; (print-doc find-doc) ; deprecated? | |
; (struct) (defstruct) ; deprecated! use map! | |
(isa? String "string") | |
(identical? 'goat 'goat) | |
(def x (:members (reflect clojure.lang.BigInt))) | |
(def r (reflect *in*)) | |
(def y (reflect not=)) | |
(print-table [:name :type :flags] (sort-by :name x)) | |
;(apply vector (take 1024 (repeat nil))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment