Skip to content

Instantly share code, notes, and snippets.

View benjamintanweihao's full-sized avatar

Benjamin Tan Wei Hao benjamintanweihao

View GitHub Profile
@benjamintanweihao
benjamintanweihao / dist_sys_syllabus.md
Created August 9, 2016 09:39
Personal Distributed System Syllabus/Training Plan
  1. Logical Clocks/Time
  2. Leader Election
  3. Consensus / Agreement
  4. Failure Detector
  5. Causual Message Delivery
  6. Distributed Shared Memory
  7. Synchronizers
  8. Snapshot
  9. Mutual Exclusion
  10. Deadlock Detector
@benjamintanweihao
benjamintanweihao / bar.ex
Created July 15, 2016 08:05
Dynamically generated macros using comprehension
defmodule Bar do
defmacro __using__(__opts) do
quote do
import unquote(__MODULE__)
end
end
for fn_name <- [:foo, :bar, :baz] do
defmacro unquote(fn_name)(do: inner) do
fn_name = unquote(fn_name)
defmodule Html do
defmacro markup(do: inner) do
quote do
{:ok, var!(buffer, Html)} = start_buffer
unquote(inner)
result = get_buffer(var!(buffer, Html))
stop_buffer(var!(buffer, Html))
result
end
@benjamintanweihao
benjamintanweihao / html_2.ex
Last active July 14, 2016 08:24
Second attempt. This one works.
defmodule Html do
defmacro markup(do: inner) do
quote do
{:ok, var!(buffer, Html)} = start_buffer
unquote(inner)
result = get_buffer(var!(buffer, Html))
stop_buffer(var!(buffer, Html))
result
end
@benjamintanweihao
benjamintanweihao / html.ex
Last active July 14, 2016 08:20
This is my first attempt at reproducing Html. Not working though.
defmodule Html do
defmacro markup(do: inner) do
{:ok, buffer} = start_buffer
quote do
unquote(inner)
end
buffer = get_buffer
stop_buffer
defmodule MacroPlayground do
defmacro para(ast) do
# 0. Store the pid of the current process
me = self
pids = Macro.prewalk(ast, fn
# 1. Pattern matching can be done with function arguments!
{:for, meta, args} ->
# 2. Extract the do block of the comprehension
@benjamintanweihao
benjamintanweihao / control_flow.ex
Last active July 13, 2016 03:40
Learning me some metaprogramming
defmodule ControlFlow do
defmacro para(for_comp) do
# 0. Store the pid of the current process
me = self
pids = Macro.prewalk(for_comp, fn
# 1. Pattern matching can be done with function arguments!
{:for, meta, args} ->
# 2. Extract the do block of the for comprehension
@benjamintanweihao
benjamintanweihao / ozplatform.md
Last active July 11, 2016 10:04
Getting Mozart Oz 1.3.2 working on Mac OSX
  1. Open /Applications/Mozart.app/Contents/Resources/bin/ozplatform
  2. Make it look like the following:
#!/bin/sh
echo "i486-darwin"
(defun dotspacemacs/user-config ()
"Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
(spacemacs/set-leader-keys-for-major-mode 'oz-mode
"sb" 'oz-feed-buffer
"sl" 'oz-feed-line
package com.fpinscala.errorhandling
object Foo {
def sequence[A](a: List[Option[A]]): Option[List[A]] =
// We need to destructure only because of the `Nil`
// case where we have to return None immediately.
a match {
case Nil => Some(Nil) /* Option[List[A]] map (List[A] => A :: List[A]) */
case h :: t => h flatMap(hh => (sequence(t) map (x => hh :: x)))