vic

vic

Asdf Core Team

Spec: like clojure.spec but for Elixir

Hello,

I’ve just pushed Spec, the first beta version of an clojure.spec inspired library for conforming data against an specification.

Currenly Spec supports conforming against most Elixir data types, supports regex operators, and some other niceties you can read of in the README file (a bit long read). But there still work to do on it, like supporting data example generators, and maybe other things.

<3, vic.

Most Liked

OvermindDL1

OvermindDL1

/me is glad he grabbed it before github went down…

This looks quite nice, could clean up my type testing that I have scattered around. I am curious though, why does something like:

conform!(is_tuple() and &(tuple_size(&1) == 2), {1})

Take the value as the second argument instead of the first? It would be convenient to do things like:

value
|> conform!(is_tuple() and &(tuple_size(&1) == 2))
|> etc...

Also, on this example from your README.md:

iex> conform!(hello :: is_binary(), "world")
{:hello, "world"}

Have you thought about mandating this syntax instead:

iex> conform!(:hello :: is_binary(), "world")
{:hello, "world"}

That way we could tag it with anything, even a variable like:

iex> blah = :vwoop
iex> conform!(blah :: is_binary(), "world")
{:vwoop, "world"}
Qqwy

Qqwy

TypeCheck Core Team

I will definitely use this when writing more elaborate behaviours/interfaces. It looks like a really clean way to structure conformity checks. :smile: Thank you for making this, @vic!

vic

vic

Asdf Core Team

Hey @OvermindDL1, Thanks a lot for your feedback.

Well, while conform/2 expects the predicates (or any defined spec) inside of it to take the value as first argument (values are just piped into predicates), conform/2 itself expects its as second, I guess just because in Elixir functions use the first argument for the subject value, eg Regex.match(regex, string), so I guess I just went for Spec.conform(spec, value) naturally.

But if you do defspec and give it a name, then the resulting function will expect to work on the first value, applying the spec to it.

defp sum_tuple({a, b}, c) when a + b == c, do: true
defp sum_tuple({a, b}, c), do: false

defspec two_tuple(is_tuple() and &(tuple_size(&1) == 2))

value = {1, 2}

value
|> two_tuple()
|> sum_tuple(3)

So I guess having Spec.conform/2 take the spec as first argument (again just like many other things on Elixir) is better.

Regarding tags, you are irght, it could be useful to tag with a bound variable. Will change it and see how it feels.

Thanks for reading and giving input! :slight_smile:

OvermindDL1

OvermindDL1

It looks quite useful indeed, I like it. :slight_smile:

vic

vic

Asdf Core Team

Hello !

Today I made some progress on the README guide, it’d would be wonderful if any of you could give a quick look at it and provide some feedback, thanks in advance! :slight_smile:

Where Next?

Popular in Libraries Top

RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 11851 134
New
Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
cjen07
parameterized pipe in elixir: |n&gt; edit: negative index in |n&gt; and mixed usage with |&gt; are supported example: use ParamP...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
michalmuskala
Another small library today. PersistentEts Hex: persistent_ets | Hex GitHub: GitHub - michalmuskala/persistent_ets Ets table backed by...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Sub Categories:

We're in Beta

About us Mission Statement