Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chouzar committed Nov 14, 2024
1 parent 95d00cb commit 3fb98f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ A gleam library for operating and querying ETS tables.

```gleam
import gleam/list
import lamb.{Set, Private}
import lamb/table.{Config, Private, Set}
import lamb/record
import lamb/query.{var, ignore, atom}
import lamb.{Config, Set, Private}
import lamb/query as q
type User {
User(name: String, age: Int, bio: String)
Expand All @@ -20,27 +18,25 @@ pub fn main() {
// Create a table and insert 5 records.
let assert Ok(table) =
Config(name: "users", access: Private, kind: Set, registered: False)
|> table.create()
|> lamb.create()
lamb.insert(table, "00", User("Raúl", age: 35, bio: "While at friends gatherings, plays yugioh."))
lamb.insert(table, "01", User("César", age: 33, bio: "While outdoors, likes bird watching."))
lamb.insert(table, "02", User("Carlos", age: 30, bio: "Always craving for coffee."))
lamb.insert(table, "10", User("Adrián", age: 26, bio: "Simply exists."))
// This query syntax builds a matchspec to make queries on the table.
let query =
q.new()
|> q.bind(#(var(0), var(1)))
// Retrieve all User records.
let _records = lamb.all(table, q.new())
// Retrieve all rows but only return the record
let query_records = query |> q.map(var(1))
let _records = lamb.all(table, query_records)
// Retrieve all User ids.
let query =
query
|> q.bind3(User)
|> q.map(fn(index, _name, _age, _bio) { index })
// Retrieve all rows but only return the index
let query_indexes = query |> q.map(var(0))
let _indexes = lamb.all(table, query_indexes)
let _ids = lamb.all(table, query)
// Retrieve all records in batches of 2.
// Retrieve all User records in batches of 2.
let assert Records([_, _] as a, step) = lamb.partial(table, by: 2, where: q.new())
let assert Records([_, _] as b, step) = lamb.continue(step)
let assert End([User(_, _, _, _)] as c) = lamb.continue(step)
Expand All @@ -58,9 +54,9 @@ gleam test # Run the tests

---

Work in progress notes.
# Work in progress notes.

# Table API
## Table API

Currently viewing the table creating in terms of protection levels:
- `Private`, private table, not named.
Expand All @@ -79,7 +75,7 @@ pub fn options() -> Options {

Still need to figure out what is going to be the API for differentiating between `set` and `bag` tables.

# Query API
## Query API

Matchspecs are composed by a Tuple of arity 3 called a `MatchFunction`:

Expand All @@ -93,7 +89,7 @@ Matchspecs are composed by a Tuple of arity 3 called a `MatchFunction`:

Operating on these 3 pieces may be tackled in different ways.

## Alternative 1
### Alternative 1

Have an API that composes matchspecs together with a builder pattern:

Expand All @@ -114,7 +110,7 @@ query |> q.select(fn(last, first) { // Alternative mapper to above.
})
```

## Alternative 2
### Alternative 2

Build a "nice" parser to compose complex queries:

Expand All @@ -129,7 +125,7 @@ let query = "
"
```

## Alternative 3
### Alternative 3

Just build a more straightforward mapper for matchspecs.

Expand All @@ -141,7 +137,7 @@ let query_driver_elegibility = #(head, [condition], [body])
lamb.search(table, [query_driver_elegibility])
```

## Notes on types and validation
### Notes on types and validation

None of these alternatives currently provide a good way of enforcing types but are meant to fail gracefully if
there are errors. Querying so far is a "dynamic" operation.
Expand All @@ -152,7 +148,7 @@ be quite a big lift for my current purposes.
Maybe another way is to provide a validator at init time, but this would exclusively check the validity of the
matchspecs.

# Checkout these ETS APIs
## Checkout these ETS APIs

* https://www.erlang.org/doc/apps/stdlib/ets.html#select_delete/2
* https://www.erlang.org/doc/apps/stdlib/ets.html#fun2ms/1
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "lamb"
version = "0.4.0"
version = "0.4.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down

0 comments on commit 3fb98f1

Please sign in to comment.