Skip to content

Instantly share code, notes, and snippets.

@andreburgaud
andreburgaud / recursion.erl
Created June 25, 2017 22:04
FutureLearn - Functional Programming in Erlang 1.21 - Tail Recursion
-module(recursion).
-export([test/0,fib/1,loop/1,sum/1,max/1,perfect/1]).
% Typical loop in Erlang
loop(N) when N>0 ->
io:format("~p~n", [N]),
loop(N-1);
loop(_) -> % implies N = 0
io:format("bye~n").
@andreburgaud
andreburgaud / fib.erl
Last active June 25, 2017 20:26
FutureLearn - Functional Programming in Erlang 1.19 - Recursion
-module(fib).
-export([fib/1]).
% Fibonacci direct recursion implementation
fib(0) ->
0;
fib(1) ->
1;
fib(N) when N>0 ->
fib(N-1) + fib(N-2).
@andreburgaud
andreburgaud / patterns.erl
Last active June 25, 2017 16:19
FutureLearn - Functional Programming in Erlang 1.15 - Erlang pattern matching examples with hand-crafted tests
-module(patterns).
-export([all_tests/0]).
% all_tests invokes all tests (xor, maxThree, howManyEquals)
all_tests() ->
all_xor_tests(),
test_max_three(),
test_how_many().
% all_xor_tests invokes all XOR tests.
@andreburgaud
andreburgaud / first.erl
Last active June 23, 2017 05:16
FutureLearn - Functional Programming in Erlang 1.9
-module(first).
-export([double/1,treble/1,square/1,mult/2,area/3]).
mult(X, Y) ->
X*Y.
double(X) ->
mult(2, X).
% Area of a triangle (Heron's formula)
@andreburgaud
andreburgaud / webcrypto.js
Created November 16, 2015 04:29
Basic example of Web Crypto (keygen/encrypt/decrypt) using symmetric encryption with AES Gallois Counter Mode algorithm.
// Resources:
// https://github.com/diafygi/webcrypto-examples
// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey
// http://blog.engelke.com/tag/webcrypto/
// Tested in Firefox Developer Edition
function strToArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
@andreburgaud
andreburgaud / list_comp_misc.py
Last active August 29, 2015 14:08
List comprehensions in Python. Translation from book "Programming Haskell" examples. Erik Meijer Functional Programming class on Edx.
pairs = lambda xs: zip(xs, xs[1:])
print pairs([1,2,3,4,5,6]) # [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
sorted = lambda xs: all([x<=y for (x, y) in pairs(xs)])
print sorted([1,2,3,4,5,6,7]) # True
print sorted([1,2,3,4,5,9,7]) # False
@andreburgaud
andreburgaud / cyber_savvy.py
Last active July 7, 2024 20:39
Infinite Campus We Are Cyber Savvy T-shirt
import sys
tshirt = [
0b01001001, 0b01101110, 0b01100110, 0b01101001,
0b01101110, 0b01101001, 0b01110100, 0b01100101,
0b00100000, 0b01000011, 0b01100001, 0b01101101,
0b01110000, 0b01110101, 0b01110011, 0b00100000,
0b01001001, 0b00100000,
0b01110111, 0b01110010,
0b01101001, 0b01110100,