runexec

runexec

I was toying around to see if I could possibly get something similar to Clojure-style pre/post enforcement. This is what I came up with and would like your input. Thanks.


Example Usage

  defmodule Example do
    require Contract

    def reverse_abc(x) do
      # Contract pre processing
      pre_must_be_abc = fn -> x == "abc" end
      pre = [
        pre_must_be_abc,
      ]

      # Contract post processing
      post_must_be_cba = fn x -> x == "cba" end
      post_must_be_string = fn x -> is_binary x end
      post = [
        post_must_be_cba,
        post_must_be_string,
      ]

      # Contract
      # The arguments here are `[x]` and do not represent the current value of `x`
      reverse_abc_contract = Contract.new([x], [pre: pre, post: post], String.reverse(x))

      # Encorce Contract
      reverse_abc_contract.(x)
    end
  end

Basic Implementation

    defmodule Contract do

    defmodule EnforcePreError do
      defexception message: "Argument(s) Failed Pre Processing Requrements."
    end

    defmacro enforce(:pre, list, expr) do
      quote do
        valid? =
          Enum.map(unquote(list), fn f -> f.() end)
          |> Enum.all?(&(&1 == true))

        case valid? do
          true -> unquote(expr)
          _ -> raise EnforcePreError
        end
      end
    end

    defmodule EnforcePostError do
      defexception message: "Return value Failed Post Processing Requrements."
    end

    defmacro enforce(:post, list, value) do
      quote do
        valid? =
          Enum.map(unquote(list), fn f -> f.(unquote(value)) end)
          |> Enum.all?(&(&1 == true))

        case valid? do
          true -> unquote(value)
          _ -> raise EnforcePostError
        end
      end
    end

    defmodule EnforceNothingError do
      defexception message: "No enforcements are defined."
    end

    defmacro enforce(:prepost, pre, post, expr) do
      quote do
        pre = unquote(pre) || []
        post = unquote(post) || []
        no_enforcements? = (pre == [] && post == [])
        get_value = fn ->
          unquote(__MODULE__).enforce(:pre, unquote(pre), unquote(expr))
        end

        cond do
          no_enforcements? -> raise EnforceNothingError
          pre && post -> unquote(__MODULE__).enforce(:post, unquote(post), get_value.())
          (pre == []) && post -> unquote(__MODULE__).enforce(:post, unquote(post), unquote(expr))
          (post == []) && pre -> get_value.()
        end
      end
    end

    defmacro new(args, opts, expr) do
      quote do
        fn (unquote_splicing(args)) ->
          unquote(__MODULE__).enforce(
            :prepost, unquote(opts[:pre]), unquote(opts[:post]), unquote(expr)
          )
        end
      end
    end
  end

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews