Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active December 18, 2015 09:09
Show Gist options
  • Save ToJans/5758964 to your computer and use it in GitHub Desktop.
Save ToJans/5758964 to your computer and use it in GitHub Desktop.
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}"
end
end
aPost = EasyPeasy.Post.new id: 1, title: "Muahaha", publishedOn: :calendar.local_time, formatter: EasyPeasy.formatter
aPost = aPost.body("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eget sagittis eros.")
aPost |> aPost.formatter
aPost = aPost.formatter (fn post -> title)
aPost |> aPost.formatter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment