All Projects → lucaong → Immutable

lucaong / Immutable

Licence: mit
Thread-safe, persistent, immutable collections for the Crystal language

Programming Languages

crystal
512 projects

Projects that are alternatives of or similar to Immutable

Fastcore
Python supercharged for the fastai library
Stars: ✭ 565 (+215.64%)
Mutual labels:  data-structures, functional-programming
Haskell
Stars: ✭ 91 (-49.16%)
Mutual labels:  data-structures, functional-programming
Libgenerics
libgenerics is a minimalistic and generic library for C basic data structures.
Stars: ✭ 42 (-76.54%)
Mutual labels:  data-structures, vector
Program Blog
Practice, thinking and reading
Stars: ✭ 228 (+27.37%)
Mutual labels:  data-structures, functional-programming
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (-30.17%)
Mutual labels:  data-structures, vector
Prelude Ts
Functional programming, immutable collections and FP constructs for typescript and javascript
Stars: ✭ 315 (+75.98%)
Mutual labels:  data-structures, functional-programming
Imtools
Fast and memory-efficient immutable collections and helper data structures
Stars: ✭ 85 (-52.51%)
Mutual labels:  data-structures, functional-programming
Staticvec
Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.
Stars: ✭ 236 (+31.84%)
Mutual labels:  data-structures, vector
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (-32.4%)
Mutual labels:  data-structures, hash
Vector
➿ A supercharged std::vector implementation (minus Allocator)
Stars: ✭ 118 (-34.08%)
Mutual labels:  data-structures, vector
Cyclops
An advanced, but easy to use, platform for writing functional applications in Java 8.
Stars: ✭ 1,180 (+559.22%)
Mutual labels:  data-structures, functional-programming
Codezilla
⚡️ codezilla ⚡️ One giant 🦖 collection of algorithms & design patterns.
Stars: ✭ 127 (-29.05%)
Mutual labels:  data-structures, functional-programming
Scalacaster
Purely Functional Algorithms and Data Structures in Scala
Stars: ✭ 1,342 (+649.72%)
Mutual labels:  data-structures, functional-programming
List
🐆 An immutable list with unmatched performance and a comprehensive functional API.
Stars: ✭ 1,604 (+796.09%)
Mutual labels:  data-structures, functional-programming
Sc
Common libraries and data structures for C.
Stars: ✭ 161 (-10.06%)
Mutual labels:  data-structures, vector
Iota
Fast [co]product types with a clean syntax. For Cats & Scalaz.
Stars: ✭ 175 (-2.23%)
Mutual labels:  functional-programming
Maryamyriameliamurphies.js
A library of Haskell-style morphisms ported to ES2015 JavaScript using Babel.
Stars: ✭ 177 (-1.12%)
Mutual labels:  functional-programming
Algorithms Data Structures In Typescript
Stars: ✭ 175 (-2.23%)
Mutual labels:  data-structures
Algo
Algorithms and data structures implemented in Go, JS, TypeScript, Rust, and Swift.
Stars: ✭ 174 (-2.79%)
Mutual labels:  data-structures
Atomix
A reactive Java framework for building fault-tolerant distributed systems
Stars: ✭ 2,182 (+1118.99%)
Mutual labels:  data-structures

Build Status

Immutable

Efficient, thread-safe immutable data structures for Crystal.

Whenever an Immutable data structure is "modified", the original remains unchanged and a modified copy is returned. However, the copy is efficient due to structural sharing. This makes Immutable data structures inherently thread-safe, garbage collector friendly and performant.

At the moment, Immutable implements the following persistent data structures:

  • Immutable::Vector: array-like ordered, integer-indexed collection implementing efficient append, pop, update and lookup operations
  • Immutable::Map: hash-like unordered key-value collection implementing efficient lookup and update operations

Installation

Add this to your application's shard.yml:

dependencies:
  immutable:
    github: lucaong/immutable

Usage

For a list of all classes and methods refer to the API documentation

To use the immutable collections, require immutable in your code:

require "immutable"

Vector (API docs)

# Vector behaves mostly like an Array:
vector = Immutable::Vector[1, 2, 3, 4, 5]  # => Vector [1, 2, 3, 4, 5]
vector[0]                                  # => 1
vector[-1]                                 # => 5
vector.size                                # => 5
vector.each { |elem| puts elem }

# Updating a Vector always returns a modified copy:
vector2 = vector.set(2, 0)                 # => Vector [1, 2, 0, 4, 5]
vector2 = vector2.push(42)                 # => Vector [1, 2, 0, 4, 5, 42]

# The original vector is unchanged:
vector                                     # => Vector [1, 2, 3, 4, 5]

# Bulk updates can be made faster by using `transient`:
vector3 = vector.transient do |v|
  1000.times { |i| v = v.push(i) }
end

Map (API docs)

# Map behaves mostly like a Hash:
map = Immutable::Map[{:a => 1, :b => 2 }]  # => Map {:a => 1, :b => 2}
map[:a]                                  # => 1

# Updating a Map always returns a modified copy:
map2 = map.set(:c, 3)                      # => Map {:a => 1, :b => 2, :c => 3}
map2 = map2.delete(:b)                     # => Map {:a => 1, :c => 3}

# The original map in unchanged:
map                                        # => Map {:a => 1, :b => 2}

# Bulk updates can be made faster by using `transient`:
map3 = Immutable::Map(String, Int32)[]
map3 = map3.transient do |m|
  1000.times { |i| m = m.set(i.to_s, i) }
end

Nested structures

# Nested arrays/hashes can be turned into immutable versions with the `.from`
# method:

nested = Immutable.from({:name => "Ada", :colors => [:blue, :green, :red] })
nested # => Map {:name => "Ada", :colors => Vector [:blue, :green, :red]}

Implementation

Immutable::Vector is implemented as a bit-partitioned vector trie with a block size of 32 bits, that guarantees O(Log32) lookups and updates, which is effectively constant time for practical purposes. Due to tail optimization, appends and pop are O(1) 31 times out of 32, and O(Log32) 1/32 of the times.

Immutable::Map uses a bit-partitioned hash trie with a block size of 32 bits, that also guarantees O(Log32) lookups and updates.

Contributing

  1. Fork it ( https://github.com/lucaong/immutable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • lucaong Luca Ongaro - creator, maintainer

Acknowledgement

Although not a port, this project takes inspiration from similar libraries and persistent data structure implementations like:

When researching on the topic of persistent data structure implementation, these blog posts have been of great help:

Big thanks to their authors for the great job explaining the internals of these data structures.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].