<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by qhwa on Medium]]></title>
        <description><![CDATA[Stories by qhwa on Medium]]></description>
        <link>https://medium.com/@qhwa-85848?source=rss-434d5cf42aa3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Yqlu5f7-cltrYPPgRhk6fw.jpeg</url>
            <title>Stories by qhwa on Medium</title>
            <link>https://medium.com/@qhwa-85848?source=rss-434d5cf42aa3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:47:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@qhwa-85848/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Six books that helped me become a better software team leader]]></title>
            <link>https://qhwa-85848.medium.com/six-books-helped-me-become-a-better-software-team-leader-ba403fbc3d9e?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba403fbc3d9e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-team]]></category>
            <category><![CDATA[leadership-development]]></category>
            <category><![CDATA[leadership-skills]]></category>
            <category><![CDATA[books]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Sat, 05 Jun 2021 00:49:44 GMT</pubDate>
            <atom:updated>2022-01-21T03:02:18.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WWhZeKD1sERp22xt3C8Pkw.jpeg" /><figcaption>photo credit: <a href="https://unsplash.com/photos/UWRn79hIQKY">https://unsplash.com/photos/UWRn79hIQKY</a></figcaption></figure><p>Today I was asked by a colleague to recommend some books. He wants to be better at leading software developers. While it is certainly an honor, it’s also a good chance for me to review what books have helped me along the way.</p><p>Definitely, I’m not an expert on leadership because I’m not so good at communicating with my bosses at this moment. But I developed my own way of leading in which everyone grows fast, and development activities are performed at a pace. We won a lot together.</p><p>So if you are a team leader of software developers and want to build a good culture, I hope the following books can be helpful to you too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hezb0WDhT-YSTSny.jpg" /></figure><h3>Code Simplicity.<br>The Science of Software Development</h3><p>by Max Kanat-Alexander</p><p>This is an amazing book on software development science. It can help us understand better software development and make wiser decisions. Leading technology teams is about expertise. A better general wins more battles and brings more soldiers home alive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9QJtWX9wnWCOlWdp.jpg" /></figure><h3>Pragmatic Thinking &amp; Learning<br>Refactor Your Wetware</h3><p>by Andy Hunt</p><p>This famous book reveals how a software developer can grow from novice to expert, step by step. I was introduced to Skill Theory first time by this book. And it specialized focus on the development of software skills. A good leader empowers growth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TeM0qFk0zLLxS00Z.jpg" /></figure><h3>Peak<br>Secrets from the New Science of Expertise</h3><p>by Anders Ericsson and Robert Pool</p><p>This is a life-changed book for me. Before it, I thought that genus people are gifted, experts are talented. This is wrong because skills are developed under certain patterns. Gifts and talents are good to start with but not always necessary for success. Leading is a skill so it follows the science of Skill Theory. A lot of researches have been done on Skill Theory. This book is a great way to learn the meta-skill of developing any skill.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/0*fwQsbNSBtMZQMx8K" /></figure><h3>Messy<br>The Power of Disorder to Transform Our Lives</h3><p>by Tim Harford</p><p>As a leader, a common mistake to make is to hire selves. Diversity is crucial to ecosystems. A good team is an ecosystem where everyone is is connected and inspiriting each other, without a leader noticing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*f5Dw74yTGiVmt0hy" /></figure><h3>Thinking in Systems<br>A Primer</h3><p>by Donella H. Meadows</p><p>I tried to build the team as a system where good behaviors are rewarded. It worked very well and almost everything worked without me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GzeK6QKjJsUHWM_k.jpg" /></figure><h3><strong>Switch</strong><br>How to Change Things When Change Is Hard</h3><p>by Chip Health &amp; Dan Health</p><p>As a leader, you occasionally need to drive other people to do something you want and create rules or boundaries. Changes are sometimes hard to sell. Well, this book can help you encourage your team.</p><p>There are lots of other books that can help you become a better software leader. What I read is just a very small subset of it. So don’t stop finding new books to blow your mind. Reading is like listening to someone wise talking to you and you will definitely learn.</p><p>Don’t hesitate to recommend what are your favourites in the comment too!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba403fbc3d9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Questions for BitString, Binary, Charlist, and String in Elixir — Part 3: String and Charlist]]></title>
            <link>https://qhwa-85848.medium.com/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-3-string-and-charlist-a317b85aff30?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a317b85aff30</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[unicode]]></category>
            <category><![CDATA[utf-8]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Sun, 10 May 2020 13:16:58 GMT</pubDate>
            <atom:updated>2020-05-11T07:45:40.058Z</atom:updated>
            <content:encoded><![CDATA[<h3>Questions for BitString, Binary, Charlist, and String in Elixir — Part 3: String and Charlist</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yQt3OO8BITcBv_tjuTqNBA.jpeg" /></figure><p>This is the third group of questions of the serial:</p><p><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-5b7e0c1e41a0">Part 1: BitString (or bits)<br></a><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-2-binary-or-bytes-687315789030">Part 2: Binary (or bytes)<br></a>Part 3: String and Charlist</p><p>Here are some questions for String and Charlist in Elixir.</p><p><strong>Q: Is the String definition in Elixir the same as in Erlang?<br></strong>A: No.</p><p><strong>Q: What is String in Erlang?<br></strong>A: A string in Erlang can be:<br>- <em>A binary with UTF-8-encoded Unicode codepoints.</em><br><em>- A list of UTF-8-encoded Unicode codepoints. (exactly the </em><em>Charlist in Elixir)<br>- A mix of the two above.</em></p><p><strong>Q: What is String in Elixir?<br></strong>A: A binary with UTF-8-encoded Unicode codepoints.</p><p><strong>Q: Are strings always binaries? (in Elixir, for this and later questions)</strong><br>A: Yes.</p><p><strong>Q: What is Unicode?</strong><br>A: Unicode (<a href="https://www.unicode.org/">https://www.unicode.org</a>) is a set of specifications that list every character, also called “user-perceived character”, used by written languages and give each character its own unique codes, or “codepoints”. (A character may have multiple codepoints.)</p><p>The Unicode standard contains a lot of tables listing characters and their corresponding codepoints:<br>0061 ‘a’; LATIN SMALL LETTER A<br>0062 ‘b’; LATIN SMALL LETTER B<br>0063 ‘c’; LATIN SMALL LETTER C<br>…<br>007B ‘{‘; LEFT CURLY BRACKET<br>…<br>2167 ‘Ⅷ’; ROMAN NUMERAL EIGHT<br>2168 ‘Ⅸ’; ROMAN NUMERAL NINE<br>…<br>265E ‘♞’; BLACK CHESS KNIGHT<br>265F ‘♟’; BLACK CHESS PAWN<br>…<br>1F600 ‘😀’; GRINNING FACE<br>1F609 ‘😉’; WINKING FACE<br>…</p><p><strong>Q: Is there any limit on the number of characters in Unicode?</strong><br>A: No.</p><p><strong>Q: What is UTF-8?</strong><br>A: It’s one of the encoding methods of Unicode. Unicode just cares about mapping characters to codepoints without representing them in memory/disk, which is the job of character encodings. A character can be represented differently in memory or disk in different encoding methods.</p><p>character -&gt; code points -&gt; bytes in memory/disk</p><p><strong>Q: Is </strong><strong>&quot;abcd&quot; a valid string?</strong><br>A: Yes.</p><p><strong>Q: Is </strong><strong>&lt;&lt;&quot;abcd&quot;&gt;&gt; a valid string?<br></strong>A: Yes.</p><p><strong>Q: Is </strong><strong>&#39;abcd&#39; a valid string?<br></strong>A: No. It’s a Charlist.</p><p><strong>Q: Is </strong><strong>~s&#39;abcd&#39; a valid string?</strong><br>A: Yes.</p><p><strong>Q: Is </strong><strong>&lt;&lt;237,160,128&gt;&gt; a valid string?</strong><br>A: No. Because if decoded as UTF-8 specifies, its corresponding codepoint is U+D800 , which is not a valid UTF-8 codepoint according to the specification.</p><p><strong>Q: Given a raw string in memory, </strong><strong>6c f0 9f 8d ad 70, how to decode it to user-perceived characters?<br></strong>A: The simplest way is to manually type `&lt;&lt;0x6c, 0xf0, 0x9f, 0x8d, 0xad, 0x70&gt;&gt;` in IEx, but let’s make more fun from decoding it in UTF-8 way.</p><p>Step 1, turn it into binaries:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34aae34165615afdee9d1f19fdbf15e6/href">https://medium.com/media/34aae34165615afdee9d1f19fdbf15e6/href</a></iframe><p>Step 2, search for leading 1s.</p><p>The first byte 01101100 has no leading 1, which means it’s an ASCII character. Its codepoint is simply the integer value of it, which is 001101100 in binary, or 6c in hexadecimal.</p><p>The second byte <strong>1111</strong>0000 has four 1 at the beginning. It means four bytes, including current byte and three bytes following, will be used to represent a codepoint. Ok, we’re going to decode these four bytes soon. Let’s skip them right now and go to the sixth byte.</p><p>The sixth byte 0111000 is also an ASCII character due to its leading 0. So its codepoint is 0111000 in binary, or 70 in hexadecimal.</p><p>Step 3, let’s decode the four bytes from 2nd to 5th.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efe2b9f33b61d0c792906613e6d98aed/href">https://medium.com/media/efe2b9f33b61d0c792906613e6d98aed/href</a></iframe><p>The Unicode codepoint is 0x1F36D.</p><p>In summary, the codepoints are [U+00<strong>6C</strong>, U+<strong>1F36D</strong>, U+00<strong>70</strong>].</p><p>Therefore, we know the characters are &quot;\u006C\u{1F36D}\u0070&quot;. As user-perceived characters, they are l, 🍭, and p.</p><p>Sweet!</p><p><strong>Q: How can I get codepoints of a string as a list of codepoints?<br></strong>A: You can get them from a Charlist.</p><p>For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ea8ec036df376a381062d6d3ca03ae9/href">https://medium.com/media/3ea8ec036df376a381062d6d3ca03ae9/href</a></iframe><p>Also, you can use <a href="https://hexdocs.pm/elixir/String.html#to_charlist/1">String/to_charlist/1</a> to convert a string into a list of codepoints.</p><h3><strong>Summary</strong></h3><p>We’ve answered some question about BitString, Binary, String and Charlist in Elixir. Naming is hard in programming. If you can’t get their meaning by the first glance, here are their more-friendly names for you to understand them better:</p><ul><li>BitString ➯ <strong>Bits</strong></li><li>Binary ➯ <strong>Bytes</strong></li><li>String ➯ <strong>String</strong></li><li>Charlist ➯ <strong>CodepointList</strong></li></ul><p>Elixir provides brilliant supports to String and raw binary. I hope you have fun programming with binaries!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a317b85aff30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Questions for BitString, Binary, Charlist, and String in Elixir — Part 2: Binary (or bytes)]]></title>
            <link>https://qhwa-85848.medium.com/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-2-binary-or-bytes-687315789030?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/687315789030</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[string]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Sun, 10 May 2020 13:14:44 GMT</pubDate>
            <atom:updated>2020-05-14T02:10:55.704Z</atom:updated>
            <content:encoded><![CDATA[<h3>Questions for BitString, Binary, Charlist, and String in Elixir — Part 2: Binary (or bytes)</h3><p>This is the second group of questions of the serial:</p><p><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-5b7e0c1e41a0">part 1. BitString (or bits)</a><br>part 2. Binary (or bytes)<strong><br></strong><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-3-string-and-charlist-a317b85aff30">part 3. String and Charlist</a></p><p>Here are some questions on Binary in Elixir to check how we know it.</p><p><strong>Q: What is a binary?</strong><br>A: A sequence of bytes.</p><p><strong>Q: Is all String in Elixir also a binary?</strong><br>A: Yes.</p><p><strong>Q: Is there any binary not being a string?</strong><br>A: Yes, if it contains any non-UTF8-encoded Unicode character or any character not in Unicode standard.</p><p>For instance, &lt;&lt;0x80&gt;&gt; is not a valid string but a valid binary.</p><p><strong>Q: Is all binary in Elixir also a BitString?</strong><br>A: Yes.</p><blockquote>A bitstring is a sequence of zero or more bits, where the number of bits does not need to be divisible by 8. If the number of bits is divisible by 8, the bitstring is also a binary.</blockquote><p>via <a href="http://erlang.org/doc/programming_examples/bit_syntax.html">http://erlang.org/doc/programming_examples/bit_syntax.html</a></p><p>Bytes are always bits but not vice versa.</p><p>The relationship between BitString, Binary, and String (in Elixir) is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kqy9wGD8vmAg_VBZdKT6Nw.png" /></figure><p><strong>Q: What’re the differences between </strong><strong>&lt;&lt;..&gt;&gt; and </strong><strong>&quot;…&quot;?</strong><br>A: There are no differences. The double quote mark &quot; just tells the binary is <em>printable</em>.</p><p><strong>Q: Is </strong><strong>&lt;&lt;&gt;&gt; a binary?</strong><br>A: Yes. So is its equivalent <strong>&quot;&quot;</strong>.</p><p><strong>Q: Is </strong><strong>&lt;&lt;1&gt;&gt; a binary?</strong><br>A: Yes. So are its equivalents &lt;&lt;1::8&gt;&gt; and &lt;&lt;1::size(8)&gt;&gt; and &lt;&lt;1::integer-size(8&gt;&gt;.</p><p><strong>Q: Is </strong><strong>&lt;&lt;1234&gt;&gt; a binary?</strong><br>A: Yes. Actually it is &lt;&lt;210&gt;&gt; because 1234 overflows one byte, hence the last 8 bits of it are kept, which is Bitwise.band(1234, 0xFF).</p><p><strong>Q: Is </strong><strong>&lt;&lt;4::2, 128&gt;&gt; a binary?<br></strong>A: No.</p><p><strong>Q: Is </strong><strong>&lt;&lt;4::2, 128::22&gt;&gt; a binary?<br></strong>A: Yes.</p><p><strong>Q: Can binaries reference to another to reduce memory usage?<br></strong>A: Yes.</p><p>For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b393b029d7b6c961284d865ff5dadf4/href">https://medium.com/media/7b393b029d7b6c961284d865ff5dadf4/href</a></iframe><p>We can see the byte size of rest is 997, but it is referring to a 1000 bytes binary where it comes from.</p><p>This kind of binary is called <strong>sub-binary</strong>. In some cases, where the compiler knows the content of the binary is not going to be used, it can be optimized into a reusable <strong>match context</strong>, which only keeps pointers to the positions in the binary and hence is even more efficient both in memory and CPU.</p><p><strong>Q: Any example?</strong><br>A: Sure. Let’s compare the performance of sub-binary and match context:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72b226a2cf9e31c6049667223ee1fc10/href">https://medium.com/media/72b226a2cf9e31c6049667223ee1fc10/href</a></iframe><p>MyBinParser1 is about 2.6x <strong>slower</strong> (!) because it creates a sub-binary every time.</p><p><strong>Q: How do I avoid writing codes creating sub-binaries like </strong><strong>MyBinParse1 above?<br></strong>A: You can add a system environment variable ERL_COMPILER_OPTIONS=bin_opt_info to get warnings.</p><p>For example:<br>ERL_COMPILER_OPTIONS=bin_opt_info mix compile --force</p><p><strong>Q: Do I need to care about big binaries?</strong><br>A: In most cases, no. Binaries up to 64 bytes are stored in memory along with the process, named <strong>heap binary</strong>. they are small and efficient.</p><p>Longer binaries are stored in shared memory spaces, with references pointing to them from processes, called <strong>reference-counted binaries</strong>.</p><p>Keep in mind that never create binaries or sub-binaries whose content you’re not interested in.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=687315789030" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Questions for BitString, Binary, Charlist, and String in Elixir — Part 1: BitString (or bits)]]></title>
            <link>https://qhwa-85848.medium.com/questions-for-bitstring-binary-charlist-and-string-in-elixir-5b7e0c1e41a0?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b7e0c1e41a0</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[bitstring]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Sat, 18 Apr 2020 02:30:10 GMT</pubDate>
            <atom:updated>2020-05-11T08:16:05.733Z</atom:updated>
            <content:encoded><![CDATA[<h3>Questions for BitString, Binary, Charlist, and String in Elixir — Part 1: BitString (or bits)</h3><p>This is the first group of questions of the serial:</p><p>part 1. BitString (or bits)<strong><br></strong><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-2-binary-or-bytes-687315789030">part 2. Binary (or bytes)</a><br><a href="https://medium.com/@qhwa_85848/questions-for-bitstring-binary-charlist-and-string-in-elixir-part-3-string-and-charlist-a317b85aff30">part 3. String and Charlist</a></p><p>Here are some questions related to BitString in Elixir, a functional programming language. Check how well you know BitString? 😃</p><p><strong>Q: Is </strong><strong>&lt;&lt;&gt;&gt; a BitString?</strong><br>A: Yes, it’s a zero-length BitString.</p><p><strong>Q: Is </strong><strong>&quot;&quot; a BitString?</strong><br>A: Yes, it is the same as &lt;&lt;&gt;&gt;.</p><p><strong>Q: Is </strong><strong>&lt;&lt;0&gt;&gt; a BitString?</strong><br>A: Yes.</p><p><strong>Q: What is the value of </strong><strong>byte_size(&lt;&lt;0&gt;&gt;) ?</strong><br>A: 1.</p><p><strong>Q: What’s the value of </strong><strong>bit_size(&lt;&lt;0&gt;&gt;) ?</strong><br>A: 8.</p><p><strong>Q: Are </strong><strong>&lt;&lt;0::8&gt;&gt; and </strong><strong>&lt;&lt;0&gt;&gt; the same? And why?</strong><br>A: Yes. The segment size for an integer is 8 by default.</p><p><strong>Q: Is </strong><strong>&lt;&lt;0::size(8)&gt;&gt; an equivalent of </strong><strong>&lt;&lt;0::8&gt;&gt;?</strong><br>A: Yes.</p><p><strong>Q: Is </strong><strong>0 (the number zero) a BitStrnig?</strong><br>A: No.</p><p><strong>Q: Is </strong><strong>[0] a BitString?<br></strong>A: No, it’s a list.</p><p><strong>Q: Are BitStrings lists of bits?<br></strong>A: No. Bit sequences of BitStrings are consecutive in memory, lists are not.</p><p><strong>Q: Is </strong><strong>&lt;&lt;0::1&gt;&gt; a BitString?<br></strong>A: Yes.</p><p><strong>Q: What’s the value of </strong><strong>bit_size(&lt;&lt;0::1&gt;&gt;?<br></strong>A: 1.</p><p><strong>Q: Is </strong><strong>&lt;&lt;2::1&gt;&gt; a valid term in Elixir?<br></strong>A: Yes, despite its overflowing.</p><p><strong>Q: Is </strong><strong>&lt;&lt;2::1&gt;&gt; an equivalent of </strong><strong>&lt;&lt;0::1&gt;&gt;?<br></strong>A: Yes.</p><p><strong>Q: Is </strong><strong>&lt;&lt;0.0&gt;&gt; an equivalent of </strong><strong>&lt;&lt;0&gt;&gt; ?<br></strong>A: No.</p><p><strong>Q: What’s the value of </strong><strong>bit_size(&lt;&lt;0.0&gt;&gt;) ? Why?<br></strong>A: 64. <br>The default size of a float segment is 64. <br>In contrast, the default size of an integer segment is 8.</p><p><strong>Q: What’s inside </strong><strong>&lt;&lt;3.14&gt;&gt;?<br></strong>A: 64 bits representing the float number 3.14.</p><p>In detail, it can be decomposed into three parts:</p><ul><li>1 bit for <strong>sign</strong></li><li>11 bits for <strong>exponent</strong></li><li>52 bits for <strong>mantissa</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nwAtQMLqUsmhx8Vzo2zbeQ.png" /></figure><pre>iex&gt; &lt;&lt;<strong>sign</strong>::1, <strong>exponent</strong>::11, <strong>friction</strong>::bits&gt;&gt; = &lt;&lt;3.14&gt;&gt;<br>...&gt; <strong>sign</strong><br>0</pre><pre>...&gt; <strong>exponent</strong><br>1024</pre><pre>...&gt; <strong>friction</strong><br>&lt;&lt;145, 235, 133, 30, 184, 81, 15::size(4)&gt;&gt;</pre><p>And compose them back:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75219e2fd9058c8f707912ff7ecaa73f/href">https://medium.com/media/75219e2fd9058c8f707912ff7ecaa73f/href</a></iframe><p>with reference to <a href="https://en.wikipedia.org/wiki/IEEE_754">IEEE_754</a>.</p><p>Hopefully, in another post following, we’ll see questions related to Binaries in Elixir. Please stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b7e0c1e41a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Docker Images From An Elixir Project, Why and How]]></title>
            <link>https://qhwa-85848.medium.com/build-docker-images-from-an-elixir-project-why-and-how-78e19468210?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/78e19468210</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Tue, 10 Mar 2020 10:18:17 GMT</pubDate>
            <atom:updated>2020-03-11T00:28:59.682Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SNoEOxngxl2uP2CNcfMtYA.jpeg" /></figure><p>A few days ago, I read <a href="https://github.com/ferd">Fred Hebert</a>’s <a href="https://twitter.com/mononcqc/status/1219309554162421765">tweet</a>:</p><blockquote>~15 years ago, if you wanted a dynamic website with PHP, you installed a LAMP stack, wrote a few toy pages, uploaded the files to a $5 host, fiddled 30mins with htaccess (optional), and you were done.</blockquote><blockquote>I don’t recall seeing anything close to this easy since then.</blockquote><p>I feel the same. Deployments are really tedious today. A lot of abstractions around there. They all have their reasons to stand. But there are so many that we can’t easily see a single simple and beautiful way of it.</p><p>On the other hand, this is the best time. I believe with the deep knowledge of the problems and infrastructures, we can make deployment simple and delightful again. We’re just on the way of it.</p><p>In this article, I will show how to write a Dockerfile of 15 lines of code, for an Elixir project, to make a Docker image of it.</p><h3>Docker is a good match for Elixir</h3><p>Speaking of deployment, there are quite a lot of solutions out there for a typical Elixir project. Most of them rely on <a href="https://hexdocs.pm/mix/Mix.Tasks.Release.html">mix release</a>.</p><p>The mix task mix release was introduced by <a href="https://github.com/bitwalker">@bitwalker</a>’s <a href="https://github.com/bitwalker/distillery">Distillery</a>, and later (Elixir 1.9) integrated into the language built-in libraries. It produces executable files that are ready to deploy. You can distribute these files through networks and run them everywhere you want.</p><p>Well, not everywhere indeed.</p><p>Since cross-compiling is not supported at this moment, you have to make sure the computer where you run mix release (aka <strong><em>builder</em></strong>) has the same architecture and fundamental libraries installed (Kernel, standard C libraries, etc.) as the production servers. In another word, you can only deploy your project into the servers (aka <strong><em>runners</em></strong>) with the same architectures as the builder.</p><p><a href="https://www.docker.com/">Docker</a> can be used to keep a consistent environment for your application. It provides a way to make your files on the system always the same, as well as environment variables, IP addresses, and software dependencies.</p><p>With docker, we don’t worry about the builder/runner match problem anymore. Thanks to <a href="https://docs.docker.com/docker-for-mac/">docker for mac</a>, we can even use Macbooks as builders.</p><p>In this post, I’ll provide an example of a small Dockerfile which can be used to generate a Docker image from an Elixir project. It’s really simple and easy, opposite to many’s impression.</p><h3>Create a docker image from source code in two simple steps</h3><p>I’m going to show you how to write a Dockerfile for an Elixir project and try to describe the ideas behind it.</p><p>I also created a hex package to make this a single command line to let you create your Dockerfile without copying &amp; pasting. You can jump to that section if you like.</p><p>Alright, let’s start from an empty Dockerfile at the root of your project. Run this command in terminal to create it:</p><pre>$ touch Dockerfile</pre><p>Open Dockerfile with your favorite editor and we are going to code.</p><p>Our Docker image will be built within two steps:</p><p><strong>Step 1: Generating release files</strong></p><p>Put the following into the Dockerfile :</p><pre><strong>FROM</strong> qhwa/elixir-builder:latest <strong>AS</strong> builder</pre><pre><strong>ARG</strong> mix_env=prod<br><strong>ENV</strong> MIX_ENV=${mix_env}</pre><pre><strong>WORKDIR</strong> /src</pre><pre><strong>COPY</strong> mix.exs mix.lock /src/<br><strong>RUN</strong> mix deps.get --only $MIX_ENV</pre><pre><strong>ADD</strong> . .<br><strong>RUN</strong> mix release --path /app --quiet</pre><p>The first line of it ( <strong>FROM</strong> qhwa/elixir-builder:latest <strong>AS</strong> builder ) defines the base image, and give a name builder to this building step (or called stage in Docker’s terminology). We’ll refer to this name in step 2.</p><p>I use <a href="https://github.com/qhwa/docker-elixir-builder">qhwa/elixir-builder</a> instead of <a href="https://hub.docker.com/_/elixir">the official elixir image</a>. You can replace it with your favorite image. Just make sure:</p><ul><li>It should be the same OS as step 2.</li><li>It should have Elixir, hex, and rebar3 installed.</li></ul><p>I prefer and recommend <a href="https://github.com/qhwa/docker-elixir-builder">qhwa/elixir-builder</a> because it has been working well for me in the past years, and has other important tools such as Node.js installed.</p><p>Let’s move on to the next two lines:</p><pre><strong>ARG</strong> mix_env=prod<br><strong>ENV</strong> MIX_ENV=${mix_env}</pre><p>They provide a point for users to specify which mix environment (MIX_ENV) to build. For example, if we want to build with MIX_ENV as staging, we can run docker build command with: --build-arg mix_env=<strong>staging</strong>.</p><p><em>A piece of background info: There was a choice I made, to use the same docker image for different mix environments, or a different docker image for each different mix environment. I chose the first approach because many things, such as dependencies, supervisor trees, etc., may be different in each mix environment.</em></p><p>Then, we add mix.exs and mix.lock into the builder, and fetch the dependencies:</p><pre><strong>WORKDIR</strong> /src</pre><pre>COPY <strong>mix.exs mix.lock</strong> /src/<br>RUN mix deps.get <strong>--only $MIX_ENV</strong></pre><p>It’s tricky to only add mix.exs and mix.lock instead of the full project. This will give us better performance because we need to re-fetch dependencies only when these two files change.</p><p>At last, we build the release by running mix releaseand put the built files into /app , rather than the current working directory of /src :</p><pre>ADD . .<br>RUN <strong>mix release</strong> --path /app --quiet</pre><p>At this point, the Dockerfile is valid. And we can get a docker image from it by running docker build .</p><blockquote>Let us make it better, a smaller Docker image!</blockquote><p><strong>Step 2: Put release files onto an operating system</strong></p><p>Append the following to our dockerfile:</p><pre><strong>FROM</strong> qhwa/elixir-runner:latest</pre><pre><strong>ARG</strong> mix_env=prod<br><strong>ENV</strong> MIX_ENV=${mix_env}</pre><pre><strong>COPY</strong> --from=builder /app /app<br><strong>WORKDIR</strong> /app</pre><pre><strong>ENTRYPOINT</strong> [&quot;/app/bin/<em>YOUR_APP_NAME</em>&quot;]<br><strong>CMD</strong> [&quot;start&quot;]</pre><blockquote>Notice: please replace <em>YOUR_APP_NAME</em> with your real project name.</blockquote><p>Let’s go through it.</p><p>The first line defines the base image we use to host our application:</p><pre>FROM <strong>qhwa/elixir-runner</strong>:latest</pre><p>This is the second time we use <a href="https://docs.docker.com/engine/reference/builder/#from"><strong>FROM</strong> instruction</a> in the Dockerfile. It’s called <a href="https://docs.docker.com/develop/develop-images/multistage-build/">multi-stage build</a> and fits our scenario perfectly. After generating release files, we can start the second build step from a clean base system, and copy the release files generated from the last step.</p><p>The base image here is for the runner and is different from the builder.</p><blockquote>Why don’t we just reuse the builder’s base image, like <a href="https://docs.docker.com/develop/develop-images/multistage-build/">official Elixir image</a> or <a href="https://github.com/qhwa/docker-elixir-builder">qhwa/elixir-builder</a> in the last step?</blockquote><p>Well, the answer is that we have lots of tools, for example, Elixir, mix, and Node.js installed in the builder, but they are only required for development and would not be used in the production. By removing these tools, we can make our final Docker image smaller.</p><p>I prefer <a href="https://github.com/qhwa/docker-elixir-runner">qhwa/elixir-runner</a> image, it’s small (based on Alpine Linux) and ships with lots of useful utilities. It feels like a Swiss Knife.</p><p>Once again, you can replace it with your favorite base image for a runner. Just to remember that keep the same architecture as the builder.</p><p>Then we copy release files from our last stage, the building stage, in step 1:</p><pre>COPY <strong>--from=builder</strong> /app /app</pre><p>At last, we define the working directory and entry point for the image:</p><pre>WORKDIR /app</pre><pre><strong>ENTRYPOINT</strong> [&quot;/app/bin/<em>YOUR_APP_NAME</em>&quot;]<br><strong>CMD</strong> [&quot;start&quot;]</pre><p>Done!</p><p>We can run docker <strong>build</strong> -t my_app . in the terminal to build the image. After the image is built, we can run it by:</p><pre>$ docker <strong>run</strong> my_app</pre><p>or</p><pre>$ docker <strong>run</strong> --rm --tty --interactive my_app <strong>start_iex</strong></pre><p>to run and attach to the console.</p><h3>For Phoenix projects</h3><p>If your project has static assets, and you want to build them too, which is very typical for a <a href="https://www.phoenixframework.org/">Phoenix</a> project, you can just append following codes to the Dockerfile, right before RUN mix release …:</p><pre>...</pre><pre>RUN <strong>npm install</strong> --prefix assets<br>RUN <strong>npm run deploy</strong> --prefix assets<br>RUN <strong>mix phx.digest</strong><br>...</pre><h3>You don’t have to write for every project</h3><p>I’ve put the template into a hex package, called <a href="https://hex.pm/packages/dockerize">Dockerize</a>. You can install it with:</p><pre>$ hex <strong>archive.install</strong> hex dockerize</pre><p>and generate a Dockerfile with:</p><pre>$ mix <strong>dockerize.init</strong></pre><p>And there it is!</p><h3>Further topics</h3><p><strong>Runtime configurations</strong></p><p>We don’t want our secret credentials to spread into the builder. How to organize these runtime configurations?</p><p>Fortunately mix release already provides a way for us, you can check the <a href="https://hexdocs.pm/mix/Mix.Tasks.Release.html#module-runtime-configuration">documentation</a> to set it up. In most cases, we only need to add a config/releasee.exs .</p><p><strong>Create a CI/CD process based on the Docker image</strong></p><p>So far we have got our Docker image. It can be used in many different ways:</p><ul><li>a local <a href="https://docs.docker.com/engine/reference/run/">docker run</a> (for other developers to quick start)</li><li><a href="https://docs.docker.com/compose/">docker-compose</a> (very simple and delightful)</li><li><a href="https://kubernetes.io/">Kubernetes</a> (very powerful)</li><li>…</li></ul><p>This is an interesting topic but too long for this article. Hopefully, we may discuss it in another post. I strongly encourage you to give <a href="https://rio.io/">Rio</a> a try if you are using <a href="https://kubernetes.io/">Kubernetes</a>.</p><p>So, at last, happy deploying your Elixir projects!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78e19468210" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Minifying with Webpack]]></title>
            <link>https://qhwa-85848.medium.com/minifying-with-webpack-ff25738e7cd3?source=rss-434d5cf42aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff25738e7cd3</guid>
            <category><![CDATA[packing]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[qhwa]]></dc:creator>
            <pubDate>Thu, 05 May 2016 01:49:40 GMT</pubDate>
            <atom:updated>2016-05-05T05:52:57.564Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsOjd1jR3eOmJNJh4Zp42g.png" /><figcaption>webpack bundling</figcaption></figure><p>When coding in a modern modular way with <a href="https://webpack.github.io/">Webpack</a> is done, it enters the deploy phrase. We minify assets to reduce the network transfer cost. While bundling is not necessary nowadays thanks to <a href="https://http2.github.io/">HTTP/2</a>, minifying is still an important work, because in many cases we can reduce the size of data transfered between servers and clients:</p><ul><li>unreachable codes can be removed</li><li>comment codes can be removed</li><li>images can be optimised losslessly</li><li>variable names can be shortened in Javascript</li><li>HTML and SVGs can be optimised</li></ul><p>Here in <a href="http://www.helijia.com/">Helijia</a>, we use webpack to build mobile apps. Here are some notes from us on minifying with w<a href="https://webpack.github.io/">ebpack</a>.</p><h3>Minifying Javascript / CSS</h3><p><a href="https://webpack.github.io/">Webpack</a> is shipped with some builtin plugins which can do the job well.</p><ul><li><a href="https://webpack.github.io/docs/list-of-plugins.html#dedupeplugin">Dedupe Plugin</a> searches for similar files and deduplicated them in the output.</li><li><a href="https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin">UglyfiJS Plugin</a> uses the famous <a href="https://github.com/mishoo/UglifyJS">UglifyJS</a> to minify JS and CSS assets.</li></ul><h3>Use webpack stats to review your modules</h3><p>Webpack can generate packing stats in json format while packing. We use <a href="https://chrisbateman.github.io/webpack-visualizer/">Webpack visualizer</a> to visualize it to find which part of our works can be optimized. It helps a lot by providing an detail view inside the bundled file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*fNQylMzJclEm5VDbVuHwVQ.png" /></figure><h3>Minifying HTML</h3><p><a href="https://github.com/ampedandwired/html-webpack-plugin">HTML webpack plugin</a> can help minifying HTML. Here is an example config:</p><pre>new HtmlWebpackPlugin({<br>  template : path.join(__dirname, &#39;../src/template.html.ejs&#39;),<br>  filename : &#39;index.html&#39;,<br>  title    : &#39;河狸家&#39;,<br>  inject   : &#39;body&#39;,<br>  minify   : {<br>    html5                          : true,<br>    collapseWhitespace             : true,<br>    minifyCSS                      : true,<br>    minifyJS                       : true,<br>    minifyURLs                     : false,<br>    removeAttributeQuotes          : true,<br>    removeComments                 : true,<br>    removeEmptyAttributes          : true,<br>    removeOptionalTags             : true,<br>    removeRedundantAttributes      : true,<br>    removeScriptTypeAttributes     : true,<br>    removeStyleLinkTypeAttributese : true,<br>    useShortDoctype                : true<br>  }<br>}),</pre><h3>Minifying SVG</h3><p>we are currently using <a href="https://github.com/rpominov/svgo-loader">svgo-loader</a> to keep SVGs clean.</p><p>before:</p><pre><strong>&lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;</strong><br><strong>&lt;!DOCTYPE svg PUBLIC &quot;-//W3C//DTD SVG 1.1//EN&quot; &quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd&quot;&gt;</strong><br>&lt;svg width=&quot;100%&quot; height=&quot;100%&quot; viewBox=&quot;0 0 10 18&quot; version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; xml:space=&quot;preserve&quot; style=&quot;fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;&quot;&gt;<br>    &lt;path id=&quot;Shape-Copy&quot; d=&quot;M9.157,0.136L0.157,8.636C-0.052,8.834 -0.052,9.166 0.157,9.364L9.157,17.864C9.357,18.053 9.674,18.044 9.864,17.843C10.053,17.643 10.044,17.326 9.843,17.136L1.222,8.994L9.843,0.864C10.044,0.674 10.053,0.357 9.864,0.157C9.674,-0.044 9.357,-0.053 9.157,0.136Z&quot; style=&quot;fill:rgb(189,157,98);&quot;/&gt;<br>&lt;/svg&gt;</pre><p>after:</p><pre>&lt;svg class=&quot;SVGInline-svg icon-svg&quot; style=&quot;width: 1em;height: 1em;&quot; viewBox=&quot;0 0 10 18&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; fill-rule=&quot;evenodd&quot; clip-rule=&quot;evenodd&quot; stroke-linejoin=&quot;round&quot; stroke-miterlimit=&quot;1.414&quot;&gt;&lt;path d=&quot;M9.157.136l-9 8.5a.5.5 0 0 0 0 .728l9 8.5a.5.5 0 0 0 .686-.728l-8.62-8.142 8.62-8.13a.5.5 0 0 0-.686-.728z&quot; fill=&quot;#bd9d62&quot;&gt;&lt;/path&gt;&lt;/svg&gt;</pre><h3>Minifying bitmap Images</h3><p><a href="https://github.com/tcoopman/image-webpack-loader">Image-webpack-loader</a> can save a lot bandwidth.</p><h3>Conclusion</h3><p>Webpack provides a powerful way of web developing, we use some plugins to automate the deployment, and there are more out of our view. So let’s keep exploring and make web development more enjoyable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff25738e7cd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>