Python Brasil - 2016 - #pybr12
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
De acordo com a Wikipedia:
"In programming languages, a type system is a collection of rules that assign a property called type to various constructs a computer program consists of"
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
Dados
Operações
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
"Strong"
"Weak"
"Static"
"Dynamic"
*As definições não são exatas na literatura
"Strong"
"Weak"
"Static"
"Dynamic"
"Untyped"
Python: strong (os tipos não são alterados pelo interpretador) e dynamic (O interpretador rastreia os dados)
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
fn main() {
fizzbuzz_to(100);
}
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
if rhs == 0 {
return false;
}
lhs % rhs == 0
}
fn fizzbuzz(n: u32) -> () {
if is_divisible_by(n, 15) {
println!("fizzbuzz");
} else if is_divisible_by(n, 3) {
println!("fizz");
} else if is_divisible_by(n, 5) {
println!("buzz");
} else {
println!("{}", n);
}
}
fn fizzbuzz_to(n: u32) {
for n in 1..n + 1 {
fizzbuzz(n);
}
}
source: http://rustbyexample.com/fn.html
Inferência de tipos limitada. Declaração de tipos explícita para parâmetros e tipo de retorno de uma função.
Macros
fn main() {
let _immutable_binding = 1;
let mut mutable_binding = 1;
println!("Before mutation: {}", mutable_binding);
// Ok
mutable_binding += 1;
println!("After mutation: {}", mutable_binding);
// Error!
_immutable_binding += 1;
// FIXME ^ Comment out this line
}
source: http://rustbyexample.com/variable_bindings/mut.html
Imutabilidade por padrão
fn is_odd(n: u32) -> bool {
n % 2 == 1
}
fn main() {
println!("Find the sum of all the squared odd numbers under 1000");
let upper = 1000;
let mut acc = 0;
for n in 0.. {
let n_squared = n * n;
if n_squared >= upper {
break;
} else if is_odd(n_squared) {
acc += n_squared;
}
}
println!("imperative style: {}", acc);
let sum_of_squared_odd_numbers: u32 =
(0..).map(|n| n * n) // All natural numbers squared
.take_while(|&n| n < upper) // Below upper limit
.filter(|n| is_odd(*n)) // That are odd
.fold(0, |sum, i| sum + i); // Sum them
println!("functional style: {}", sum_of_squared_odd_numbers);
}
source: http://rustbyexample.com/fn/hof.html
High
Order
Functions
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
fn foo() {
let v = vec![1, 2, 3];
let v2 = v;
println!("v[0] is: {}", v[0]);
}
#pybr12 @hannelita
fn foo() {
let v = vec![1, 2, 3];
let v2 = v;
println!("v[0] is: {}", v[0]);
}
O compilador de Rust reclama: "error: use of moved value: `v` println!("v[0] is: {}", v[0]);"
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
fn main() {
fn sum_vec(v: &Vec<i32>) -> i32 {
return v.iter().fold(0, |a, &b| a + b);
}
fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
let s1 = sum_vec(v1);
let s2 = sum_vec(v2);
s1 + s2
}
let v1 = vec![1, 2, 3];
let v2 = vec![4, 5, 6];
let answer = foo(&v1, &v2);
println!("{}", answer);
}
&
#pybr12 @hannelita
(More info: https://users.cs.duke.edu/~chase/cps210-archive/slides/moresync6.pdf )
Rust usa um modelo similar em tempo de compilação.
Rust usa um modelo similar em tempo de compilação.
T: Base type; possui um valor
&T: Reader compartilhado
&mut T: Writer exclusivo
(Note: Não estou considerando a feature de Copy nesses slides!)
Rust usa um modelo similar em tempo de compilação.
T: Base type; possui um valor
&T: Reader compartilhado
&mut T: Writer exclusivo
(Note: Não estou considerando a feature de Copy nesses slides!)
Referência imutável
Referência mutável
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
fn lifetimes() {
let a1 = vec![1, 2, 3]; // +
let a2 = vec![4, 5, 6]; // + |
// | |
let b1 = &a1; // + | |
let b2 = &a2; // + | | |
foo(b1); // | | | |
foo(b2); // 'b2 'b1 'a2 'a1
// | | | |
}
#pybr12 @hannelita
fn explicit_lifetime<'a>(x: &'a i32) {
}
fn multiple_lifetimes<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {
}
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
http://huonw.github.io/blog/2015/05/finding-closure-in-rust/
#pybr12 @hannelita
#pybr12 @hannelita
fn main() {
let u: &[u8] = &[49, 50, 51];
unsafe {
assert!(u == std::mem::transmute::<&str, &[u8]>("123"));
}
}
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
#pybr12 @hannelita
https://www.ralfj.de/blog/2015/10/12/formalizing-rust.html
#pybr12 @hannelita
O tópico sobre sistema de tipos é interessante! Quais são os próximos passos para se aprofundar?
Resposta: Lambda Calculus e Type Theory :)
#pybr12 @hannelita
http://slides.com/hannelitavante-hannelita/type-theory-101-35#/
Free GIF!
(Alguns desses links não são muito científicos)
Rust e Pokémons - http://slides.com/hannelitavante-hannelita/rust-and-pokmons#/
Perguntas?
hannelita@gmail.com
@hannelita