2. About Me
• Frontend, rust and node.js enthusiast
• Desktop app developer with electron
• 12th ITHelp ironman competition
- software development category (champion)
25. Question Mark Syntax Sugar
#[derive(Debug)]
struct Obj {
a: Option<B>
}
#[derive(Debug)]
struct B {
b: Option<i32>
}
let obj = Obj { a: None };
let getPropB = |o: &Obj| o.a.as_ref()?.b;
println!("obj.a.b: {:?}", getPropB(&obj)); // obj.a.b: None
26. Map option value
let f = |x: i32| x * 2;
let x = Some(34);
let y = match x {
Some(v) => Some(f(v)),
None => None,
};
println!("y: {:?}", y);
let x = Some(34);
let x = x.map(|v| v + 2);
println!("y: {:?}", y);
// y: 68
// y: 68
34
Some
68
Some
let f = |x: i32| x * 2;
map
27. Map with another option
// some_z: Some(Some(37))
let some_x = Some(34);
let some_y = Some(3);
let some_z = some_x.map(|x| some_y.map(|y| x + y));
println!("some_z: {:?}", some_z);
34
Some
map
let f = |x| some_y.map(|y| x + y); Some(37)
Some
28. AndThen with another option
// some_z: Some(37)
let some_x = Some(34);
let some_y = Some(3);
let some_z = some_x.and_then(|x| some_y.map(|y| x + y));
println!("some_z: {:?}", some_z);
34
Some
flat_map/bind
let f = |x| some_y.map(|y| x + y);
37
Some
30. let f = |x: i32| x * 2;
let x = Some(34);
let y = match x {
Some(v) => Some(f(v)),
None => None,
};
Some (34)
None
Some(68)
None
map(|x| x * 2)
Think as Railway
31. Think as Railway
let f = |x: i32| x * 2;
let x = None;
let y = match x {
Some(v) => Some(f(v)),
None => None,
};
Some (34)
None
Some(68)
None
map(|x| x * 2)
32. Think as Railway
Some (i32::MAX)
None
Some(i32::MAX)
None
and_then(|x: i32| x.checked_mul(2))
let f = |x: i32| x.checked_mul(2);
let x = Some(i32::MAX);
let y = x.and_then(f);
println!("y: {:?}", y);
33. Std library with option
https://doc.rust-lang.org/std/primitive.u32.html#method.checked_add
34. Working with two options
let some_x = Some(1);
let some_y = Some(2);
let xy = some_x.and_then(|x| {
some_y.map(|y| x * y)
});
xy: Some(2)
• and_then
let some_x = Some(1);
let some_y = Some(2);
let xy = some_x
.zip(y)
.map(|(x, y)| x * y);
xy: Some(2)
• zip
35. Working with multiple options
let some_x = Some(1);
let some_y = Some(2);
let some_z = Some(3);
let xyz = some_x.and_then(|x| {
some_y.and_then(|y| {
some_z.map(|z| x + y + z)
})
});
xyz: Some(6)
• Bad practice:
36. (TS) Applicative’s solution
import { Just } from "pratica";
let just_x = Just(1);
let just_y = Just(2);
let just_z = Just(3);
let unary_fn = x => y => z => x + y + z;
let just_added = Just(unary_fn)
.ap(just_x)
.ap(just_y)
.ap(just_z)
.inspect();
console.log(just_added); // Just(6)
37. (RS) Haskell do notation alternative
fn work_with_multiple_option(x: i32, y: i32, z: i32) -> Option<i32> {
let added_x = x.checked_mul(2)?; // 2
let added_y = y.checked_mul(3)?; // 6
let added_z = z.checked_mul(added_x)?; // 6
Some(added_x + added_y + added_z) // 14
}
40. let f = |x: i32| x * 2;
let x = Ok(34);
let y = match x {
Ok(v) => Ok(f(v)),
Err(e) => Err(e),
};
Ok (34)
Err(e)
Ok(68)
Err(e)
map(|x| x * 2)
Think as Railway - map
41. let f = |x: i32| x * 2;
let x = Err("x not found");
let y = match x {
Ok(v) => Ok(f(v)),
Err(e) => Err(e),
};
Ok (34)
Err(e)
Ok(68)
Err(e)
map(|x| x * 2)
Think as Railway - map
42. Fast fail validator with result
fn rule1(input: i32) -> Result<i32, &'static str> {
if i > 4 {
Err("should bigger than four")
} else {
Ok(input)
}
}
fn rule2(input: i32) -> Result<i32, &'static str> {
if i > 5 && i <100 {
Err("should bigger than five and less than one hundred")
} else {
Ok(input)
}
}
51. (TS) Ramda - sequence
(Applicative f, Traversable t) => (a ! f a) ! t (f a) ! f (t a)
R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
52. (TS) Ramda - traverse
Traverse :: (Applicative f, Traversable t) => (a ! f a) ! (a ! f b) ! t a ! f (t b)
Maybe.Just([5, 2.5, 2])
const parseInt = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d);
R.traverse(Maybe.of, parseInt(10), [2, 4, 5]);
67. • Enjoy built-in option and result functionality
With rust, you can…
• Enjoy rust’s zero cost abstraction
• Improve your service’s performance and
memory usage