<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Tianyi as a Developer</title>
    <link>/</link>
    <description>Recent content on Tianyi as a Developer</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 05 May 2016 21:48:51 -0700</lastBuildDate>
    
	<atom:link href="/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Javascript Async Examples</title>
      <link>/js/javascript-async/</link>
      <pubDate>Tue, 07 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/js/javascript-async/</guid>
      <description>Error propagatioon It is not necessary to
async function foo() { let rand = ~~(Math.random() * 10); if (rand % 2) { return &amp;quot;Hello&amp;quot;; } else throw Error(&amp;quot;Error at foo!&amp;quot;); } async function bar() { let x = await foo(); x += &amp;quot; world&amp;quot;; return x; } async function baz() { let y = await bar(); y += &amp;quot;!!!&amp;quot;; return y; } let a = 0; (async () =&amp;gt; { for (let i = 0; i &amp;lt; 10; i++) { try { res = await baz(); console.</description>
    </item>
    
    <item>
      <title>Working with Cookies in Selenium, Axios, Fetch and Requests</title>
      <link>/web/working-with-cookies-in-selenium-js-and-python/</link>
      <pubDate>Tue, 07 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/web/working-with-cookies-in-selenium-js-and-python/</guid>
      <description>Must navigate to the page before cookies can be used.
Adding cookies before navigating to the domain are called domain-less cookies which i believe is not possible.
https://stackoverflow.com/questions/45842709/unable-to-set-cookies-in-selenium-webdriver
async example() { let driver = await new Builder().forBrowser(&amp;quot;chrome&amp;quot;).build(); try { cookies = JSON.fs.readFileSync(&amp;#39;cookies&amp;#39;) await driver.get(&amp;quot;https://link.springer.com/referenceworkentry/10.1007/978-3-319-77093-2_14-1&amp;quot;); for (cookie of cookies) { await driver.manage().addCookie(cookie); } driver.navigate().refresh(); await driver.wait(() =&amp;gt; false, 10000000); } finally { await driver.quit(); } }; Axios and fetch https://codewithhugo.com/pass-cookies-axios-fetch-requests/#pass-cookies-with-requests-in-axios</description>
    </item>
    
    <item>
      <title>Parsing Escaped Quotes in Serde</title>
      <link>/2020/04/05/parsing-escaped-quotes-in-serde/</link>
      <pubDate>Sun, 05 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/2020/04/05/parsing-escaped-quotes-in-serde/</guid>
      <description>library(stringr) use serde::Deserialize; use std::borrow::Cow; #[derive(Deserialize, Debug)] struct Comment&amp;lt;&amp;#39;a&amp;gt; { id: u32, text: Cow&amp;lt;&amp;#39;a, str&amp;gt;, } fn main() { let data: Vec&amp;lt;Comment&amp;gt; = serde_json::from_str( r#&amp;quot; [ { &amp;quot;id&amp;quot;: 345213, &amp;quot;text&amp;quot;: &amp;quot;Hello world!&amp;quot; }, { &amp;quot;id&amp;quot;: 2412345, &amp;quot;text&amp;quot;: &amp;quot;This is a double quote: \&amp;quot;&amp;quot; } ] &amp;quot;#, ) .unwrap(); println!( &amp;quot;{}&amp;quot;, data[1].text //.iter().map(|entry| entry.id).collect::&amp;lt;Vec&amp;lt;_&amp;gt;&amp;gt;() ); } The problem is that this deserilise into a borrowed string, then the lifetime of that borrowed string is tied to the input of the JSON string, and moreover, presumably must correspond to a substring of the original JSON.</description>
    </item>
    
    <item>
      <title>Constructors of Rust structs</title>
      <link>/rust/constructors-of-rust-structs/</link>
      <pubDate>Sat, 04 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/constructors-of-rust-structs/</guid>
      <description>The new() Method The new() method is nothing special but it’s idiomatically used to initialise a new instance of a struct with empty fields, for example:
#[derive(Debug)] struct Car&amp;lt;&amp;#39;a&amp;gt; { brand: &amp;amp;&amp;#39;a str, model: &amp;amp;&amp;#39;a str, color: &amp;amp;&amp;#39;a str, } impl&amp;lt;&amp;#39;a&amp;gt; Car&amp;lt;&amp;#39;a&amp;gt; { fn new() -&amp;gt; Self { Car { brand: &amp;quot;&amp;quot;, model: &amp;quot;&amp;quot;, color: &amp;quot;&amp;quot;, } } } fn main(){ println!(&amp;quot;{:?}&amp;quot;, Car::new()) } ## Car { brand: &amp;quot;&amp;quot;, model: &amp;quot;&amp;quot;, color: &amp;quot;&amp;quot; }  Builder Methods For structs with many fields (some of which may be optional), it’s common to define a number of builder methods to construct them.</description>
    </item>
    
    <item>
      <title>Closures in Rust</title>
      <link>/rust/closures-in-rust/</link>
      <pubDate>Thu, 02 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/closures-in-rust/</guid>
      <description>Using Closures and Functions as Arguments fn apply&amp;lt;F&amp;gt;(f: F) where F: Fn() { f(); } fn apply_mut&amp;lt;F&amp;gt;(mut f: F) where F: FnMut() { f(); } fn apply_once&amp;lt;F&amp;gt;(f: F) where F: FnOnce() { f(); } fn main() { use std::mem; let mut greeting = &amp;quot;hello&amp;quot;.to_owned(); let farewell = &amp;quot;goodbye&amp;quot;; // Capture 2 variables: `greeting` by value and // `farewell` by reference. let diary = || { println!(&amp;quot;Fn: {}, {}&amp;quot;, greeting, farewell); }; apply(diary); apply_mut(diary); apply_once(diary); // dyn Fn actually implements all Fn, FnMut and FnOnce // try &amp;amp;T, then &amp;amp;mut T, then T let diary_mut = || { greeting.</description>
    </item>
    
    <item>
      <title>OOP in Rust</title>
      <link>/rust/oop-in-rust/</link>
      <pubDate>Wed, 01 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/oop-in-rust/</guid>
      <description>Example: Post Using Enum enum PostState { Draft, PendingReview, Published, } struct Post { text: String, state: PostState, } impl Post { fn new() -&amp;gt; Self { Post { text: String::new(), state: PostState::Draft, } } fn add_text(&amp;amp;mut self, s: &amp;amp;str) { self.text.push_str(s) } fn request_review(&amp;amp;mut self) { self.state = PostState::PendingReview } fn approve(&amp;amp;mut self) { self.state = PostState::Published } fn content(&amp;amp;self) -&amp;gt; &amp;amp;str { if let PostState::Published = self.state { &amp;amp;self.</description>
    </item>
    
    <item>
      <title>Polymorphism in Rust Traits</title>
      <link>/rust/polymorphism-in-rust-traits/</link>
      <pubDate>Wed, 01 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/polymorphism-in-rust-traits/</guid>
      <description>The From and Into traits are good examples of polymorphism.
If a trait uses a generic type T in its signature, such as in From:
pub trait From&amp;lt;T&amp;gt;: Sized { fn from(_: T) -&amp;gt; Self; } then different ‘variants’ of the trait, where &amp;lt;T&amp;gt; is replaced with concrete types, can be implemented. Here is an example:
struct Foo(i32); #[derive(Debug)] struct Bar(i32); impl From&amp;lt;i32&amp;gt; for Bar { fn from(n: i32) -&amp;gt; Self { Bar(n) } } impl&amp;lt;&amp;#39;a&amp;gt; From&amp;lt;&amp;amp;&amp;#39;a Foo&amp;gt; for Bar { fn from(foo: &amp;amp;Foo) -&amp;gt; Self { Bar(foo.</description>
    </item>
    
    <item>
      <title>Borrowing and Ownership in Rust</title>
      <link>/rust/borrowing-and-ownership-in-rust/</link>
      <pubDate>Tue, 31 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/borrowing-and-ownership-in-rust/</guid>
      <description>Slice Slices
fn main() { let mut x = [1, 2, 3, 4, 5]; let r = &amp;amp;mut x; if let Some((first, rest)) = r.split_first_mut() { *first = 100; rest[0] = 99; rest[1] = 98; } assert_eq!(x, [100, 99, 98, 4, 5]) }   Deref Coersion in Functions fn main() { let v = 5; let r = &amp;amp;v; let r2 = &amp;amp;r; println!(&amp;quot;{}, {}&amp;quot;, is_five(r), is_five(r2)) } fn is_five(val: &amp;amp;i32) -&amp;gt; bool { *val == 5 } ## true, true  Smart Pointers Deref Coersion From The Book:</description>
    </item>
    
    <item>
      <title>PostSQL Administrative Commands</title>
      <link>/sql/postsql-administrative-commands/</link>
      <pubDate>Tue, 31 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/sql/postsql-administrative-commands/</guid>
      <description> User Management Changing Password Edit pg_hba.conf to allow trust authorization temporarily pg_ctl reload to reload the config file Connect and issue ALTER ROLE postgres WITH PASSWORD &#39;newpassword&#39;; Edit pg_hba.conf again and restore the previous settings; pg_ctl reload again    </description>
    </item>
    
    <item>
      <title>Default Parameters &amp; Optional Arguments in Rust</title>
      <link>/rust/default-parameters-optional-arguments-in-rust/</link>
      <pubDate>Tue, 24 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/default-parameters-optional-arguments-in-rust/</guid>
      <description>In Rust’s std docs, there is an example of using unwrap_or() to mimic a default parameter:
use std::fmt; struct Vector2D { x: isize, y: isize, } impl fmt::Binary for Vector2D { fn fmt(&amp;amp;self, f: &amp;amp;mut fmt::Formatter) -&amp;gt; fmt::Result { let magnitude = (self.x * self.x + self.y * self.y) as f64; let magnitude = magnitude.sqrt(); let decimals = f.precision().unwrap_or(3); let string = format!(&amp;quot;{:.*}&amp;quot;, decimals, magnitude); f.pad_integral(true, &amp;quot;&amp;quot;, &amp;amp;string) } } fn main() { let myvector = Vector2D { x: 3, y: 4 }; println!</description>
    </item>
    
    <item>
      <title>Creating Multidimensional Arrays in Javascript</title>
      <link>/js/js-ndarray/</link>
      <pubDate>Mon, 23 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/js/js-ndarray/</guid>
      <description>Initialising multidimensional arrays in Javascript can be tricky because some initialisation approches lead to unexpected behaviour due to shallow copying.
2D Arrays (Matrices) If we try to create a 4 \(\times\) 4 matrix by filling an empty array with 4 zero-filled arrays each with 4 zeros using the Array(n).fill(e) syntax:
let arr = Array(4).fill(Array(4).fill(0)); console.table(arr); ┌─────────┬───┬───┬───┬───┐ │ (index) │ 0 │ 1 │ 2 │ 3 │ ├─────────┼───┼───┼───┼───┤ │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 1 │ 0 │ 0 │ 0 │ 0 │ │ 2 │ 0 │ 0 │ 0 │ 0 │ │ 3 │ 0 │ 0 │ 0 │ 0 │ └─────────┴───┴───┴───┴───┘ and then try to change the element \(arr_{1,2}\):</description>
    </item>
    
    <item>
      <title>Extending knitr</title>
      <link>/r/extending-knitr/</link>
      <pubDate>Mon, 23 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/r/extending-knitr/</guid>
      <description>Many R users use R Markdown to author documents, often containing R code, that can be seamlessly rendered into various output formats (usually PDF and HTML) with a single click or keyboard shortcut. Ordered R markdown documents can be converted into books and websites (for example the one you’re now reading!) with a few lines of command, thanks to Yihui’s bookdown and blogdown packages.</description>
    </item>
    
    <item>
      <title>R Housekeeping</title>
      <link>/r/r-housekeeping/</link>
      <pubDate>Mon, 23 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/r/r-housekeeping/</guid>
      <description>This post is to remind myself of some not-so-common, but very useful operations in R.
.Rprofile and .Renviron Options  Setting PATH in .Renviron RStudio has a different PATH from bash’s, which can lead command not found when calling system() or system2(). You can set the PATH for RStudio in .Renviron file like this:
PATH=/additional/path:${PATH} Note that curly braces are required.
  Package Management Installing Local Packages: First, open the R Project of the package in RStudio.</description>
    </item>
    
    <item>
      <title>Bash String Manipulation</title>
      <link>/linux/bash-string-manipulation/</link>
      <pubDate>Sun, 22 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/linux/bash-string-manipulation/</guid>
      <description>Bash String Variable Manipulation Substring by Index  Syntax: ${var:start:length} index starts from 0 (unlike awk’s substr(), which starts from 1)  chars=0123456789ABCDE echo ${chars:3} # chars[3] to end echo ${chars:1:0} # length=0 -&amp;gt; empty string echo ${chars:3:5} # chars[3:8] ## 3456789ABCDE ## ## 34567  Stripping  # and ##: stripping the shortest/longest match from start % and %%: stripping the shortest/longest match from end  path=foo/bar/hello.txt echo ${path#*/} echo ${path##*/} # get filename echo ${path%/*} # get dir echo ${path%%/*} ## bar/hello.</description>
    </item>
    
    <item>
      <title>PostgreSQL Querying</title>
      <link>/sql/postgresql-querying/</link>
      <pubDate>Sun, 22 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/sql/postgresql-querying/</guid>
      <description> The library(DBI) con &amp;lt;- DBI::dbConnect(RPostgres::Postgres(), user = &amp;#39;postgres&amp;#39;, password = getOption(&amp;#39;db.password.postgres&amp;#39;), host = &amp;#39;localhost&amp;#39;, port = &amp;#39;5432&amp;#39;) select now();  Table 1: 1 records  now    2020-03-23 00:16:36     1 + 1 ## 2  </description>
    </item>
    
    <item>
      <title>Rust Iterators</title>
      <link>/rust/rust-iterators/</link>
      <pubDate>Thu, 19 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/rust/rust-iterators/</guid>
      <description>Methods on Iterators next()  Iterator Adaptors Iterator adaptors takes ownership of the iterator and returns another kind of iterator, which allows chaining. The commonly used adaptors are map(), filter(),
The chain of iterators is lazy
 Aggregation Functions fn main() { use Direction::*; println!( &amp;quot;{:?}&amp;quot;, dir_reduc(&amp;amp;[NORTH, SOUTH, EAST, WEST, NORTH, WEST]) ); } #[derive(PartialEq, Eq, Debug, Clone)] enum Direction { NORTH, SOUTH, EAST, WEST, } impl Direction { fn opposite(&amp;amp;self) -&amp;gt; Direction { match self { Direction::NORTH =&amp;gt; Direction::SOUTH, Direction::SOUTH =&amp;gt; Direction::NORTH, Direction::EAST =&amp;gt; Direction::WEST, Direction::WEST =&amp;gt; Direction::EAST, } } } fn dir_reduc(arr: &amp;amp;[Direction]) -&amp;gt; Vec&amp;lt;Direction&amp;gt; { let mut res: Vec&amp;lt;Direction&amp;gt; = vec!</description>
    </item>
    
    <item>
      <title>Working with Date and Time</title>
      <link>/sql/working-with-date-and-time/</link>
      <pubDate>Thu, 19 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>/sql/working-with-date-and-time/</guid>
      <description>In R, the package lubridate handles date and times.
Basic Functions Current Date/Time Current time:
SELECT NOW();  Table 1: 1 records  now    2020-03-23 00:13:29     Current date:
today() ## [1] &amp;quot;2020-03-23&amp;quot; as_date(now()) ## [1] &amp;quot;2020-03-23&amp;quot; SELECT current_time;  Table 2: 1 records  current_time    00:13:29.623558     SELECT NOW()::DATE;  Table 3: 1 records  now    2020-03-23       Addition and Subtraction In R, durations are always stored in seconds.</description>
    </item>
    
    <item>
      <title>About</title>
      <link>/about/</link>
      <pubDate>Thu, 05 May 2016 21:48:51 -0700</pubDate>
      
      <guid>/about/</guid>
      <description>This is a &amp;ldquo;hello world&amp;rdquo; example website for the blogdown package. The theme was forked from @jrutheiser/hugo-lithium-theme and modified by Yihui Xie.</description>
    </item>
    
  </channel>
</rss>