Skip to content

Instantly share code, notes, and snippets.

View apbarrero's full-sized avatar

Antonio Pérez Barrero apbarrero

View GitHub Profile
@apbarrero
apbarrero / lists1.erl
Created June 27, 2017 20:53
Defining functions over lists in practice
-module(lists1).
-export([product/1, maximum/1]).
product(Xs) -> product(Xs, 1).
product([], P) -> P;
product([X|Xs], P) -> product(Xs, X*P).
maximum([X|Xs]) -> maximum(Xs, X).
maximum([X|[]], M) -> max(X, M);
maximum([X|Xs], M) -> maximum(Xs, max(X, M)).
@apbarrero
apbarrero / tailrecurs.erl
Last active June 24, 2017 13:52
Tail recursion exercise
-module(tailrecurs).
-export([fib/1, perfect/1, testPerfect/0]).
fib(0) -> 0;
fib(1) -> 1;
fib(N) when N>1 ->
fib(N, 1, 0).
fib(2, Acc1, Acc2) -> Acc1 + Acc2;
fib(N, Acc1, Acc2) when N>2 -> fib(N-1, Acc1 + Acc2, Acc1).
-module(recurs).
-export([fib/1, pieces/1]).
fib(1) ->
[0];
fib(2) ->
[1,0];
fib(N) when N>2 ->
L = fib(N-1),
[X|Xs] = L,
@apbarrero
apbarrero / first.erl
Created June 21, 2017 20:17
First erlang program
-module(first).
-export([double/1,mult/2,area/3,sq/1,treble/1]).
mult(X,Y) ->
X*Y.
double(X) ->
mult(2,X).
area(A,B,C) ->
@apbarrero
apbarrero / flatten.rb
Created March 13, 2016 21:45
Array flatten
#!/usr/bin/env ruby
require 'test/unit'
def flatten(array)
f = []
if array.kind_of?(Array)
array.each{ |e| f.concat(e.kind_of?(Array) ? flatten(e) : [e]) }
else
f = [array]
@apbarrero
apbarrero / gist:2682f5d1db60ef204f49
Created June 22, 2014 07:24
git ahead (manual)
git log <current branch>..origin/<current branch>
git config --global alias.ahead '!git log HEAD..origin/$(git symbolic-ref HEAD | sed "s:refs/heads/::g")'
@apbarrero
apbarrero / process_state_codes.md
Created August 26, 2013 07:06
Meaning of process state codes (C column) on `ps` output

PROCESS STATE CODES

Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process.

CODE Meaning
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
@apbarrero
apbarrero / ps_headers.md
Last active May 30, 2022 02:42
Legend for headers dumped by `ps -f` command
HEADER Meaning
UID effective user ID. (alias uid).
PID process ID number of the process.
PPID parent process ID.
C processor utilization. Currently, this is the integer value of the percent usage over the lifetime of the process. (see %cpu).
START/STIME starting time or date of the process. Only the year will be displayed if the process was not started the same year ps was invoked, or "mmmdd" if it was not started the same day, or "HH:MM" otherwise.
TTY controlling tty (terminal). (alias tt, tty).
STAT multi-character process state. See section PROCESS STATE CODES for the different values meaning. See also s and state if you just want the first character displayed.
TIME cumulative CPU time, "[dd-]hh:mm:ss" format. (alias cputime).
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
function red() {
echo -e "$RED$*$NORMAL"
}
function green() {