20 Topics in 20 Minutes

bootlessbwakInternet και Εφαρμογές Web

12 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

68 εμφανίσεις

20 Topics in 20 Minutes

Eric Wastl

@topaz2078

Software Architect

Synacor

BarCamp Buffalo 2013

(Synacor)

(Buffalo)

(Canada)

PHP

CSS3

Perl

Java

JavaScript

Linux

Apache

MySQL

Git

HTML5

Ruby

Lua

Bash

ActiveMQ

Kafka

Hadoop

Cassandra

VMWare

jQuery

Graphviz

digraph G {


a
-
> b


b
-
> c


b
-
> d


c
-
> e


c
-
> f

}

Graphviz

digraph G {


concentrate = true;


labeljust = r;


subgraph cluster_internet {


label = "internet"


client_a; client_b; client_c


}


subgraph cluster_internal {


label = "internal"


node [shape=box]


loadbalancer
-
> server01


loadbalancer
-
> server02


loadbalancer
-
> svr_spare [style=dashed]


database [shape=box3d]


server01
-
> database


server02
-
> database


svr_spare
-
> database [style=dashed]


}


client_a
-
> loadbalancer


client_b
-
> loadbalancer


client_c
-
> loadbalancer

}

Graphviz

Graphviz

Graphviz

Perl: userland syntax sugar

sub forever {


my $code = shift;


while (1) {


$code
-
>();


}

}


forever(sub {


print "round and round...
\
n";

});

Perl: userland syntax sugar

sub forever

(&)

{


my $code = shift;


while (1) {


$code
-
>();


}

}


forever
{


print "round and round...
\
n
";

}
;

Perl: userland syntax sugar

sub
if2
(&@) {


while (@_) {


my $cond = shift;


my $code = shift;


if ($cond
-
>()) {


$code
-
>();


last;


}


}

}

sub
then2
(&@) { @_ }

sub
elsif2
(&@) { @_ }

sub
else2
(&) { sub{1}, $_[0] }

Perl: userland syntax sugar

my $
x

= 3;


if2
{ $
x

== 1 }
then2
{


print "
x

was 1
\
n";

}
elsif2
{ $
x

== 2 }
then2
{


print "
x

was 2
\
n";

}
else2
{


print "
x

was something else
\
n
";

}

Polar coordinates

y

x

r

t

x

=
r

* cos(
t
)

y

=
r

* sin(
t
)

t

= atan2(
y
,
x
)

r

= sqrt(
x
*
x

+
y
*
y
)

Operators: Arity

To how many things does the operator apply?


! x <
-

1: unary

a + b <
-

2: binary

c ? t : f <
-

3: ternary


Operators: Precedence

Given different operators, which goes first?



1 + 2 * 3



1 + (2 * 3) <
-

* is higher

(1 + 2) * 3 <
-

+ is higher

Operators: Associativity

Given operators of the same precedence, in
what order do they apply?



a


b


c



a


(b


c) <
-

right
-
associative

(a


b)


c <
-

left
-
associative

Operators: Fixity

To which operands does the operator apply?


! a <
-

prefix (unary)

a ++ <
-

postifx (unary)

a + b <
-

infix (binary)

a b + <
-

postfix
(binary!)

+ a b <
-

prefix
(binary!)

Array mapping in PHP/JS/Perl

$input = array(1, 2, 3, 4, 5);


function square($v) { return $v*$v; }

$output = array_map("square", $input);


// $output is array(1, 4, 9, 16, 25)

Array mapping in PHP/JS/Perl

input = [1,2,3,4,5];


output =


input.map(function(v){ return v*v; });


// output is [1, 4, 9, 16, 25]


Array mapping in PHP/JS/Perl

@input = (1, 2, 3, 4, 5);


@output = map { $_*$_ } @input;


# @output is (1, 4, 9, 16, 25)

Array mapping in PHP/JS/Perl

function square($v) { return
$v*$v
; }

$output = array_map
("square",
$input
);


output =


input.map
(function(v){ return

v*v
; });


@output = map
{

$_*$_
}

