-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.json
1 lines (1 loc) · 11.4 KB
/
index.json
1
[{"content":"Imagine a Rock - Paper - Scissor game, it has a player versue a Host. When the game starts, the Host will wait for the player forming his/her hand shape. And while waiting, let the Host continuously forms his hand (to make it not boring).\nMake an infinite loop The pseudocode might look like this:\nwhile (playerThinking) randomly form a hand shape show it to player In Android, we have a main looper that also handles UI. If we want another infinite loop, we must not create the loop in main thread, as it\u0026rsquo;ll block the UI, which then leads to ANR (Application Not Responding).\nInstead, we should create the infinite loop in another thread by using AsynTask, Handler#post() or Kotlin\u0026rsquo;s coroutine\u0026hellip;\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 private fun gameLoop() { viewModelScope.launch { while (true) { delay(DELAYED_PLAY) // #1 when (gameState) { GameState.PLAYER_THINKING -\u0026gt; { randomHostPlay() // #2 } ... } } } } #1: Add some millisecond delay to let CPU relaxed. #2: The function contains logic for forming the Host\u0026rsquo;s hand. Make a blocked invocation When the player and the Host have formed their hands, it\u0026rsquo;s time to decide who wins the round.\nAssume the computation for that decision is very long, this computation should provide two things:\n It should not block the UI thread. It should notify the result to the Host. One approach for solving this problem is to use a callback: The computation is brought out of main thread. And after its computation, it invokes a callback, fall back to the Host.\nfun decideWinOrLose(callback) do a long heavy computation get result, invoke callback.onResult(result) ... //usage callback = { onResult(result) = { announce this result } } decideWinOrLose(callback) This is fine when you only have a computation. But what if you have a chains of computation, each depends on the previous one? The callback creation might become messy.\nIn that case, let\u0026rsquo;s convert callback to a blocked invocation:\n1 2 3 4 5 6 7 8 9 10 11 suspend fun waitGameResult(): GameScore = suspendCancellableCoroutine { cont -\u0026gt; callback = object : IOnGameResult { // #1 override fun onGameResult(result: GameScore) { callback = null cont.resume(result) // #2 gameState = GameState.OBSERVE_RESULT } } cont.invokeOnCancellation { callback = null } gameState = GameState.DECIDE_WIN_LOSE } 1 2 3 4 5 6 7 8 9 10 11 //producer private fun decideWinOrLose() { // long computation callback.onGameResult(result) // #3 } //consumer viewModelScope.launch { val result = waitGameResult() // #4 sendToHost(result) } At #1: A callback object is created and managed by a producer. At #4, when the function waitGameResult exits, the calling thread is blocked/suspended. When the long computation finishes at #3, the callback is invoked. Then at #2, the cont object invokes its resume() function, which will unblock the calling thread at #4, the result is returned from resume() function, and is sent to the host. ","description":"Let's play with Kotlin's coroutine by making an infinite loop logic and a blocked invocation logic.","id":0,"section":"programming","tags":["kotlin","android"],"title":"Play with Coroutine in Kotlin \u0026 Android","uri":"https://favoritekk.github.io/programming/kotlin-android-coroutines/"},{"content":"Problem description If your project was using standard C++11 and you wanted to copy an open source file somewhere which contains the use of std::make_unique function, you might get compile error\n error: ‘make_unique’ is not a member of ‘std’\n Because std::make_unique was added in C++14, not available in C++11.\nSolution Add a wrapper template in a common header file in your project:\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 namespace details { // make_unique support for pre c++14 #if __cplusplus \u0026gt;= 201402L // C++14 and beyond using std::make_unique; #else template\u0026lt;typename T, typename... Args\u0026gt; std::unique_ptr\u0026lt;T\u0026gt; make_unique(Args \u0026amp;\u0026amp;... args) { static_assert(!std::is_array\u0026lt;T\u0026gt;::value, \u0026#34;arrays not supported\u0026#34;); return std::unique_ptr\u0026lt;T\u0026gt;(new T(std::forward\u0026lt;Args\u0026gt;(args)...)); } #endif } // namespace details Then, convert the origin usage of std::make_unique to details::make_unique. For instance:\n1 2 3 4 Boundary::Ptr Boundary::create() { return std::make_unique\u0026lt;BoundaryImpl\u0026gt;(\u0026#34;bundle\u0026#34;, 0, true); } References Stackoverflow ","description":"How to add std::make_unique from C++14 into your C++11 code base.","id":1,"section":"programming","tags":["cplusplus","cpp"],"title":"Implement std::make_unique in C++11","uri":"https://favoritekk.github.io/programming/std_make_unique_in_cpp_11/"},{"content":"Alternative function syntax The arrow (-\u0026gt;) in function heading in C++ is just another form of function syntax in C++11.\nThis is standard function declaration:\n1 return-type function_name(argument-list) { body-statement } As C++ grew more complex, it exposed several limits. For example, in C++03 this is not allowed:\n1 2 template\u0026lt;class Lhs, class Rhs\u0026gt; Ret adding_func(const Lhs \u0026amp;lhs, const Rhs \u0026amp;rhs) {return lhs + rhs;} //Ret must be the type of lhs+rhs The compiler would not know what Ret is. And this is still not possible with the new introduced decltype keyword:\n1 2 template\u0026lt;class Lhs, class Rhs\u0026gt; decltype(lhs+rhs) adding_func(const Lhs \u0026amp;lhs, const Rhs \u0026amp;rhs) {return lhs + rhs;} //Not valid C++11 It\u0026rsquo;s not valid because Lhs and Rhs would not be valid identifiers until after the parser has passed the rest of the function prototype.\nTo work around this, C++11 introduced a new function declaration syntax, with a trailing-return-type:\n1 2 template\u0026lt;class Lhs, class Rhs\u0026gt; auto adding_func(const Lhs \u0026amp;lhs, const Rhs \u0026amp;rhs) -\u0026gt; decltype(lhs+rhs) {return lhs + rhs;} In this syntax, auto and decltype keywords are combined.\nReferences C++11 Wikipedia Stackoverflow ","description":"There is arrow (-\u003e) in function heading in C++. What is it?","id":2,"section":"programming","tags":["cplusplus","cpp"],"title":"What is arrow (-\u003e) in function declaration in C++","uri":"https://favoritekk.github.io/programming/cplusplus-function-with-arrow/"},{"content":"This is my first post about creating a personal blog. I plan to create my own blog using a static site generator, and a free host service to host my created blog.\nI first tried to use Jekyll for generating my static site. But I found out I\u0026rsquo;m more familiar with Go language, so I decided to use Hugo - a build-website framework. And I\u0026rsquo;ll use Github Page to host my blog site, it\u0026rsquo;s totally free according to my plan.\nPresiquite Before proceeding, I assume that you have these requirements:\n Familiar with Markdown Github account Install Hugo I\u0026rsquo;m using Homebrew on Mac OS for this guide. To install Hugo, open a terminal:\n $ brew install hugo\n$ hugo version # verify hugo installed\n Words after \u0026lsquo;#\u0026rsquo; are comments, you don\u0026rsquo;t need to type. Create a new project Open a terminal and cd to your workspace.\n $ hugo new site quickstart\n$ ls # there\u0026rsquo;ll be a folder named quickstart\n Add a theme A theme will describe how your blog looks like (its color, format, content .etc). See themes.gohugo.io and look for what fits your taste. I\u0026rsquo;ll choose zzo theme for this guide.\n Go to the theme\u0026rsquo;s Github repository and download it. (In my choice, it is hugo-theme-zzo). Ectract that downloaded zip file. Then rename it to zzo, and move the folder to quickstart\u0026rsquo;s themes folder. The folder structure should look like this:\n├── quickstart │ ├── archetypes │ ├── config │ ├── content │ ├── ... │ └── themes │ └── zzo Then copy config, content, resources folders in themes/zzo/exampleSite/ and paste them inside quickstart/.\nAdd your own blog post The folder content we\u0026rsquo;ve just copied, contains our blog posts. These blog posts are written in Markdown, plus extra Hugo\u0026rsquo;s terms. Let\u0026rsquo;s add a post.\n Copy markdown-syntax.md (in content/en/posts folder) file into a new file named my-first-post.md. Open my-first-post.md, edit its content like so: 1 2 3 4 5 6 7 8 --- title: My First Post date: 2019-12-20T12:00:06+09:00 description: My first post draft: false --- Hello World! date is when you write; description appears in Home\u0026rsquo;s blog list; draft false to publish it in deployment.\n Start the Hugo server $ hugo server\n\u0026hellip;\nWeb Server is available at http://localhost:1313/ (bind address 127.0.0.1)\nPress Ctrl+C to stop\n Then access your site at http://localhost:1313/, you should see something similar to this: Customize the Theme Open config.toml file, change its baseURL, title .etc; anything that might be your own personal information. The baseURL will be my blog address on Github Page.\n1 2 3 4 baseURL = \u0026#34;https://quickstart.github.io/\u0026#34; title = \u0026#34;My Porfolio\u0026#34; theme = \u0026#34;zzo\u0026#34; ... You should refer to the chosen Theme\u0026rsquo;s documentation for how to customize it properly. Every theme should have a Home page for documentation and Demo; look for them.\nCreate your site address on Github Page Login to Github, and create a repository named \u0026lt;your username\u0026gt;.github.io. Let\u0026rsquo;s assume your Github username is quickstart, then baseURL in config.toml will match your repository, ex: https://quickstart.github.io/.\n Clone quickstart.github.io repository to your local machine:\n $ git clone https://github.com/quickstart/quickstart.github.io.git\n Now, go to quickstart.github.io repository\u0026rsquo;s Setting and scroll to GitHub Pages section; choose the Source to Branch: main; then click Save. Deploy your project Change to quickstart project directory and build your static site: $ hugo\n Your site content will be generated to a folder named public. Then change to qickstart.github.io repository directory and add your generated content: $ rm ./**\n$ cp -r \u0026lt;quickstart\u0026gt;/public/** ./\n Line #1 removes old content, do this from the second time you deploy your site. And line #2 copies quickstart/public/\u0026rsquo;s content into quickstart.github.io\u0026rsquo;s content.\n After that, make your content to remote. $ git push origin main\n Finally, your site is live. Visit it at https://quickstart.github.io/. References Hugo Quickstart Zzo theme Github Pages ","description":"My first post - A guide to create a personal blog site with Hugo + Github Page","id":3,"section":"programming","tags":["Hugo"],"title":"My First Post","uri":"https://favoritekk.github.io/programming/my-first-post/"},{"content":"Greeting,\nMy name is Khiêm (Kevin). I\u0026rsquo;m a software developer, from Ninh Hiệp village, Việt Nam. My bio includes computer programming, art, game and music. My favorite answers for the really-worth-asking question \u0026ldquo;Why are we here?\u0026quot;\n I don\u0026rsquo;t know why we are here, but I\u0026rsquo;m pretty sure it\u0026rsquo;s NOT in order to enjoy ourselves.\n Ludwig Wittgenstein We are here on Earth to help others. What the others are here for, I\u0026rsquo;ve NO idea.\n W.H. Auden My boom boom melodies\nfavo · Nhac Tay ","description":"About page","id":4,"section":"","tags":null,"title":"About","uri":"https://favoritekk.github.io/about/"}]