Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Malabarba/lazy-map-clojure
Create Clojure maps whose values are only calculated when accessed, either from data or from java objects.
https://github.com/Malabarba/lazy-map-clojure
Last synced: 3 months ago
JSON representation
Create Clojure maps whose values are only calculated when accessed, either from data or from java objects.
- Host: GitHub
- URL: https://github.com/Malabarba/lazy-map-clojure
- Owner: Malabarba
- License: epl-1.0
- Created: 2015-06-24T15:22:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-05-27T01:43:32.000Z (over 4 years ago)
- Last Synced: 2024-07-06T16:23:24.927Z (4 months ago)
- Language: Clojure
- Size: 70.3 KB
- Stars: 49
- Watchers: 5
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
- awesome-clojure - Lazy Map
README
#+OPTIONS: toc:nil num:nil
#+TITLE: lazy-map [[https://travis-ci.org/Malabarba/lazy-map-clojure?branch%3Dmaster][https://secure.travis-ci.org/Malabarba/lazy-map-clojure.png?branch=master]] [[https://dry-clojure.herokuapp.com/repo/Malabarba/lazy-map-clojure/heads/master][https://img.shields.io/badge/dryness-68-97CA00.svg]] [[https://coveralls.io/github/Malabarba/lazy-map-clojure?branch=master][https://coveralls.io/repos/Malabarba/lazy-map-clojure/badge.svg?branch=master&service=github]]Create maps whose values are only calculated when accessed, either from data or from java objects.
Supports both Clojure and Clojurescript![[http://clojars.org/malabarba/lazy-map][file:https://clojars.org/malabarba/lazy-map/latest-version.svg]]
[[http://malabarba.github.io/lazy-map-clojure/][Documentation]]
** The [[http://malabarba.github.io/lazy-map-clojure/lazy-map.core.html#var-lazy-map][lazy-map]]
This macro is analogous to ~lazy-seq~. It takes a map (instead of a
seq), but the value expressions you write aren't actually evaluated
until they are accessed.#+BEGIN_SRC clojure
user> (def my-map
(lazy-map
{:cause (do (println "Getting Cause")
:major-failure)
:name (do (println "Getting Name")
"Some Name")}))
#'user/my-mapuser> (:name my-map)
Getting Name
"Some Name"user> (:name my-map)
"Some Name"user> (:cause my-map)
Getting Cause
:major-failureuser> (:cause my-map)
:major-failure
#+END_SRCYou can also assoc new keys into a LazyMap like a regular Clojure map.
If you assoc a ~delay~ it will act as a lazy value, and if you assoc
anything else it acts as a regular value.#+BEGIN_SRC clojure
user> (def new-map (-> (assoc my-map :surname "Malabarba")
(assoc :delayed-surname
(delay (println "Resolved")
"Late Malabarba"))))
#'user/new-map
user> (:surname my-map)
"Malabarba"
user> (:delayed-surname my-map)
Resolved
"Late Malabarba"
#+END_SRC** The [[http://malabarba.github.io/lazy-map-clojure/lazy-map.iop.html#var-extend-lazy-map][to-lazy-map]] protocol
This protocol allows you to convert any java class into a lazy map,
where each entry correponds to a method call. Since everything is
lazy, you can rest assured the methods won’t actually be called until
you use them.#+BEGIN_SRC clojure
user> (use 'lazy-map.iop)
nil
user> (extend-lazy-map String)
niluser> (to-lazy-map "My Own Map!")
{:to-char-array #object[clojure.lang.Delay 0x5c3c775a {:status :pending, :val nil}],
:empty? #object[clojure.lang.Delay 0x774f63f2 {:status :pending, :val nil}],
:to-string #object[clojure.lang.Delay 0x4a62ed8c {:status :pending, :val nil}],
:intern #object[clojure.lang.Delay 0x4ddc7018 {:status :pending, :val nil}],
:chars #object[clojure.lang.Delay 0x72e5585e {:status :pending, :val nil}],
:class #object[clojure.lang.Delay 0x7e39e503 {:status :pending, :val nil}],
:length #object[clojure.lang.Delay 0x236a69c5 {:status :pending, :val nil}],
:trim #object[clojure.lang.Delay 0xd988100 {:status :pending, :val nil}],
:bytes #object[clojure.lang.Delay 0x55671f45 {:status :pending, :val nil}],
:code-points #object[clojure.lang.Delay 0x64c7f917 {:status :pending, :val nil}],
:to-lower-case #object[clojure.lang.Delay 0x1493800b {:status :pending, :val nil}],
:hash-code #object[clojure.lang.Delay 0x5d4a8318 {:status :pending, :val nil}],
:object #object[clojure.lang.Delay 0x30ba32c3 {:status :pending, :val nil}],
:to-upper-case #object[clojure.lang.Delay 0x6b6e6a82 {:status :pending, :val nil}]}user> (:to-upper-case *1)
"MY OWN MAP!"
#+END_SRCNote how there’s an entry for each method. Obviously, only methods
that takes no arguments (0-arity) are included.There’s also an extra ~:object~ entry holding the string itself.
** License
Copyright © 2015 Artur Malabarba
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.