Skip to content

Instantly share code, notes, and snippets.

@iain17
iain17 / background.md
Last active September 19, 2022 09:43
Moving / Alive background

Add the following to your crontab (crontab -e): * * * * * /Users/iain17/background.sh

  • Create a file called background.sh (preferably in your home directory)
  • Set the right location of the background.sh file in your crontab.
  • Run the following command on the file, making it executable: chmod +x /Users/iain17/background.sh
  • Setup any fancy screenshot. I prefer this one: JohnCoates's Aerial screensaver
  • Now every minute the background.sh shell script should check if the screensaver process is running. If it isn't it'll start it in the background, using your desktop wallpaper as output.

Enjoy!

@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 / words.erl
Last active September 19, 2022 10:06
Write a function that uses recursion to return the number of words in a string.
-module(words).
-export([strlen/1]).
%Assignment: Write a function that uses recursion to return the number of words in a string.
% If we send an empty list, return 0.
strlen([]) -> 0;
%Entry point for this module
strlen(Sentence) -> count(Sentence, 1).
%Base case. When its finally empty return the sum count.
count([], Count) -> Count;
@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;