Are you tired of calling .freeze
on your data structures (or your colleagues forgetting to do so)?
Do you wish Ruby had a literal for creating immutable arrays?
Then persistent-π aka persistent diamond is for you!
Persistent-π gives you a very tidy way of creating immutable…
-
Arrays:
my_array = a
-
Hashes:
my_hash = h
-
Sets:
my_set = s
…and it behaves as you expect it to:
-
You can compare immutable data structures with regular Ruby instances
a
-
You can compare immutable hashes with
<
/<=
/>=
/>
and with regular Ruby hashes:h
-
You can compare immutable sets with
<
/<=
/>=
/>
and with regular Ruby sets:s
-
You can splat (
*
) immutable arrays:def sum(a, b, c) a + b + c end sum(*a
-
You can double-splat (
**
) immmutable hashes:def hello(name:, age:) "Hello there #{name}, you are #{age} years old!" end hello(h
Beyond being immutable, these data structures are thread-safe, and can be efficiently copied: when you "update" them, a new copy gets created that shares most of its structure with the original. Thus, creating new instances from existing structures is both memory-efficient and quite fast!
It also (optionally!) interoperates with the concurrent-ruby gem, for when you need that extra Oomph (or just thread-safe mutability). See below for more details.
Underneath the covers, persistent-π mostly builds atop the awesome hamster gem. Big thanks to its equally-awesome authors!
Persistent-π is fully supported and tested on Ruby versions 1.9.3 to 3.2, JRuby 1.7 to 9.4, and TruffleRuby π. If we don’t support your Ruby, it’s probably a Python binary instead (or a potato?). Keep calm and π away!
Contents
Installation
Add this line to your application’s gems.rb
or Gemfile
:
gem 'persistent-dmnd'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install persistent-dmnd
This gem is versioned according to Semantic Versioning.
Usage
To use persistent-π, first load it:
require 'persistent-π'
# note: you can also use require 'persistent-dmnd'
Persistent-π can be added as a module to individual classes (or even to other modules!):
class FooController
include Persistent
Or you can add it to your whole application by just doing
require 'persistent_dmnd/everywhere'
a
As you may have noticed, everywhere there is an π
, you can replace it with dmnd
, e.g. PersistentDmnd
instead of Persistentπ
for the gem module and for aDmnd[]
instead of aπ[]
to create an array.
Creating new persistent structures
Array
Use aπ[]
(or aDmnd[]
) to create a new array:
empty_array = a
Hash
Use hπ[]
(or hDmnd[]
) to create a new hash:
empty_hash = h
Set
Use sπ[]
(or sDmnd[]
) to create a new set:
empty_set = s
Converting from existing structures
You can use πify[]
(or dmndify[]
) to convert any received argument to a persistent structure (without modifying the original).
It is great for getting a protected copy of your input, that you can now store, operate on and share among threads without concern.
It works for all the persistent structures above:
my_array = a
It works for regular Ruby arrays (and any object that implements to_ary()
):
my_array = [:regular, :ruby, :array]
It works for regular Ruby hashes (and any object that implements to_hash()
):
my_hash = {regular: :ruby, hash: nil}
It works for regular Ruby sets (and any object that implements to_set()
):
my_set = Set.new([:regular, :ruby, :set])
And it works for hamster gem (Hamster::Vector
, Hamster::Hash
, Hamster::Set
) and concurrent-ruby gem (Concurrent::Array
, Concurrent::Tuple
, Concurrent::Hash
, Concurrent::Map
) data structures:
my_vector = Hamster::Vector[1, 2, 3]
And you can even implement it on your own classes:
class MyList
include Persistent
Converting to regular Ruby structures
The usual to_a()
/to_h()
/to_set()
can be used to convert persistent data structures back to their regular Ruby counterparts:
a
Converting between persistent structures
All three persistent structures implement to_aπ()
(or to_aDmnd()
), to_hπ()
(or to_hDmnd()
) and to_sπ()
(or to_sDmnd()
) as persistent counterparts for the usual Ruby to_a()
, to_h()
and to_s()
:
a
Concurrent Ruby interoperability
When you need to go from thread-safe immutable data structures to thread-safe mutable data structures you can use Persistent-π's optional interoperability with the concurrent-ruby gem.
You’ll need to install concurrent-ruby first, see https://github.com/ruby-concurrency/concurrent-ruby#installation for instructions.
Note
|
If you’re using TruffleRuby, you’ll need to use concurrent-ruby >= 1.1.0, as older versions did not work correctly on TruffleRuby. |
After that, you’ll be able to:
Array
Use to_concurrent()
(or to_concurrent_array()
) to convert your array into a Concurrent::Array
:
my_array = a
Use to_concurrent_tuple()
to convert your array into a Concurrent::Tuple
:
my_array = a
Hash
Use to_concurrent()
(or to_concurrent_hash()
) to convert your hash into a Concurrent::Hash
:
my_hash = h
Use to_concurrent_map()
to convert your hash into a Concurrent::Map
:
my_hash = h
API documentation for the persistent structures
Because the persistent structures are provided by the awesome hamster gem, you can refer back to Hamster’s API docs for details on the operations provided by each data structure.
AAARGH YOU FIEND WHY IS THERE AN EMOJI ON MY CODEBASE?
Every printable ascii character is already in use by Ruby, but I didn’t want persistent data structures to clutter my source code. I also did not want to use cryptic single, two-letter or three-letter acronyms. Ruby is supposed to be beautifully readable!
Thus, I kept my Ruby beautiful. With two very clear characters you can create an immutable data structure. No more awkward typing of namespaces. No more .freeze
everywhere. No-one will ever mistake the use of π
for another operation.
Alas, you may not agree with me! The good news is that you can avoid having π
on your codebase altogether: just use dmnd
as a replacement, as documented above.
If you’re having a hard time typing the emoji, I recommend just adding a quick snippet to your editor or a quick command to search-and-replace aDmnd
/hDmnd
/sDmnd
/dmndify
for aπ
/hπ
/sπ
/πify
. That way you get best of both worlds: easy to type, and beautiful to read!
Usage on Ruby 1.9
Because of our usage of emojis for method names, you’ll need to add
# encoding: UTF-8
as the first (or second) line of any file that uses Persistent-π. As an alternative, you can also use the dmnd
syntax.
This setting is the default from Ruby 2.0 on, so users of later versions do not need to worry about this small detail.
Development
After checking out the repo, run bundle install
to install dependencies. Then, run rake spec
to run the tests.
To open a console with the gem loaded, run bundle console
.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Feedback and success stories
Your feedback is welcome!
Please leave a comment on https://gitlab.com/ivoanjo/persistent-dmnd/issues/8 and share how persistent-π delighted (or disappointed? π±) you today!
Contributing
Bug reports and pull requests are welcome on GitLab at https://gitlab.com/ivoanjo/persistent-dmnd.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Maintained with πβ€οΈ by Ivo Anjo.
Thanks
Thanks to these amazing people for their contributions!
-
JoΓ£o Fernandes (@jcmfernandes)
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Persistent-π projectβs codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Interesting links
Interested in immutable/persistent data structures? Here are some interesting resources for your exploration: