iamafanasyev

iamafanasyev

Resource - Safe and composable way to handle "resources"

Elixir’s kernel has Stream.resource/3 function, that acts “resource-like” (it defines resource acquiring, usage and releasing phases). As a “resource-like” facility it also guarantees resource releasing once it was acquired (no matter what usage phase returns or even raises). However, it biased toward “stream-like” resources (as the name of the module suggests). So every time I want to safely handle a resource in my code, I found myself adopting Stream.resource/3 for “singleton-resource”.

E.g. in case of WithTimeout (also got a post about it) local task supervisor is spawned under the hood, so we need to ensure it’s properly released (no matter what happens in between). And adopting Stream.resource/3 for non-stream-like-resource could be quite cumbersome.

So I also decided to extract this repeatable pattern into separate library.

While doing so I’ve came across a noticeable use case — handling several resources. Exposing some kind of @spec use_all!(list(resource(a)), (list(a) -> b)) :: b when a: var, b: var felt unnatural. On the other hand, when you found yourself “flat-mapping” your data types (Resource.use!(r1, fn a -> Resource.use(r2, fn b -> ... end) end)), it is the monadic glimpse whispering you. So long story short, I’ve equipped the library with for-comprehension batteries to be able to compose resources (I exported general purpose for-comprehension from Bindable).

As a disclaimer: there is no reasonable filtering semantics for Resource (so there is no Bindable.Empty implementation for Resource, that is why if is not available for Resource inside for-comprehension).

At the end of the day, the library exposes composable facility for safe resource handling.

Resource (hex)
Resource (GitHub)

First Post!

iamafanasyev

iamafanasyev

Some great concerns on Stream.resource/3 from hissssst

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
mcrumm
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Th...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Sub Categories:

We're in Beta

About us Mission Statement