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 / run_length_encoder.ex
Last active August 1, 2016 20:09
Elixir Quiz #2: Run Length Encoding
# Given a string of uppercase characters in the range A-Z,
# replace runs of sequential characters with a single
# instance of that value preceded by the number of items
# in the run.
defmodule RunLengthEncoder do
def encode(string) do
encode_aux(String.codepoints(string), [])
end

ExWiKiDx - A Wikipedia Indexer in Elixir

This is meant to be an approximate port of Web Words, written in Elixir. I was appalled at the number of external dependencies needed (MongoDB, RabbitMQ), so I decided to see if I could come up with something similar.

Also, I wasn't smart enough to understand the Scala code – my brain melted and my eyes bled. My apologies to all Scala programmers out there.

How it works

The application takes in a Wikipedia site URL and performs crawling of various other Wikipedia pages. As it goes through the pages, it calculates the word frequencies and presents the results in "real-time" (whatever that means). Stop words are ignored (e.g. 'a', 'the', 'of' etc.).

@benjamintanweihao
benjamintanweihao / agent_example.ex
Created October 4, 2014 08:26
agent_example.ex
defmodule MyState do
def start_link do
Agent.start_link(fn -> :ets.new(named: MyState.ETS) end, local: MyState.Agent)
end
def nb_read(key) do
:ets.lookup_element(MyState.ETS, key)
end
def read(key) do
defmodule Mix.TasksServer do
@moduledoc false
use GenServer.Behaviour
def start_link() do
:gen_server.start_link({ :local, __MODULE__ }, __MODULE__, :ok, [])
end
def clear_tasks() do
call :clear_tasks
defmodule Wiky.ParserChannel do
use Phoenix.Channel
def join(socket, "start-parser", message) do
Wiky.Parser.start_link("/Users/benjamintan/Downloads/wiki.xml")
Enum.map 1..100, fn _ ->
reply socket, "join", Wiky.Parser.Agent.get_state
end
{:ok, socket}
end
defmodule Exile.Bot do
use GenServer
@nick "exile-bot"
@timeout 10_000
def start_link(host, port, chan, nick \\ @nick) do
GenServer.start_link(__MODULE__, %{host: host, port: port, chan: chan, nick: nick, sock: nil}, timeout: @timeout)
end
-module(ant).
-export([indicesList/1, matrix/1, printMatrix/1, randomMatrix/3,
wrappedPath/1, pathLength/2, updatePher/3, evaporatePher/3,
doSumWeight/7, findSumWeight/9,genPathRecurse/8,genPath/4,
bestPathRecurse/9,main/0,parallelWork/6,parallelMain/0]).
%% ant.erl
%% Eric Rollins 2006
%% updatePher and evaporatePher have suggested fixes from Robert Virding.
@benjamintanweihao
benjamintanweihao / working_with_dates.swift
Created November 4, 2014 01:57
Working with Dates, Calendars in Swift.
// Playground - noun: a place where people can play
import UIKit
// Setup the calendar object
let calendar = NSCalendar.currentCalendar()
// Set up date object
let date = NSDate()
@benjamintanweihao
benjamintanweihao / non_dist_to_dist.ex
Created November 10, 2014 10:32
Transforming from non-distributed ... to distributed.
# Non-distributed
defp do_requests(url, n_requests) do
coord_task = Task.async(Coordinator, :start, [n_requests])
Enum.map(1..n_requests, fn i ->
spawn(Worker, :start, [url, i])
end)
Task.await(coord_task, :infinity) |> parse_results
@benjamintanweihao
benjamintanweihao / step_generator.ex
Created January 13, 2015 08:42
Stream.resource/3 example
defmodule StepGenerator do
use GenServer
defmodule State do
defstruct step: nil, current: nil
end
def start_link(start_from, step) do
GenServer.start_link(__MODULE__, %State{step: step, current: start_from})
end