All Projects → jdevuyst → ruminant

jdevuyst / ruminant

Licence: EPL-1.0 license
Swift persistent vectors à la Clojure

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to ruminant

shoki
Purely functional data structures in Java
Stars: ✭ 30 (-18.92%)
Mutual labels:  persistent-data-structure
Functionaljava
Functional programming in Java
Stars: ✭ 1,472 (+3878.38%)
Mutual labels:  persistent-data-structure
treap
A thread-safe, persistent Treap (tree + heap) for ordered key-value mapping and priority sorting.
Stars: ✭ 23 (-37.84%)
Mutual labels:  persistent-data-structure
Arrow
Λrrow - Functional companion to Kotlin's Standard Library
Stars: ✭ 4,771 (+12794.59%)
Mutual labels:  functional-data-structure
steady vector
Array-like collections optimized for tail manipulation
Stars: ✭ 12 (-67.57%)
Mutual labels:  persistent-vectors

Ruminant

A Swift implementation of Clojure's persistent vectors.

Persistent Vectors

Core operations such as conj, assoc, get (using subscripts), subvec (using subscripts), and concat have been implemented.

let v: PersistentVector = ["a", "b", "c"]
let v2 = v.conj("d").assoc(index: 2, "C")
XCTAssertEqual(v, ["a", "b", "c"])
XCTAssertEqual(v2, ["a", "b", "C", "d"])
XCTAssert(v.pop() == v2[0..<2])
XCTAssertEqual(v.map {$0.uppercased()}, ["A", "B", "C"])
XCTAssertEqual(v[1], "b")
XCTAssertEqual(Array(v[1...2]), ["b", "c"])

Transient vectors are included:

let v: PersistentVector = ["a", "b", "c"]
var tmp = v.transient()
tmp = tmp.pop()
tmp = tmp.conj("3")
tmp = tmp.conj("4")
XCTAssert(tmp.persistent() == ["a", "b", "3", "4"])

Integration

You can use the Swift-Package manager to integrate Ruminant.

Add the following dependency in Package.swift:

dependencies: [
.package(url: "https://github.com/jdevuyst/ruminant", from: "1.0.7")
],

Sample Usage

Here is a sample walkthrough with Swift Package Manager to use this library.

First, create a complete new directory from CLI named "Sample" and cd into it.

mkdir sample
cd sample

Next, create a new exectuable swift template inside this directory.

swift package init --type executable

Now it's time to update Package.swift.

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "sample",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/jdevuyst/ruminant", from: "1.0.7")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "sample",
            dependencies: ["ruminant"])
    ]
)

Let's install the new dependency.

swift package update

We'll also updatee Main.swift to test that Ruminant can be loaded.

import ruminant

print("Hello, Persistent Vector!")

let sample = PersistentVector([1,2,3,4]).conj(45).conj(42)
print(sample)

Finally, we can build and run the program from the command line.

swift build
swift run

Hello, Persistent Vector!
[1, 2, 3, 4, 45, 42]

That's it. Enjoy the world of persistent datastructures!

License

Copyright © 2015 Jonas De Vuyst

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

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].