Skip to content

Instantly share code, notes, and snippets.

View asonge's full-sized avatar

Alexander Songe asonge

View GitHub Profile
defmodule HTMLCharacters do
def sanitize(phrase) do
chars = %{"&quot;" => "\"", "&#39;" => "'", "&amp;" => "&", "&lt;" => "<", "&gt;" => ">"}
cond do
String.match?(phrase, ~r/&\#?.+;/) ->
[code] = Regex.run(~r/&\#?.+;/, phrase)
Regex.replace(Regex.compile!(code), phrase, chars[code])
true -> phrase
@asonge
asonge / behaviour.ex
Last active August 29, 2015 14:25 — forked from anonymous/behaviour.ex
defmodule Datastore do
use Behaviour
@type key :: any
@type value :: any
defcallback start_link() :: Agent.t
defcallback get(Agent.t, key) :: value
defcallback put(Agent.t, key, value) :: :ok
-module(chat_server).
-export([start/1]).
handle_socket(Router, Client) ->
case gen_tcp:recv(Client, 0) of
{ok, "quit!} ->
gen_tcp:close(Client);
{ok, Data} ->
Router ! {msg, Client, Data}
@asonge
asonge / resolveLines
Created May 22, 2016 01:25 — forked from cutecycle/resolveLines
Array.find crash at length = 1447
var look = function(lineid) {
var find;
find = (lines_json.find( function(value) {
return (value.lineid === lineid);
}));
if(find.speakerid !== "" || (typeof find.speakerid === "undefined")) {
find.retrievedSpeaker = line(entity(find.speakerid).entityname);
}
lines_out.push(find);
if(find.advance_lineid !== "" || (typeof find.advance_lineid === "undefined")) {
// See http://brunch.io for documentation.
exports.files = {
javascripts: {
entryPoints: {
'game.js': 'brunchtest.js'
}
},
};
exports.paths = {
@asonge
asonge / test.ex
Created July 30, 2017 23:48 — forked from pikeas/test.ex
defmodule Test.Sup do
use Supervisor
def start_link(opts), do: Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
def init([]) do
children = [
# Supervisor.Spec.worker(Test.Server, [[]]),
Test.Server
]
str = SSHEx.stream connection, 'ping -c 5 www.google.com'
Enum.reduce(str, {:ok, :running}, fn
_, {:error, reason} -> {:error, reason
{:stdout,row}, {:ok, :running} ->
IO.puts row
{:ok, :running}
{:stderr,row}, {:ok, :running} ->
IO.puts row
{:ok, :running}
def add_attr(event_attr, person_attrs) do
new_attr_value = [%Person.AttributeValue{
:timestamp => event_attr.timestamp,
:confidence => event_attr.confidence,
:value => event_attr.value
}]
curr_attr = Map.get(person_attrs, event_attr.field)
if (curr_attr === nil) do #the person doesn't have that attr
# add_new(person_attrs, )
new_attr_source = %{