Add RDF.Description.objects/2 with custom filter function

This commit is contained in:
Marcel Otto 2017-07-22 00:18:22 +02:00
parent 44b527038d
commit df766972c2
2 changed files with 9 additions and 2 deletions

View file

@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- Turtle decoder
- `RDF.Data.descriptions/1` returns all descriptions within a RDF data structure
- `RDF.Description.first/2` returns a single object a predicate of a `RDF.Description`
- `RDF.Description.objects/2` with custom filter function
### Changed

View file

@ -475,11 +475,17 @@ defmodule RDF.Description do
...> ]) |> RDF.Description.objects
MapSet.new([RDF.uri(EX.O1), RDF.uri(EX.O2), RDF.bnode(:bnode)])
"""
def objects(%RDF.Description{predications: predications}) do
def objects(%RDF.Description{} = description),
do: objects(description, &RDF.resource?/1)
@doc """
The set of all resources used in the objects within a `RDF.Description` satisfying the given filter criterion.
"""
def objects(%RDF.Description{predications: predications}, filter_fn) do
Enum.reduce predications, MapSet.new, fn ({_, objects}, acc) ->
objects
|> Map.keys
|> Enum.filter(&RDF.resource?/1)
|> Enum.filter(filter_fn)
|> MapSet.new
|> MapSet.union(acc)
end