Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ztellman/proteus
local. mutable. variables.
https://github.com/ztellman/proteus
Last synced: about 1 month ago
JSON representation
local. mutable. variables.
- Host: GitHub
- URL: https://github.com/ztellman/proteus
- Owner: ztellman
- Archived: true
- Created: 2013-07-12T01:04:44.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-10-24T14:41:02.000Z (about 8 years ago)
- Last Synced: 2024-10-28T13:39:10.808Z (2 months ago)
- Language: Clojure
- Size: 444 KB
- Stars: 112
- Watchers: 5
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-clojure - proteus - introduce mutable variables (don't use, just learn how to map your mind into immutable world) (Awesome macros usage)
README
![](docs/the-blob.jpg)
Proteus gives you back the local mutable variables you've so dearly missed.
## usage
Add this to your `project.clj`:
```clj
[proteus "0.1.6"]
```Proteus exposes a single macro, `proteus/let-mutable`:
```clj
(use 'proteus)(let-mutable [x 0]
(dotimes [_ 100]
(set! x (inc x)))
x)
````let-mutable` gives you variables that can be set using `set!` within the scope. Since it's unsynchronized and doesn't box numbers, it's faster (often significantly) than Clojure's volatiles, atoms, or refs. However, these variables cannot escape the local scope; if passed into a function or closed over, the current value of the variable will be captured. This means that even though this is unsynchronized mutable state, there's no potential for race conditions.
Unless, of course, you want there to be. It can be sometimes useful to close over the variable rather than the value, for instance when trying to communicate more than the new value from within a `swap!` call.
```clj
(let [a (atom 0)]
(let-mutable [x :foo](swap! a
^:local
(fn [a]
(set! x :bar)
(inc a)))x))
```Here we've hinted the closure as `:local`, meaning that it's only called within the local scope. Since this is true for `swap!`, we can safely use the mutable variable as a side-channel for communication. However, using `:local` on a non-local closure may have strange, reality-defying effects. Use at your own risk.
That's it. That's the end of the library.
## license
Copyright © 2013 Zachary Tellman
Distributed under the MIT License.