Skip to content

Instantly share code, notes, and snippets.

@ToJans
ToJans / bom.js
Created June 4, 2013 15:28
exploring how I would design a simple MEF with angularjs
function BOM($scope) {
$scope.Materials = [
{ "id": "MAT/TOMATOSOUP",
"name": "Tomato soup",
"unit": "L",
"Precision": 0.01
}, {
"id": "MAT/ONION",
"name": "Onion",
"unit": "PCS",
@ToJans
ToJans / Posts.ex
Last active December 18, 2015 04:58
defmodule Posts do
defrecord BlogPost, id: nil, title: nil, publishedOn: nil
defmodule FakeData do
def allPosts do
[
BlogPost.new(id: 1,title: "Blog post one" ,publishedOn: "20130201"),
BlogPost.new(id: 2,title: "Blog post two" ,publishedOn: "20130202"),
BlogPost.new(id: 3,title: "Blog post three",publishedOn: "20130312"),
@ToJans
ToJans / model.ex
Last active December 18, 2015 09:09
My take on immutable models and builders
defmodule EasyPeasy
defrecord Post,
id: nil,
title: nil,
body: nil,
publishedOn: nil,
formatter: nil
def formatter(post) do
"#{post.id} - #{post.title}\n published on #{post.publishedOn}\n\n#{post.body}"
@ToJans
ToJans / _usage.exs
Last active December 18, 2015 09:49
A simple blogging routine in Elixir
store=Posts.MemStore.start
store |> Posts.createPost("Another one bites the dust")
newPost = store |> Posts.Query.items |> Posts.OrderBy.recentlyCreated |> Enum.first
# now it works!!!
store |> Posts.publishPost(newPost.id)
recentPosts = store |> Posts.Query.items |> Posts.Filter.published |> Posts.OrderBy.recentlyPublished
@ToJans
ToJans / makefile
Last active December 18, 2015 14:28
Make file test
REBAR := $(shell echo `pwd`/rebar)
ELIXIRC := bin/elixirc.bat --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
VERSION := $(strip $(shell cat VERSION))
INSTALL_PATH := /usr/local
.PHONY: install compile erlang elixir dialyze test clean docs release_docs release_zip release_erl
.NOTPARALLEL: compile
@ToJans
ToJans / elixir.sh
Last active December 18, 2015 14:39
bin/elixir
#!/bin/sh
if [ $# -eq 0 ] || [ $1 = "--help" ] || [ $1 = "-h" ]; then
echo "Usage: `basename $0` [options] [.exs file] [data]
-v Prints version
-e \"command\" Evaluates the given command (*)
-r \"file\" Requires the given files/patterns (*)
-S \"script\"   Finds and executes the given script (*)
-pr \"file\" Requires the given files/patterns in parallel (*)
-pa \"path\" Prepends the given path to Erlang code path (*)
@ToJans
ToJans / for.exs
Created July 26, 2013 10:44
See how easy it is to extend Elixir: implement a "for x in y" in 3 simple lines.
defmacro for({ :in, _, [left, right] }, do: block) do
quote do
Enum.map unquote(right), fn unquote(left) -> unquote(block) end
end
end
@ToJans
ToJans / beer.exs
Created July 26, 2013 23:36
My version of the beer koan
defmodule Beer do
@moduledoc """
# Beer Song
A program which produces the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
Note that Not all verses are identical.
The verse for 1 bottle is as follows:
@ToJans
ToJans / Word.exs
Created July 27, 2013 01:12
Word count koan
defmodule Words do
@doc """
Given a phrase can count the occurrences of each word in that phrase.
## Example
Words.count("olly olly in come free")
#=> #HashDict<[{"come",1},{"free",1},{"in",1},{"olly",2}]>
"""
@ToJans
ToJans / Anagram.exs
Created July 27, 2013 01:14
Anagram koan
defmodule Anagram do
@doc """
# Anagram
Given a word and a list of possible anagrams, selects the correct one(s).
## Example
iex> Anagram.match "listen",%w(enlists google inlets banana)
["inlets"]