@input;




PHP: Comparison Operators


$a < $b $a > $b

PHP: Comparison Operators


$a < $b $a > $b

PHP: Comparison Operators

$ cat circular.php

<?php


$a = INF;

$b = array();

$c = (object)array();


var_dump($a < $b);

var_dump($b < $c);

var_dump($c < $a);



$ php circular.php

bool(true)

bool(true)

bool(true)

$array = array(


INF,


array(),


(object)array(),

);


sort($array);


is_sorted($array); //false

Git Basics

$ git init

$ git clone git://...


$ git add stuff.c thing.php
pants.pl

$ git commit

am “new stuff”


$ git push

$ git pull

CAP Theorem

A distributed system cannot simultaneously be
consistent, available, and partition
-
tolerant.


consistent


only returns correct answer

available


always returns an answer

partition
-
tolerant


works during network failure

Node 1

x = 0

Node 2

x = 0

x = 1

x = 1

x = 1











CAP Theorem

A distributed system cannot simultaneously be
consistent, available, and partition
-
tolerant.


consistent


only returns correct answer

available


always returns an answer

partition
-
tolerant


works during network failure

Node 1

x = 0

Node 2

x = 0

x?

x = 1

?

?

?





Perl Inline::* Modules

use Inline Java => <<'END';

class Java_Class {


public Java_Class() {}



public int sum(int a, int b) {


return a + b;


}

}

END


my $java_class = new Java_Class();

print "Java: ".$java_class
-
>sum(19, 23) . "
\
n";


Perl Inline::* Modules

use Inline Python => <<'END';

def python_sum(a, b):


return int(a + b)

END


print "Python: " . python_sum(19, 23) . "
\
n";

Perl Inline::* Modules

use Inline Lua => <<'END';

function lua_sum(a, b)


return a + b

end

END


print "Lua: " . lua_sum(19, 23) . "
\
n";

Perl Inline::* Modules

use Inline CPP => <<'END';

class Cpp_Class {


public:


Cpp_Class() {} //nothing to construct


~Cpp_Class() {} //nothing to destruct


int sum(int a, int b) { return a + b; }

};

END


my $cpp_class = new Cpp_Class();

print "CPP: " . $cpp_class
-
>sum(19, 23) . "
\
n";

Perl Inline::* Modules

use Inline C => <<'END';

int c_sum(int a, int b) {


return a + b;

}

END


print "C: " . c_sum(19, 23) . "
\
n";

Perl Inline::* Modules

use Inline Basic => <<'END';

10 DEF FNBASICSUM(A,B) = A + B

END


print "BASIC: " . FNBASICSUM(19, 23) . "
\
n";

Perl Inline::* Modules

use Inline ASM => <<'END', AS => 'nasm',
ASFLAGS => '
-
f elf', PROTO => {asm_sum =>
'int(int,int)'};


GLOBAL asm_sum


SECTION .text

asm_sum: mov eax,[esp+4]


sum eax,[esp+8]


ret

END


print "ASM: " . asm_sum(0, 23) . "
\
n";

Perl Inline::* Modules

print "Sum: " .


$java_class
-
>sum(


asm_sum(


python_sum(4, 7),


$cpp_class
-
>sum(6, 3)


),


c_sum(


FNBASICSUM(5, 8),


lua_sum(0, 9)


)


)


. "
\
n";

Vanilla JS Speed Advantages

Vanilla JS Speed Advantages

Screen Basics


Keep running console programs if you log out
or get disconnected!


Run several things at once from one
connection!


(A billion other reasons I don't have time to
talk about!)

Screen Basics

Start a new session

$ screen


List sessions

$ screen

ls


Reconnect to a session

$ screen
-
dr

Screen Basics

^A (ctrl
-
A) talks to screen


^A c New window

^A ^A Switch to previous window

^A 1 Switch to specific window

^A " List windows

^A A Rename window

^A d Detach from screen

^A a Send literal ^A

Fractals in your Terminal!




+
-

.
-



-
.
-
+
-
.+
-
. .
---



++=... .
-
+
-
.
-

