<?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 Yu-Ming Liao on Medium]]></title>
        <description><![CDATA[Stories by Yu-Ming Liao on Medium]]></description>
        <link>https://medium.com/@yumingliao?source=rss-e8cfe0443ff7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Yu-Ming Liao on Medium</title>
            <link>https://medium.com/@yumingliao?source=rss-e8cfe0443ff7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 05:50:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yumingliao/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[Synchronization In Two Programming Langauages]]></title>
            <link>https://medium.com/@yumingliao/synchronization-in-two-programming-langauages-0fbd47784676?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/0fbd47784676</guid>
            <category><![CDATA[synchronous]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[haskell]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Mon, 27 Oct 2025 13:22:37 GMT</pubDate>
            <atom:updated>2025-10-27T13:22:37.424Z</atom:updated>
            <content:encoded><![CDATA[<p>Comparing React with Synchron</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P02pJtkDqrNKDgW4PYTqoA.png" /></figure><p>Recently I started to the React and NextJS journey, trying to build something in frontend. I found better-auth, better-auth-ui, and react-leaflet very useful.</p><p>However, I want to talk about the similarity shared with React and <a href="https://github.com/pkamenarsky/synchron">Synchron</a>.</p><p>And here is <a href="https://github.com/YuMingLiao/synchron">my fork</a>. I tried to make it work locally and make it a flake. For poking around, you can nix develop .#final to get a shell with ghc and this package.</p><p>I also add orr io actions. It can’t kill threads early now like<a href="https://ajnsit.github.io/concur-documentation/ch02-08-input-output.html"> Allow cancelling long running IO actions in response to GUI event</a>, but it did step through orr with lengthy io actionsearly.</p><p><strong>useEffect</strong></p><p>useEffect(...) (without dependency array)</p><p>It is a bit like <a href="https://ajnsit.github.io/concur-documentation/ch02-08-input-output.html">Show a “Loading” screen while performing IO</a>.</p><p>useEffect(...,[])</p><p>I guess it just needs a boolean variable to only switch once in a recursive function.</p><p>useEffect(...,[a,b])</p><p>I tried two streams in Synchron. And it works! Though I haven’t tried three of them.</p><p><strong>useMemo</strong></p><p>Haskell is lazy, so it is kinds of a built-in.</p><p><strong>useCallback</strong></p><p>The same above.</p><p><strong>useState</strong></p><p>It’s basically var &quot;initial string&quot; $ \v -&gt; loop v $ stream $ \s -&gt; text s</p><p>and putVar vsomewhere.</p><p><strong>useRef</strong></p><p>Use function argument and recurisve functions.</p><p>Or in var &quot;&quot; $ \v -&gt; , use readVar and putVar but don’t use v in a component.</p><p><strong>useReducer</strong></p><p>Just readVar , a smart constructor and putVar .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0fbd47784676" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Files, “Freeze”!]]></title>
            <link>https://medium.com/@yumingliao/files-freeze-dc4560eacbe3?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc4560eacbe3</guid>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Sat, 23 Aug 2025 04:35:05 GMT</pubDate>
            <atom:updated>2025-08-23T04:35:05.862Z</atom:updated>
            <content:encoded><![CDATA[<h4>Nix can convert a tree of files into fixed-output derivations, <strong>separately</strong>.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2hKraoAtbjfqeC7Ym00iiQ.jpeg" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c79dc381a3af165b29c6ca6fe95aefd2/href">https://medium.com/media/c79dc381a3af165b29c6ca6fe95aefd2/href</a></iframe><p>When <a href="https://github.com/nmattia/snack">snack </a>compiles haskell module separately, the current nix expression sees the source folder as a whole. If one file changes, `readDir` has to re-calculate the source path. With this trick, file changes becomes more accurate in nix evaluation.</p><p>[1]: <a href="https://hackage.haskell.org/package/nix-freeze-tree">nix-freeze-trees</a></p><p>[2]: <a href="https://nixos.wiki/wiki/Nix_Hash">Nix Hash</a></p><p>[3]: <a href="https://www.tweag.io/blog/2023-11-28-file-sets/">Source filtering with file sets</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc4560eacbe3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When Nothing Goes Right, Turn Left.]]></title>
            <link>https://medium.com/@yumingliao/when-nothing-goes-right-turn-left-51fa7392c8f9?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/51fa7392c8f9</guid>
            <category><![CDATA[trees]]></category>
            <category><![CDATA[bfs-and-dfs]]></category>
            <category><![CDATA[haskell]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Sat, 23 Aug 2025 03:46:33 GMT</pubDate>
            <atom:updated>2025-08-23T04:09:51.786Z</atom:updated>
            <content:encoded><![CDATA[<p>How come `foldl (foldr f)` traverse a tree breadth-first?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A-vlBTUIoUX53hwi" /><figcaption>Photo by <a href="https://unsplash.com/@narrowedge?utm_source=medium&amp;utm_medium=referral">Jacob Kiesow</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><pre>bfe :: Tree a -&gt; [a]<br>bfe r = f r b []<br>  where<br>    f (Node x xs) fw bw = x : fw (xs : bw)<br>  <br>    b [] = []<br>    b qs = foldl (foldr f) b qs []</pre><p>I found an BTS haskell code here:</p><p><a href="https://doisinkidney.com/posts/2018-12-18-traversing-graphs.html">Pure and Lazy Breadth-First Traversals of Graphs in Haskell - Donnacha Oisín Kidney</a></p><p>But I couldn’t understand it at the moment.</p><p>After three days later, I finally figured it out all the type signatures in ghci.</p><pre>f :: Tree a -&gt; ([[Tree a]] -&gt; [a]) -&gt; [[Tree a]] -&gt; [a]<br>f (Node x xs) fw bw = x : fw (xs : bw)</pre><ol><li>To create a node list breadth-first, the root x should be, of course, in front of the result list.</li><li>fw is the recursive function. It is applied to the rest nodes in the list.</li><li>The seemingly redundant bw here turns the applied `f node fw` <strong>a </strong><a href="https://wiki.haskell.org/Difference_list"><strong>difference list</strong></a><strong> as a function</strong>, and the last argument [], whether in `f` or in `foldl`, turns it back a list.</li></ol><p>To me, the tricky part is the `foldl (foldr f)` here.</p><pre>b [] = []<br>b qs = foldl (foldr f) b qs []</pre><pre>f :: Tree a -&gt; ([[Tree a]] -&gt; [a]) -&gt; [[Tree a]] -&gt; [a]<br>f (Node x xs) fw bw = x : fw (xs : bw)<br>-- ^^^^^^^^^^a ^b ^b as (a -&gt; b -&gt; b) in foldr<br><br>foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b<br>foldr f :: ([[Tree a]] -&gt; [a]) -&gt; [Tree a] -&gt; [[Tree a]] -&gt; [a]<br>--         ^^^^^^^^^^^^^^^^^^b -&gt; ^^^^^[a] -&gt; ^^^^^^^^^^^^^^^^b as(b -&gt; a -&gt; b) in foldl<br><br>foldl :: (b -&gt; a -&gt; b) -&gt; b -&gt; [a] -&gt; b<br>-- foldl (foldr f) :: ([[Tree a]] -&gt; [a]) -&gt; [[Tree a]] -&gt; [[Tree a]] -&gt; [a]<br>+ --                    ^^^^^^^^^^^^^^^^b -&gt; ^^^^^^^^[a] -&gt; ^^^^^^^^^^^^^^^^^ Apply function to argument [] and get the   result [a]</pre><p>So they all output <strong>a deference list as a function (</strong>if the last argument bw has never been passed).</p><p>f turns a Tree a into [a] (leaving [[Tree a]] for fw, which is b, or foldl (foldr f) b).</p><p>So f handles a node on top of a tree.</p><p>foldr f takes a [Tree a] and return [a].</p><p>So foldr f handles nodes on top of a list of Tree a.</p><p>b in foldl becomes foldr’s intial value b, and thus fw in f. foldl handles a list of children.</p><p>So foldl handles a list of lists.</p><p>f calls foldl, which calls foldr, which calls f, … to build the breadth-first-ordered list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51fa7392c8f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Do You Remember?”— Nix Can Memoize!]]></title>
            <link>https://medium.com/@yumingliao/do-you-remember-nix-can-memoize-d359d1b9755e?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d359d1b9755e</guid>
            <category><![CDATA[memoization]]></category>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Sun, 18 Aug 2024 03:23:38 GMT</pubDate>
            <atom:updated>2024-08-18T03:40:08.807Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2qoZuQw4SqZFDoBj" /><figcaption>Photo by <a href="https://unsplash.com/@fujix_pal?utm_source=medium&amp;utm_medium=referral">Sehajpal Singh</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Long time ago, I was tired of spending time waiting large Haskell project compilation. So I start playing around with <a href="https://github.com/nmattia/snack">snack</a> (a Nix-based incremental build tool for Haskell projects).</p><p>I learned Nix from trying to understand this project. Nix’s really elegant. <br>And I was amazed that all those unchanged Haskell modules can be built once and linked many times.</p><p>One thing I could share in this journey is<strong> how to memoize in nix.</strong></p><p>Below is an example stole from here:</p><p><a href="https://discourse.nixos.org/t/memoize-result-of-builtins-exec/2028/3?u=dycan">Memoize result of builtins.exec</a></p><p>Here tail in primes is shared by passing primes into get . So the primes list is memoized and evaluation time is saved.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e8f97f29138c1baedf33f3fa9926d6e/href">https://medium.com/media/5e8f97f29138c1baedf33f3fa9926d6e/href</a></iframe><p>And here primes is not shared. So bar took twice the time foo need to evaluate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/355ad7f38edfde9df26aead691af643a/href">https://medium.com/media/355ad7f38edfde9df26aead691af643a/href</a></iframe><p><a href="https://nixos.wiki/wiki/Nix_Evaluation_Performance">NixOS Wiki</a> has more thoughts about how to think it as thunks.</p><p>And<a href="https://github.com/DavHau/nix-eval-cache"> nix-eval-cache</a> can cache evaluation of nix functions.</p><p>Moreover, nix is experimenting <a href="https://www.youtube.com/watch?v=kvhGcRIyI3g">Dynamic Derivation</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FGs069dndIYk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DGs069dndIYk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FGs069dndIYk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5af931a2d74295fd5347e3def75fb37/href">https://medium.com/media/f5af931a2d74295fd5347e3def75fb37/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d359d1b9755e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[理想型世界盃的資料結構──Heap]]></title>
            <link>https://medium.com/@yumingliao/%E7%90%86%E6%83%B3%E5%9E%8B%E4%B8%96%E7%95%8C%E7%9B%83%E7%9A%84%E8%B3%87%E6%96%99%E7%B5%90%E6%A7%8B-heap-bc556c3c68ed?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc556c3c68ed</guid>
            <category><![CDATA[haskell]]></category>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[priority-queue]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Fri, 09 Aug 2024 03:13:22 GMT</pubDate>
            <atom:updated>2025-08-23T05:22:51.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aN0ERGK_FA2Kl2jX4cnIbw.jpeg" /></figure><p>一直覺得Heap很神奇，可以O(1)找出極值。後來想想，這就是利用數學的遞移律呀：</p><blockquote>若 a&gt;b 且 b&gt;c ，則 a&gt;c 。</blockquote><p>有一天突然想到，這不就是韓綜最愛問的理想型世界盃大賽嗎？問到最後總會有位偶像勝出，那就是極值阿。</p><p>為了在Haskell實作Dijkstra演算法，在比較最短路徑時，需要一個找出極值的資料結構。本來用了Wikipedia的<a href="https://literateprograms.org/priority_queue__haskell_.html">Piority Search Queue</a>，但根據profile，時間似乎都花在union上了。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1527fce1efba5669e3e26864336dfc59/href">https://medium.com/media/1527fce1efba5669e3e26864336dfc59/href</a></iframe><p>後來又在StackOverflow的<a href="https://stackoverflow.com/questions/27136691/is-there-a-simple-way-to-implement-a-fast-priority-queue-in-haskell">答案</a>中找了一個Pairing Heap，Haskell實作很簡單。測資立馬就從過22個到30個全過。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/489a299a9b196a3735bf6fc8abfe8e58/href">https://medium.com/media/489a299a9b196a3735bf6fc8abfe8e58/href</a></iframe><p>偏心偏慣了，就算要退而求其次，也是心底早已有了答案。至̸今̸仍̸是̸未̸解̸之̸謎̸。̸</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc556c3c68ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[從演算法題目「大貓小貓去買魚」看逛街時間管理]]></title>
            <link>https://medium.com/@yumingliao/%E9%80%9B%E8%A1%97-%E6%81%95%E4%B8%8D%E5%A5%89%E9%99%AA-32de4ee4e83e?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/32de4ee4e83e</guid>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[breadth-first-search]]></category>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[dijkstra]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Thu, 08 Aug 2024 09:21:47 GMT</pubDate>
            <atom:updated>2024-08-08T09:53:57.582Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UOLT73Odp8kTyoeFhOyD3w.jpeg" /></figure><p>題目傳送門：<a href="https://www.hackerrank.com/challenges/synchronous-shopping/problem">synchronous shopping</a></p><p>在unweighted graph中，用BFS可以找到某個點到每個點的最短路徑。</p><p>那weighted graph呢？在unweighted graph中可以看作每段路徑時間花費為1。使用BFS廣度優先，意味著不會先去走較花時間的路。</p><p>而Dijkstra，在weighted graph中，走完接下來的一步，總時間花費最短的話，那就先走這條路。這樣在weighted graph中，Dijkstra也可以找到某個點到每個點的最短路徑。</p><p>所以Dijkstra只是比較的過程變得比較細緻一點。</p><p>我想了一個諧音梗：Dijkstra，待刻始抓。選比較早能夠走到的點。</p><p>如果graph有cycle呢？只要weight是positive，Dijkstra對不同路線的比較就會是對的。</p><h3>建構式解題：</h3><p>倘若是一隻貓要買一種魚，而每家店又都有賣的話，那就是用Dijkstra找出最短路經，沒毛病。</p><p>如果是有些店會沒有賣的話，終止條件就不是終點而已，還要看是否沿路上有買到魚。</p><p>如果魚的種類不只一種，終止條件則是買齊所有種類。每條路的結果，會是路上所有店的魚種類的聯集（土豪式買法，「給我全包了吧。」）。</p><p>如果貓不只一隻，可以分工合作的話，那就是拿前面一題的結果去作組合，選較慢到達時間最少、兩隻貓聯集又能買齊魚的路線組合。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32de4ee4e83e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[No Senior Left Behind]]></title>
            <link>https://medium.com/@yumingliao/no-parent-left-behind-a-review-for-breadth-first-tree-relabeling-or-the-power-of-laziness-693ae037a61a?source=rss-e8cfe0443ff7------2</link>
            <guid isPermaLink="false">https://medium.com/p/693ae037a61a</guid>
            <category><![CDATA[laziness]]></category>
            <category><![CDATA[haskell]]></category>
            <category><![CDATA[breadth-first-search]]></category>
            <dc:creator><![CDATA[Yu-Ming Liao]]></dc:creator>
            <pubDate>Thu, 08 Aug 2024 06:00:54 GMT</pubDate>
            <atom:updated>2025-09-12T07:08:06.074Z</atom:updated>
            <content:encoded><![CDATA[<h4>a review of Breadth-First Tree Relabeling, or the Power of Laziness</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*waGTyHabr8Y0KHcYZJ4zqA.jpeg" /></figure><p>I was caught up in <a href="https://discourse.haskell.org/t/breadth-first-tree-relabelling-or-the-power-of-lazyness/9961">Breadth First Tree Relabeling, or the Power of Laziness</a>. And it took me some time to understand how laziness works like a charm here.</p><p>So the question is:<strong> traverse a tree data structure in a breadth-first manner, and replace all the values with their “index”</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/787069dea789fe7b7b2a827e96c6708d/href">https://medium.com/media/787069dea789fe7b7b2a827e96c6708d/href</a></iframe><p>The depth first `index` is just in order of depth first traversal of the same tree. So the writing of dfLabel is intuitive to me.</p><p>But how could bfLabel traverse breadth-first while its code structure seems depth-first?</p><p>case 1: Leaf _</p><p>go (0:ns) (Leaf a) = (Leaf 0, 1:ns)</p><p>Well, ,future) will be 1:ns , and (0:future) will be (0:1:1:...) .</p><p>case 2: Node (Leaf _) _ (Leaf _)</p><pre>,future)<br>= (0+1): ns&#39;&#39;<br>= 1 : (headPlusOne ns&#39;)<br>= 1 : (headPlusOne (headPlusOne ns)<br>= 1 : (headPlusOne (headPlusOne future)<br>= 1 : 3 : ns&#39;&#39;<br>= 1 : 3 : 3 : ...</pre><p>So basically, the list future lists the correct start `indices` of every level in a complete binary tree, with repeating the index of the level after the bottom-most level.</p><p>In that way, whenever go ns l , you pass right index for next-level children from the “future”.</p><p>Look how dfLabel and bfLabel are similar in code structure.</p><p>SobfLabel is saying to me, like, “Well, I intend to evaluate and build the tree depth-first, but when I realize that I need correct, breadth-first index, I ask ns, aka future. And I will evaluate just enough to know the index. Hence, in the power of laziness, I am breadth-first, eventually.”</p><h3>Conclusion:</h3><ol><li>The argument 0:future is a “not-known-yet” list that represents each indices starting from each level. ex. 0:1:3: ...</li><li>In the let clause, one of the function result, ,future) and the argument 0:future tie the knot so that you can “ask for future” inside a function.</li><li>An index will be evaluated only when the evaluation hits that level of tree. Hence the laziness. It’s just like take n [0..] . When an index is asked, all the (+1) needed is evaluated. That’s how you turn a depth-first Haskell code into a breadth-first one. Hence the title: No Senior Left Behind.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=693ae037a61a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>