|
150 | 150 | #^{:arglists '([obj])
|
151 | 151 | :doc "Returns the metadata of obj, returns nil if there is no metadata."}
|
152 | 152 | meta (fn meta [x]
|
153 |
| - (if (instance? clojure.lang.IObj x) |
154 |
| - (. #^clojure.lang.IObj x (meta))))) |
| 153 | + (if (instance? clojure.lang.IMeta x) |
| 154 | + (. #^clojure.lang.IMeta x (meta))))) |
155 | 155 |
|
156 | 156 | (def
|
157 | 157 | #^{:arglists '([#^clojure.lang.IObj obj m])
|
|
1047 | 1047 | [sym] (. clojure.lang.Var (find sym)))
|
1048 | 1048 |
|
1049 | 1049 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Refs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
| 1050 | +(defn #^{:private true} |
| 1051 | + setup-reference [r options] |
| 1052 | + (let [opts (apply hash-map options)] |
| 1053 | + (when (:meta opts) |
| 1054 | + (.resetMeta r (:meta opts))) |
| 1055 | + (when (:validator opts) |
| 1056 | + (.setValidator r (:validator opts))) |
| 1057 | + r)) |
| 1058 | + |
1050 | 1059 | (defn agent
|
1051 |
| - "Creates and returns an agent with an initial value of state and an |
1052 |
| - optional validate fn. validate-fn must be nil or a side-effect-free fn of |
1053 |
| - one argument, which will be passed the intended new state on any state |
| 1060 | + "Creates and returns an agent with an initial value of state and |
| 1061 | + zero or more options (in any order): |
| 1062 | + |
| 1063 | + :meta metadata-map |
| 1064 | + |
| 1065 | + :validator validate-fn |
| 1066 | +
|
| 1067 | + If metadata-map is supplied, it will be come the metadata on the |
| 1068 | + agent. validate-fn must be nil or a side-effect-free fn of one |
| 1069 | + argument, which will be passed the intended new state on any state |
1054 | 1070 | change. If the new state is unacceptable, the validate-fn should
|
1055 |
| - throw an exception." |
| 1071 | + return false or throw an exception." |
1056 | 1072 | ([state] (new clojure.lang.Agent state))
|
1057 |
| - ([state validate-fn] (new clojure.lang.Agent state validate-fn))) |
| 1073 | + ([state & options] |
| 1074 | + (setup-reference (agent state) options))) |
1058 | 1075 |
|
1059 | 1076 | (defn send
|
1060 | 1077 | "Dispatch an action to an agent. Returns the agent immediately.
|
|
1119 | 1136 | [] (. clojure.lang.Agent shutdown))
|
1120 | 1137 |
|
1121 | 1138 | (defn ref
|
1122 |
| - "Creates and returns a Ref with an initial value of x and an optional validate fn. |
1123 |
| - validate-fn must be nil or a side-effect-free fn of one argument, which will |
1124 |
| - be passed the intended new state on any state change. If the new |
1125 |
| - state is unacceptable, the validate-fn should throw an |
1126 |
| - exception. validate-fn will be called on transaction commit, when |
1127 |
| - all refs have their final values." |
| 1139 | + "Creates and returns a Ref with an initial value of x and zero or |
| 1140 | + more options (in any order): |
| 1141 | + |
| 1142 | + :meta metadata-map |
| 1143 | + |
| 1144 | + :validator validate-fn |
| 1145 | +
|
| 1146 | + If metadata-map is supplied, it will be come the metadata on the |
| 1147 | + ref. validate-fn must be nil or a side-effect-free fn of one |
| 1148 | + argument, which will be passed the intended new state on any state |
| 1149 | + change. If the new state is unacceptable, the validate-fn should |
| 1150 | + return false or throw an exception. validate-fn will be called on |
| 1151 | + transaction commit, when all refs have their final values." |
1128 | 1152 | ([x] (new clojure.lang.Ref x))
|
1129 |
| - ([x validate-fn] (new clojure.lang.Ref x validate-fn))) |
| 1153 | + ([x & options] (setup-reference (ref x) options))) |
1130 | 1154 |
|
1131 | 1155 | (defn deref
|
1132 | 1156 | "Also reader macro: @ref/@agent/@var/@atom Within a transaction,
|
|
1135 | 1159 | or atom, returns its current state."
|
1136 | 1160 | [#^clojure.lang.IRef ref] (. ref (get)))
|
1137 | 1161 |
|
1138 |
| -(defn set-validator |
| 1162 | +(defn atom |
| 1163 | + "Creates and returns an Atom with an initial value of x and zero or |
| 1164 | + more options (in any order): |
| 1165 | + |
| 1166 | + :meta metadata-map |
| 1167 | + |
| 1168 | + :validator validate-fn |
| 1169 | +
|
| 1170 | + If metadata-map is supplied, it will be come the metadata on the |
| 1171 | + atom. validate-fn must be nil or a side-effect-free fn of one |
| 1172 | + argument, which will be passed the intended new state on any state |
| 1173 | + change. If the new state is unacceptable, the validate-fn should |
| 1174 | + return false or throw an exception." |
| 1175 | + ([x] (new clojure.lang.Atom x)) |
| 1176 | + ([x & options] (setup-reference (atom x) options))) |
| 1177 | + |
| 1178 | +(defn swap! |
| 1179 | + "Atomically swaps the value of atom to be: |
| 1180 | + (apply f current-value-of-atom args). Note that f may be called |
| 1181 | + multiple times, and thus should be free of side effects. Returns |
| 1182 | + the value that was swapped in." |
| 1183 | + [#^clojure.lang.Atom atom f & args] (.swap atom f args)) |
| 1184 | + |
| 1185 | +(defn compare-and-set! |
| 1186 | + "Atomically sets the value of atom to newval if and only if the |
| 1187 | + current value of the atom is identical to oldval. Returns true if |
| 1188 | + set happened, else false" |
| 1189 | + [#^clojure.lang.Atom atom oldval newval] (.compareAndSet atom oldval newval)) |
| 1190 | + |
| 1191 | +(defn reset! |
| 1192 | + "Sets the value of atom to newval without regard for the |
| 1193 | + current value. Returns newval." |
| 1194 | + [#^clojure.lang.Atom atom newval] (.reset atom newval)) |
| 1195 | + |
| 1196 | +(defn set-validator! |
1139 | 1197 | "Sets the validator-fn for a var/ref/agent/atom. validator-fn must be nil or a
|
1140 | 1198 | side-effect-free fn of one argument, which will be passed the intended
|
1141 | 1199 | new state on any state change. If the new state is unacceptable, the
|
1142 |
| - validator-fn should throw an exception. If the current state (root |
| 1200 | + validator-fn should return false or throw an exception. If the current state (root |
1143 | 1201 | value if var) is not acceptable to the new validator, an exception
|
1144 | 1202 | will be thrown and the validator will not be changed."
|
1145 | 1203 | [#^clojure.lang.IRef iref validator-fn] (. iref (setValidator validator-fn)))
|
|
1148 | 1206 | "Gets the validator-fn for a var/ref/agent/atom."
|
1149 | 1207 | [#^clojure.lang.IRef iref] (. iref (getValidator)))
|
1150 | 1208 |
|
| 1209 | +(defn alter-meta! |
| 1210 | + "Atomically sets the metadata for a namespace/var/ref/agent/atom to be: |
| 1211 | + |
| 1212 | + (apply f its-current-meta args) |
| 1213 | + |
| 1214 | + f must be free of side-effects" |
| 1215 | + [#^clojure.lang.IReference iref f & args] (.alterMeta iref f args)) |
| 1216 | + |
| 1217 | +(defn reset-meta! |
| 1218 | + "Atomically resets the metadata for a namespace/var/ref/agent/atom" |
| 1219 | + [#^clojure.lang.IReference iref metadata-map] (.resetMeta iref metadata-map)) |
| 1220 | + |
1151 | 1221 | (defn commute
|
1152 | 1222 | "Must be called in a transaction. Sets the in-transaction-value of
|
1153 | 1223 | ref to:
|
|
0 commit comments