Skip to content

A very simple library (function) for updating atoms and returning values

License

Notifications You must be signed in to change notification settings

beoliver/swap-and-return

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swap-and-return

A very simple library (well, it's just a function) for updating atoms and returning values

Clojars Project

Usage

(ns thread-safe-queues.core
  (:require [swap-and-return.core :as s]))

(defn queue [& elems]
  (atom (into [] elems)))

(defn push!!
  [queue val]
  (swap! queue conj val) true)

;; for the pop operation we want to return the popped value to the calling thread

(def pop!!
  (partial s/swap-and-return!
           empty?
           (fn [xs] {::s/atom (subvec xs 1)
                     :val (first xs)
                     :some-key "some other data"})))

(def q (apply queue [1 2 3 4]))

> (pop!! q)
{:val 1, :some-key "some other data"}
> (pop!! q)
{:val 2, :some-key "some other data"}
> (pop!! q)
{:val 3, :some-key "some other data"}
> (pop!! q)
{:val 4, :some-key "some other data"}
> (pop!! q)
nil
> (push!! q 5)
true
> (pop!! q)
{:val 5, :some-key "some other data"}
> (pop!! q)
nil

About

A very simple library (function) for updating atoms and returning values

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published