-
==+.
--

-
..


.... .....+=
--
...+.
--
.....+++
-
. +&+


+=
--
+
--
.. +.
-
.+
-
..
---
+=+..+
-
+....
-
+
---
.


+++
-
++ .
-
..
--
+*.
-
.
-
=.
-
.
---
.*+ . .


.
--

--
+.
-
.
--

+&..*
-
+=*#+.+


. .....+ .. .
--
+
--

-

+
-
.
--
++.


--
*&=+.
-

.
---
.
-

+*
-
.
-
.
-
.=


----
+.
--
.
-
.
-

. .+.+
-



... . .....=
-
+*+
-

-
.
---
..
-
.
---
++=*+..


-

.
-
.
--
.. .
--

+
-
.. ...=. ==+
---
+=
-

.+

++. ......
-
....
-
=+.. .
-
.
-
. +.
-
.=..

-

.
--
+=+*
-
...
-
.. .+==. =+
-
..= .
-



..
-
+*=++
-
.=. .
-
.
-
+
-

.+
-
*
-
.
-

--
..+..
-
+=*=.


..
-
.
-
+
--
.
-
..
-
+**
-
+
-

&=
--
.
-
..
-
.
-
++.=
---

.+


.. . .. .
-
.
---

. .
-
==
-

++
-
=+
-
. .

. *...
--
.
-

-
.
-

. .
--


++ +.
---
...
-


.+
-
+....
--
&.+
-


-

.
-
.
--

.
-
.

----
..
-
..
-

.

https://github.com/topaz/perl
-
mandelbrot

$ ./mandelbrot.pl
-
x
-
.7435669
-
y .1314023
-
w .003
-
q
--
lines=24
--
cols=80
-
v
-
d 600

Perl Symbol Table Hackery

package Greeter;

sub greet {


print "Hello, $_[0]!
\
n";

}


# elsewhere, deep in some call
stack

Greeter::greet("Fred
");


Hello, Fred!

Perl Symbol Table Hackery

my $old_greet =
\
&Greeter::greet;

*Greeter::greet = sub {


print "$_[0]? Nope!
\
n";


return $old_greet
-
>("pants");

};


# same elsewhere as before

Fred? Nope!

Hello, pants!

Perl Symbol Table Hackery

BEGIN {


*CORE::GLOBAL::time = sub {


return 42;


};

}


print "It is
".localtime(time)."
\
n";


It is Wed Dec 31 19:00:42 1969


Easy Bash Tips


You can search your history for a command by
typing ctrl
-
R and any substring.



You can iterate over lists:

$ for f in dir/*.log; do grep
-
i error $f | wc
-
l; done



You can expand multiple similar words:

$ cat {access,error}.log
-
> $ cat access.log error.log

$ mv pants{,.ext}
-
> $ mv pants pants.ext


PHP: Adding () can change meaning

function &r(&$v){return $v;}


$b = array("c");

array_pop(
(

r($b)
)

);
print_r($b);

array_pop( r($b) );
print_r($b);


Array( [0] => c )

Array( )



Color
-
grapheme Synesthesia

A B C D E F G H I

J K L M N O P Q R

S T U V W X Y Z 1

2 3 4 5 6 7 8 9 0

! @ # $ % ^ & * <

Color
-
grapheme Synesthesia

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

1

2

3

4

5

6

7

8

9

0

!

@

#

$

%

^

&

*
<

Color
-
grapheme Synesthesia

Color
-
grapheme Synesthesia

function square($v) { return $v * $v; }

$output = array_map("square", $input);


output =


input.map(function(v){ return v*v; });


@output = map { $_*$_ } @input;

Color
-
grapheme Synesthesia

function

square
(
$v
)
{

return

$v

*

$v
;
}

$output

=

array_map
("
square
",
$input
);


output

=


input.map
(
function
(
v
)
{

return

v*v
;
}
);


@output

=

map

{

$_*$_

}

@input
;

Eric Wastl


@topaz2078

ewastl@synacor.com


phpsadness.com

vanilla
-
js.com