Skip to content

Instantly share code, notes, and snippets.

@iain17
iain17 / module.erl
Last active April 4, 2017 22:08
Erlang file example
% Defines the basic module
-module(basic).
% Expose the example/1 function through the module
-export([example/1]).
% Defines the actual mirror function, which returns the first argument
example(Anything) -> Anything.
@iain17
iain17 / count.erl
Created April 5, 2017 11:29
Write a function that uses recursion to count to ten.
-module(count).
-export([to_ten/0]).
%Assignment: Write a function that uses recursion to count to ten.
%Entrypoint
to_ten() -> count(0).
% Base case
count(10) -> 10;
%keep on adding 1 untill you reach the above function
count(Value) -> count(Value + 1).
@iain17
iain17 / print_err_success.erl
Created April 5, 2017 11:38
Write a function that uses matching to selectively print “success” or “error: message” given input of the form {error, Message} or success.
-module(print_err_success).
-export([print/1]).
%Assignment: Write a function that uses matching to selectively print “success” or “error: message” given input of the form {error, Message} or success.
%Simple return Success String.
print(success) -> "Success";
%Error message. The '++' concatenates the two strings.
print({error, Message}) -> "Error: " ++ Message.
@iain17
iain17 / key_lookup.erl
Created April 6, 2017 19:08
Assignment: Consider a list of keyword-value tuples, such as [{erlang, "a functional language"}, {ruby, "an OO language"}]. Write a function that accepts the list and a keyword and returns the associated value for the keyword.
- module(key_lookup).
- export([search/2]).
%Assignment: Consider a list of keyword-value tuples, such as [{erlang, "a functional language"}, {ruby, "an OO language"}]. Write a function that accepts the list and a keyword and returns the associated value for the keyword.
search(Key, List) -> do_search(Key, List).
%With the use of pattern matching we are able to have the do_search1 return once we
%hit the result.
do_search(Key, [{Key, Value}|_]) ->
io:fwrite("do_search1 Keyword(~p) Value(~p)\n", [Key, Value]),
@iain17
iain17 / Bill.erl
Last active April 6, 2017 19:21
Assignment: Consider a shopping list that looks like [{item quantity price}, ...]. Write a list comprehension that builds a list of items of the form [{item total_price}, ...], where total_price is quantity times price.
[{"Beer", 2.50, 8}, {"Snacks", 1.99, 4}]

Usage

c(translate_service).
Service = spawn(fun translate_service:watch_loop/0).
Service ! new.
translate_service:translate(translator, "casa").
translate_service:translate(translator, "blanca").
translate_service:translate(translator, "incorrect").
translate_service:translate(translator, "casa").

Usage

c(translate_service).
c(doctor_translate).

Doctor = spawn(fun doctor_translate:watch/0).
Doctor ! new.

translate_service:translate(translator, "casa").
translate_service:translate(translator, "blanca").
@iain17
iain17 / problem1.erl
Last active April 8, 2017 11:55
Project euler erlang problems.
-module(problem1).
-export([solve/0]).
%Assignment: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
%Find the sum of all the multiples of 3 or 5 below 1000.
solve() ->
solve(1, 0).
solve(Number, Sum) when Number == 1000 ->
Sum;
@iain17
iain17 / k8s.sh
Last active June 29, 2017 19:22
Rancher little snippets
#SSH into the machine
ssh -i "aws-rancher.pem" rancher@ec2-00-158-000-000.eu-central-1.compute.amazonaws.com
#To switch to a Docker version supported by k8s.
#http://rancher.com/docs/rancher/v1.6/en/hosts/#supported-docker-versions
sudo ros engine switch docker-1.12.6
#Install Rancher
#http://rancher.com/docs/rancher/v1.6/en/installing-rancher/installing-server/
sudo docker run -d -v /home/rancher/mysql:/var/lib/mysql --restart=unless-stopped -p 8080:8080 rancher/server
@iain17
iain17 / instructions.md
Last active July 6, 2017 08:30
Terminal use proxy (Socks proxy to HTTP proxy)
  1. Setup your socks proxy. In my case I'm creating a socks proxy by creating a secure shell connection (SSH): ssh iain@8.8.4.4 -D 9050

  2. (Optional for mac users): You can also simplify this action by using a lovely (free) application called Secure Pipes: https://www.opoet.com/pyro/

  3. Download delegate and move the executable (dg9_9_13) into /usr/local/sbin or /usr/bin. Example:

cp dg9_9_13/DGROOT/bin/dg9_9_13 /usr/local/sbin/delegate
chmod +x /usr/local/sbin/delegate