Skip to content

Instantly share code, notes, and snippets.

# lib/witchcraft/monoid.ex
properties do
def left_identity(data) do
a = generate(data)
# a == TypeClass.Property.Generator.Function.generate(data)
# but that input will get ignored.
if is_function(a) do
equal?(Semigroup.append(Monoid.empty(a), a).("foo"), a.("foo"))
defmodule TypeClass.Property do
@moduledoc "A *very* simple prop checker"
# (...)
@doc "Run all properties for the type class"
def run!(datatype, class, prop_name, times \\ 100) do
property_module = Module.concat(class, Property)
# Irrelevant parts in this scenario are commented out.
defmodule TypeClass do
# (...)
defmacro definst(class, opts, do: body) do
[for: datatype] = opts
quote do
# The type class protocol's instance implementation goes into
# `type_class.Proto.datatype` (in the case of `Monoid`, this will
import TypeClass
################
# DEPENDENCIES #
################
extend Witchcraft.Semigroup, alias: true
############
# PROTOCOL #
############
defmodule Aquir.Users.Read.Schemas.User do
use Ecto.Schema
alias Aquir.Users.Read.Schemas, as: RS
@primary_key {:user_id, :binary_id, autogenerate: false}
schema "users" do
has_one :credential, RS.UsernamePasswordCredential,
foreign_key: :user_id
@toraritte
toraritte / elixir-derive-mod-attr.md
Last active May 3, 2020 01:22
Composing the answer to "What is the specific syntax of @derive module attribute and where is it documented explicitly?" Stackoverflow question

What is the specific syntax of @derive module attribute and where is it documented explicitly?

NOTE: All links to the Elixir github repo and documentation are fixed to version 1.7.4.

"Elixir allows us to derive a protocol implementation based on the Any implementation"2 using @derive. The [Kernel.@/1][2] macro compiles to a call to [Module.get_attribute/2][3] function3.

Progress

Got bogged down looking at Kernel.@/1 definition: what is does the curly brace do in the definition? (i.e., defmacro @{..})

/* Understanding iOS views*/
/* In ViewController.swift after creating a Single View App. */
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
@toraritte
toraritte / elixir_agent.example.ex
Last active May 3, 2020 01:27
Mixing `Agent.cast/2` and `Agent.update/3`
# Mixing `Agent.cast/2` and `Agent.update/3`
# Ignoring the state here, hence `:ok`
Agent.start_link(fn -> :ok end, name: :lofa)
f = fn (command, t) ->
apply(Agent, command, [
:lofa,
fn _ -> # becuase state is ignored
Process.sleep(t)
defmodule UniqueUsername do
use Agent
def start_link(_) do
Agent.start_link(fn -> MapSet.new() end, name: __MODULE__)
end
def claim(name) do
name_is_taken = Agent.get(__MODULE__, &MapSet.member?(&1, name))