From bb7ca94369e9a60baaf620bc4e1f0dfebeaa3349 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 16 May 2016 20:52:48 -0500 Subject: [PATCH] Add Define Something Only Once as a clojure til --- README.md | 3 ++- clojure/define-something-only-once.md | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 clojure/define-something-only-once.md diff --git a/README.md b/README.md index 3cf8616..d50aa43 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_418 TILs and counting..._ +_419 TILs and counting..._ --- @@ -44,6 +44,7 @@ _418 TILs and counting..._ - [Aggregation Using merge-with](clojure/aggregation-using-merge-with.md) - [Argument Requirements For A Function](clojure/argument-requirements-for-a-function.md) - [Combinations Of Items From A Sequence](clojure/combinations-of-items-from-a-sequence.md) +- [Define Something Only Once](clojure/define-something-only-once.md) - [Evaluate One Liners With lein-exec](clojure/evaluate-one-liners-with-lein-exec.md) - [Expanding Macros](clojure/expanding-macros.md) - [Get The Value Of An Environment Variable](clojure/get-the-value-of-an-environment-variable.md) diff --git a/clojure/define-something-only-once.md b/clojure/define-something-only-once.md new file mode 100644 index 0000000..dad48a2 --- /dev/null +++ b/clojure/define-something-only-once.md @@ -0,0 +1,22 @@ +# Define Something Only Once + +Clojure provides [`defonce`](https://clojuredocs.org/clojure.core/defonce) +which is a macro that defines something only once. Once a variable has been +bound to a value, subsequent attempts to do `defounce` for that variable +will go unevaluated. This will have no implications for how the `def` +special form works. + +Here is an example: + +```clojure +(defonce stuff 5) +#'user/stuff +user=> (defonce stuff "what") +nil +user=> stuff +5 +user=> (def stuff "okay") +#'user/stuff +user=> stuff +"okay" +```