/elixir-pnum

Concurrent list enumeration

Primary LanguageElixirMIT LicenseMIT

Pnum.ex

Version: 1.0.0-dev Unstable
Master build: Master branch build status
Requires: Elixir >= 0.12.2

Pnum is a simple module for simple concurrent enumeration. When using this library you should note that concurrent list enumeration is not necessarily the fastest or most efficient method, but in some cases it can offer the perfect solution.

It can be installed in whichever way you prefer, but I recommend standard Mixfile project dependencies.

defmodule MyProject.Mixfile do
    #...

    defp deps do
      [{:pnum, github: "adlawson/pnum.ex", tag: "1.0.0-dev" }]
    end
end

Usage

  • filter(collection, func)

@spec filter(t, (item -> as_boolean(term))) :: list

Filters the collection, i.e. returns only those items for which func returns true.

iex> Pnum.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
[2]
  • filter_map(collection, filter, mapper)

@spec filter_map(t, (item -> as_boolean(term)), (item -> item)) :: list

Filter the collection and map values in one pass.

iex> Pnum.filter_map([1, 2, 3], fn(x) -> rem(x, 2) == 0 end, &(&1 * 2))
[4]
  • map(collection, func)

@spec map(t, (item -> any)) :: list

Returns a new collection, where each item is the result of invoking func on each corresponding item of collection.

For dicts, the function expects a key-value tuple.

iex> Pnum.map([1, 2, 3], fn(x) -> x * 2 end)
[2, 4, 6]
iex> Pnum.map([a: 1, b: 2], fn({k, v}) -> { k, -v } end)
[a: -1, b: -2]
  • process(item, func, parent)

@spec process(item, (item -> any), pid) :: any

Sends the result of invoking func with item to the parent PID in a {child_pid, result} tuple. Returns the resulting tuple.

Used internally to facilitate Pnum concurrent operations.

iex> Pnum.process(1, fn(x) -> x * 2 end, self)
{#PID<0.42.0>, 2}

Contributing

Contributions are accepted via Pull Request, but passing unit tests must be included before it will be considered for merge.

$ mix test

A basic Vagrant development VM is available at adlawson/vagrantfiles for easy setup.

$ curl -O https://raw.github.com/adlawson/vagrantfiles/master/elixir/Vagrantfile
$ vagrant up
$ vagrant ssh
...
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
$ cd /srv

License

The content of this library is released under the MIT License by Andrew Lawson.
You can find a copy of this license in LICENSE or at http://opensource.org/licenses/mit.