diff options
| author | Paul Buetow <paul@buetow.org> | 2023-05-28 00:01:26 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-05-28 00:01:26 +0300 |
| commit | 1be7a8716d8913a40ab4720c4eab78c185aa172a (patch) | |
| tree | 44466c775631e0335b9ca6d0e17f2a2b6a520727 | |
| parent | dbe4c437f05686a0fd1a500159bc62380e8d010e (diff) | |
Update content for gemtext
| -rw-r--r-- | gemfeed/atom.xml | 8151 | ||||
| -rw-r--r-- | gemfeed/atom.xml.tmp | 764 | ||||
| -rw-r--r-- | index.gmi | 2 | ||||
| -rw-r--r-- | notes/a-monks-guide-to-happiness.gmi | 2 | ||||
| -rw-r--r-- | notes/mental-combat.gmi | 2 | ||||
| -rw-r--r-- | notes/soft-skills.gmi | 2 | ||||
| -rw-r--r-- | notes/the-stoic-challenge.gmi | 2 | ||||
| -rw-r--r-- | resources.gmi | 4 | ||||
| -rw-r--r-- | uptime-stats.gmi | 2 |
9 files changed, 390 insertions, 8541 deletions
diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml deleted file mode 100644 index 94351bb5..00000000 --- a/gemfeed/atom.xml +++ /dev/null @@ -1,8151 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<feed xmlns="http://www.w3.org/2005/Atom"> - <updated>2023-05-06T23:58:11+03:00</updated> - <title>foo.zone feed</title> - <subtitle>To be in the .zone!</subtitle> - <link href="gemini://foo.zone/gemfeed/atom.xml" rel="self" /> - <link href="gemini://foo.zone/" /> - <id>gemini://foo.zone/</id> - <entry> - <title>'The Obstacle is the Way' book notes</title> - <link href="gemini://foo.zone/gemfeed/2023-05-06-the-obstacle-is-the-way-book-notes.gmi" /> - <id>gemini://foo.zone/gemfeed/2023-05-06-the-obstacle-is-the-way-book-notes.gmi</id> - <updated>2023-05-06T17:23:16+03:00</updated> - <author> - <name>Paul Buetow</name> - <email>hi@paul.cyou</email> - </author> - <summary>These are my personal takeaways after reading 'The Obstacle Is the Way' by Ryan Holiday. This is mainly for my own use, but you might find it helpful too.</summary> - <content type="xhtml"> - <div xmlns="http://www.w3.org/1999/xhtml"> - <h1 style='display: inline'>"The Obstacle is the Way" book notes</h1><br /> -<br /> -<span class='quote'>Published at 2023-05-06T17:23:16+03:00</span><br /> -<br /> -<span>These are my personal takeaways after reading "The Obstacle Is the Way" by Ryan Holiday. This is mainly for my own use, but you might find it helpful too.</span><br /> -<br /> -<br /> -<pre> - ,.......... .........., - ,..,' '.' ',.., - ,' ,' : ', ', - ,' ,' : ', ', - ,' ,' : ', ', - ,' ,'............., : ,.............', ', -,' '............ '.' ............' ', - '''''''''''''''''';''';'''''''''''''''''' - ''' -</pre> -<br /> -<span>"The obstacle is the way" is a powerful statement that encapsulates the wisdom of turning challenges into opportunities for growth and success. We will explore using obstacles as fuel, transforming weaknesses into strengths, and adopting a mindset that allows us to be creative and persistent in the face of adversity.</span><br /> -<br /> -<h2 style='display: inline'>Reframe your perspective</h2><br /> -<br /> -<span>The obstacle in your path can become your path to success. Instead of being paralyzed by challenges, see them as opportunities to learn and grow. Remember, the things that hurt us often instruct us. </span><br /> -<br /> -<span>We spend a lot of time trying to get things perfect and look at the rules, but what matters is that it works; it doesn't need to be after the book. Focus on results rather than on beautiful methods. In Jujitsu, it does matter that you bring your opponent down, but not how. There are many ways from point A to point B; it doesn't need to be a straight line. So many try to find the best solution but need to catch up on what is in Infront of them. Think progress and not perfection.</span><br /> -<br /> -<span>Don't always try to use the front door; a backdoor could open. It's nonsense. Don't fight the judo master with judo. Non-action can be action, exposing the weaknesses of others.</span><br /> -<br /> -<br /> -<h2 style='display: inline'>Embrace rationality</h2><br /> -<br /> -<span>It is a superpower to see things rationally when others are fearful. Focus on the reality of the situation without letting emotions, such as anger, cloud your judgment. This ability will enable you to make better decisions in adversity. Ability to see things what they really are. E.g. wine is old fermented grapes, or other people behaving like animals during a fight. Show the middle finger if someone persists on the stupid rules occasionally.</span><br /> -<br /> -<h2 style='display: inline'>Control your response</h2><br /> -<br /> -<span>You can choose how you respond to obstacles. Focus on what you can control, and don't let yourself feel harmed by external circumstances. Remember, you decide how things affect you; nobody else does. Choose to feel good in response to any situation. Embrace the challenges and obstacles that come your way, as they are opportunities for growth and learning.</span><br /> -<br /> -<h2 style='display: inline'>Practice emotional and physical resilience</h2><br /> -<br /> -<span>Martial artists know the importance of developing physical and emotional strength. Cultivate the art of not panicking; it will help you avoid making mistakes during high-pressure situations.</span><br /> -<br /> -<span>Focus on what you can control. Don't choose to feel harmed, and then you won't be harmed. I decide things that affect me; nobody else does. E.g., in prison, your mind stays your own. Don't ignore fear but explain it away, have a different view.</span><br /> -<br /> -<h2 style='display: inline'>Persistence and patience</h2><br /> -<br /> -<span>Practice persistence and patience in your pursuits. Focus on the process rather than the prize and take one step at a time. Remember, the journey is about finishing tasks, projects, or workouts to the best of your ability. Never be in a hurry and never be desperate. There is no reason to be rushed; there are all in the long haul. Follow the process and not the price. Take it one step at a time. The process is about finishing (workout, task, project, etc.).</span><br /> -<br /> -<h2 style='display: inline'>Embrace failure</h2><br /> -<br /> -<span>Failure is a natural part of life and can make us stronger. Treat defeat as a stepping stone to success and education. What is defeat? The first step to education. Failure makes you stronger. If we do our best, we can be proud of it, regardless of the result. Do your job, but do it right. Only an asshole thinks he is too good at the things he does. Also, asking for forgiveness is easier than asking for permission. </span><br /> -<br /> -<h2 style='display: inline'>Be adaptable</h2><br /> -<br /> -<span>There are many ways to achieve your goals; sometimes, unconventional methods are necessary. Feel free to break the rules or go off the beaten path if it will lead to better results. Transform weaknesses into strengths. We have a choice of how to respond to things. It's not about being positive but to be creative. Aim high, but stuff will happen; E.g., surprises will always happen.</span><br /> -<br /> -<h2 style='display: inline'>Embrace non-action</h2><br /> -<br /> -<span>We constantly push to the next thing. Sometimes the best course of action is standing still or even going backwards. Obstacles might resolve by themselves. Or going sideways. Sometimes, the best action is to stand still, go sideways, or even go backwards. Obstacles may resolve themselves or present new opportunities if you're patient and observant. People always want your input before you have all the facts. They want you to play after their rules. The question is, do you let them? The English call it the cool head. Being in control of Stress; requires practice. Appear, the absence of fear (Greek). When all others do it one way, it does not mean it is the correct or best practice.</span><br /> -<br /> -<h2 style='display: inline'>Leverage crisis</h2><br /> -<br /> -<span>In times of crisis, seize the chance to do things never done before. Great people use negative situations to their advantage and become the most effective in challenging circumstances.</span><br /> -<br /> -<span>The art of not panicking; otherwise, you will make mistakes. When overs are shocked, you know which way to take due to your thinking of the problem at Hand. A crisis gives you a chance to do things which never done before. Ordinary people shy from negative situations; great people use these for their benefit and are the most effective. The obstacle is not just turned upside down but used as a catapult.</span><br /> -<br /> -<span>Be prepared for nothing to work. Problems are an opportunity to do your best, not to do miracles. Always manage your expectations. It will suck, but it will be ok. Be prepared to begin from the beginning. Be cheerful and eagerly work on the next obstacle. Each time you become better. Life is not a sprint but a marathon. After each obstacle lies another obstacle, there won't be anything without obstacles. Passing one means you are ready for the next.</span><br /> -<br /> -<h2 style='display: inline'>Build your inner citadel</h2><br /> -<br /> -<span>Develop your inner strength during good times so you can rely on it in bad times. Always prepare for adversity and face it with calmness and resilience. Be humble enough that things which happen will happen. Build your inner citadel. In good times strengthen it. In bad times rely on it.</span><br /> -<br /> -<span>We should always prepare for things to get tough. Your house burns down: no worries, we eliminated much rubbish. Imagine what can go wrong before things go wrong. We are prepared for adversity; it's other people who aren't. Phil Jackson's hip problem example. To receive unexpected benefits, you must first accept the unexpected obstacles. Meditate on death. It's a universal obstacle. Use it as a reminder to do your best.</span><br /> -<br /> -<h2 style='display: inline'>Love everything that happens</h2><br /> -<br /> -<span>Turn an obstacle the other way around for your benefit. Use it at fuel. It's simple but challenging. Most are paralyzed instead. The obstacle in the path becomes the path. Obstacles are neither good nor bad. The things which hurt, instruct.</span><br /> -<br /> -<span>Should I hate people who hate me? That's their problem and not mine. Be always calm and relaxed during the fight. The story of the battle is the story of the smile. Cheerfulness in all situations, especially the bad ones. Love for everything that happens; if it happens, it was meant to happen. We can choose how we react to things, so why not choose to feel good? I love everything that happens. You must never lower yourself to the person you don't like.</span><br /> -<br /> -<h2 style='display: inline'>Conclusion</h2><br /> -<br /> -<span>Life is a marathon, not a sprint. Each obstacle we overcome prepares us for the next one. Remember, the obstacle is not just a barrier to be turned upside down; it can also be used as a catapult to propel us forward. By embracing challenges and using them as opportunities for growth, we become stronger, more adaptable, and, ultimately, more successful.</span><br /> -<br /> -<span>Other book notes of mine are:</span><br /> -<br /> -<a class='textlink' href='./2023-05-06-the-obstacle-is-the-way-book-notes.html'>2023-05-06 "The Obstacle is the Way" book notes (You are currently reading this)</a><br /> -<a class='textlink' href='./2023-04-01-never-split-the-difference-book-notes.html'>2023-04-01 "Never split the difference" book notes</a><br /> -<a class='textlink' href='./2023-03-16-the-pragmatic-programmer-book-notes.html'>2023-03-16 "The Pragmatic Programmer" book notes</a><br /> -<br /> -<span>E-Mail your comments to hi@paul.cyou :-)</span><br /> -<br /> -<a class='textlink' href='../resources.html'>More books and other resources I found useful.</a><br /> -<a class='textlink' href='../'>Back to the main site</a><br /> - </div> - </content> - </entry> - <entry> - <title>Unveiling `guprecords.raku`: Global Uptime Records with Raku</title> - <link href="gemini://foo.zone/gemfeed/2023-05-01-unveiling-guprecords:-uptime-records-with-raku.gmi" /> - <id>gemini://foo.zone/gemfeed/2023-05-01-unveiling-guprecords:-uptime-records-with-raku.gmi</id> - <updated>2023-04-30T13:10:26+03:00</updated> - <author> - <name>Paul Buetow</name> - <email>hi@paul.cyou</email> - </author> - <summary>For fun, I am tracking the uptime of various personal machines (servers, laptops, workstations...). I have been doing this for over ten years now, so I have a lot of statistics collected.</summary> - <content type="xhtml"> - <div xmlns="http://www.w3.org/1999/xhtml"> - <h1 style='display: inline'>Unveiling <span class='inlinecode'>guprecords.raku</span>: Global Uptime Records with Raku</h1><br /> -<br /> -<span class='quote'>Published at 2023-04-30T13:10:26+03:00</span><br /> -<br /> -<pre> -+-----+-----------------+-----------------------------+ -| Pos | Host | Lifespan | -+-----+-----------------+-----------------------------+ -| 1. | dionysus | 8 years, 6 months, 17 days | -| 2. | uranus | 7 years, 2 months, 16 days | -| 3. | alphacentauri | 6 years, 9 months, 13 days | -| 4. | *vulcan | 4 years, 5 months, 6 days | -| 5. | sun | 3 years, 10 months, 2 days | -| 6. | uugrn | 3 years, 5 months, 5 days | -| 7. | deltavega | 3 years, 1 months, 21 days | -| 8. | pluto | 2 years, 10 months, 30 days | -| 9. | tauceti | 2 years, 3 months, 22 days | -| 10. | callisto | 2 years, 3 months, 13 days | -+-----+-----------------+-----------------------------+ -</pre> -<br /> -<h1 style='display: inline'>Introduction</h1><br /> -<br /> -<span>For fun, I am tracking the uptime of various personal machines (servers, laptops, workstations...). I have been doing this for over ten years now, so I have a lot of statistics collected.</span><br /> -<br /> -<span>As a result of this, I am introducing <span class='inlinecode'>guprecords.raku</span>, a handy Raku script that helps me combine uptime statistics from multiple servers into one comprehensive report. In this blog post, I'll explore what Guprecords is and some examples of its application. I will also add some notes on Raku.</span><br /> -<br /> -<span>Guprecords, or global uptime records, is a Raku script designed to generate a consolidated uptime report from multiple hosts:</span><br /> -<br /> -<a class='textlink' href='https://codeberg.org/snonux/guprecords'>https://codeberg.org/snonux/guprecords</a><br /> -<a class='textlink' href='https://raku.org'>The Raku Programming Language</a><br /> -<br /> -<span>A previous version of Guprecords was actually written in Perl, the older and more established language from which Raku was developed. One of the primary motivations for rewriting Guprecords in Raku was to learn the language and explore its features. Raku is a more modern and powerful language compared to Perl, and working on a real-world project like Guprecords provided a practical and engaging way to learn the language.</span><br /> -<br /> -<span>Over the last years, I have been reading the following books and resources about Raku:</span><br /> -<br /> -<ul> -<li>Raku Guide (at raku.guide)</li> -<li>Think Perl 6</li> -<li>Raku Fundamentals</li> -<li>Raku Recipes</li> -</ul><br /> -<span>And I have been following the Raku newsletter, and sometimes I have been lurking around in the IRC channels, too. Watching Raku coding challenges on YouTube was pretty fun, too. However, nothing beats actually using Raku to learn the language. After reading all of these resources, I may have a good idea about the features and paradigms, but I am by far not an expert.</span><br /> -<br /> -<h2 style='display: inline'>How Guprecords works</h2><br /> -<br /> -<span>Guprecords works in three stages:</span><br /> -<br /> -<ul> -<li>1. Generating uptime statistics using <span class='inlinecode'>uptimed</span>: First, I need to install and run <span class='inlinecode'>uptimed</span> on each host to generate uptime statistics. This tool is available for most common Linux and *BSD distributions and macOS via Homebrew.</li> -<li>2. Collecting uptime records to a central location: The next step involves collecting the raw uptime statistics files generated by <span class='inlinecode'>uptimed</span> on each host. It's a good idea to store all record files in a central git repository. The records file contains information about the total uptime since boot, boot time, and the operating system and kernel version. Guprecords itself does not do the collection part, but have a look at the <span class='inlinecode'>README.md</span> in the git repository for some guidance.</li> -<li>3. Generating global uptime stats: Finally, run the <span class='inlinecode'>guprecords.raku</span> script with the appropriate flags to create a global uptime report. For example, I can use the following command:</li> -</ul><br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre>$ raku guprecords<font color="#990000">.</font>raku --stats<font color="#990000">=</font><font color="#009900">dir</font><font color="#990000">=</font><font color="#009900">$HOME</font>/git/uprecords/stats --all -</pre> -<br /> -<span>This command will generate a comprehensive uptime report from the collected statistics, making it easy to review and enjoy the data.</span><br /> -<br /> -<span>Guprecords supports the following features:</span><br /> -<br /> -<ul> -<li>Supports multiple categories: Host, Kernel, KernelMajor, and KernelName</li> -<li>Supports multiple metrics: Boots, Uptime, Score, Downtime, and Lifespan</li> -<li>Output formats available: Plaintext, Markdown, and Gemtext</li> -<li>Provides top entries based on the specified limit</li> -</ul><br /> -<h2 style='display: inline'>Example</h2><br /> -<br /> -<span>You have already seen an example at the very top of this post, where the hosts were grouped by their total lifespans (uptime+downtime). Here's an example of what the global uptime report (grouped by total host uptimes) might look like:</span><br /> -<br /> -<pre> -Top 20 Uptime's by Host - -+-----+-----------------+-----------------------------+ -| Pos | Host | Uptime | -+-----+-----------------+-----------------------------+ -| 1. | *vulcan | 4 years, 5 months, 6 days | -| 2. | uranus | 3 years, 11 months, 21 days | -| 3. | sun | 3 years, 9 months, 26 days | -| 4. | uugrn | 3 years, 5 months, 5 days | -| 5. | deltavega | 3 years, 1 months, 21 days | -| 6. | pluto | 2 years, 10 months, 29 days | -| 7. | tauceti | 2 years, 3 months, 19 days | -| 8. | tauceti-f | 1 years, 9 months, 18 days | -| 9. | *ultramega15289 | 1 years, 8 months, 17 days | -| 10. | *earth | 1 years, 5 months, 22 days | -| 11. | *blowfish | 1 years, 4 months, 20 days | -| 12. | ultramega8477 | 1 years, 3 months, 25 days | -| 13. | host0 | 1 years, 3 months, 9 days | -| 14. | tauceti-e | 1 years, 2 months, 20 days | -| 15. | makemake | 1 years, 1 months, 6 days | -| 16. | callisto | 0 years, 10 months, 31 days | -| 17. | alphacentauri | 0 years, 10 months, 28 days | -| 18. | london | 0 years, 9 months, 16 days | -| 19. | twofish | 0 years, 8 months, 31 days | -| 20. | *fishfinger | 0 years, 8 months, 17 days | -+-----+-----------------+-----------------------------+ -</pre> -<br /> -<span>This table ranks the top 20 hosts based on their total uptime, with the host having the highest uptime at the top. The hosts marked with <span class='inlinecode'>*</span> are still active, means stats were collected within the last couple of months. </span><br /> -<br /> -<span>My up to date stats can be seen here:</span><br /> -<br /> -<a class='textlink' href='../uptime-stats.html'>My machine uptime stats</a><br /> -<br /> -<span>Just recently, I decommissioned <span class='inlinecode'>vulcan</span> (the number one stop from above), which used to be my CentOS 7 (initially CentOS 6) VM hosting my personal NextCloud and Wallabag (which I modernised just recently with a brand new shiny Rocky Linux 9 VM). This was the last <span class='inlinecode'>uptimed</span> output before shutting it down (it always makes me feel sentimental decommissioning one of my machines <span class='inlinecode'>:'-(</span>):</span><br /> -<br /> -<pre> - # Uptime | System Boot up -----------------------------+--------------------------------------------------- - 1 545 days, 17:58:15 | Linux 3.10.0-1160.15.2.e Sun Jul 25 19:32:25 2021 - 2 279 days, 10:12:14 | Linux 3.10.0-957.21.3.el Sun Jun 30 12:43:41 2019 - 3 161 days, 06:08:43 | Linux 3.10.0-1160.15.2.e Sun Feb 14 11:05:38 2021 - 4 107 days, 01:26:35 | Linux 3.10.0-957.1.3.el7 Thu Dec 20 09:29:13 2018 - 5 96 days, 21:13:49 | Linux 3.10.0-1127.13.1.e Sat Jul 25 17:56:22 2020 --> 6 89 days, 23:05:32 | Linux 3.10.0-1160.81.1.e Sun Jan 22 12:39:36 2023 - 7 63 days, 18:30:45 | Linux 3.10.0-957.10.1.el Sat Apr 27 18:12:43 2019 - 8 63 days, 06:53:33 | Linux 3.10.0-1127.8.2.el Sat May 23 10:41:08 2020 - 9 48 days, 11:44:49 | Linux 3.10.0-1062.18.1.e Sat Apr 4 22:56:07 2020 - 10 42 days, 08:00:13 | Linux 3.10.0-1127.19.1.e Sat Nov 7 11:47:33 2020 - 11 36 days, 22:57:19 | Linux 3.10.0-1160.6.1.el Sat Dec 19 19:47:57 2020 - 12 21 days, 06:16:28 | Linux 3.10.0-957.10.1.el Sat Apr 6 11:56:01 2019 - 13 12 days, 20:11:53 | Linux 3.10.0-1160.11.1.e Mon Jan 25 18:45:27 2021 - 14 7 days, 21:29:18 | Linux 3.10.0-1127.13.1.e Fri Oct 30 14:18:04 2020 - 15 6 days, 20:07:18 | Linux 3.10.0-1160.15.2.e Sun Feb 7 14:57:35 2021 - 16 1 day , 21:46:41 | Linux 3.10.0-957.1.3.el7 Tue Dec 18 11:42:19 2018 - 17 0 days, 01:25:57 | Linux 3.10.0-957.1.3.el7 Tue Dec 18 10:16:08 2018 - 18 0 days, 00:42:34 | Linux 3.10.0-1160.15.2.e Sun Jul 25 18:49:38 2021 - 19 0 days, 00:08:32 | Linux 3.10.0-1160.81.1.e Sun Jan 22 12:30:52 2023 -----------------------------+--------------------------------------------------- -1up in 6 days, 22:08:18 | at Sat Apr 29 10:53:25 2023 -no1 in 455 days, 18:52:44 | at Sun Jul 21 07:37:51 2024 - up 1586 days, 00:20:28 | since Tue Dec 18 10:16:08 2018 - down 0 days, 01:08:32 | since Tue Dec 18 10:16:08 2018 - %up 99.997 | since Tue Dec 18 10:16:08 2018 -</pre> -<br /> -<h2 style='display: inline'>Conclusion</h2><br /> -<br /> -<span>Guprecords is a small, yet powerful tool for analyzing uptime statistics. While developing Guprecords, I have come to truly appreciate and love Raku's expressiveness. The language is designed to be both powerful and flexible, allowing developers to express their intentions and logic more clearly and concisely.</span><br /> -<br /> -<span>Raku's expressive syntax, support for multiple programming paradigms, and unique features, such as grammars and lazy evaluation, make it a joy to work with. </span><br /> -<br /> -<span>Working on Guprecords in Raku has been an enjoyable experience, and I've found that Raku's expressiveness has significantly contributed to the overall quality and effectiveness of the script. The language's ability to elegantly express complex logic and data manipulation tasks makes it an excellent choice for developing tools like these, where expressiveness and productiveness are of the utmost importance.</span><br /> -<br /> -<span>So far, I have only scratched the surface of what Raku can do. I hope to find more time to become a regular Rakoon (a Raku Programmer). I have many Ideas for other small tools like Guprecords, but the challenge is finding the time. I'd love to explore Raku Grammars and also I would love to explore writing concurrent code in Raku (I also love Go (Golang), btw!). Ideas for future Raku personal projects include:</span><br /> -<br /> -<ul> -<li>A log file analyzer, for generating anonymized <span class='inlinecode'>foo.zone</span> visitor stats for both, the Web and Gemini.</li> -<li>A social media sharing scheduler a la <span class='inlinecode'>buffer.com</span>. I am using Buffer at the moment to share posts on Mastadon, Twitter, Telegram and LinkedIn, but it is proprietary and also it's not really reliable.</li> -<li>Rewrite the static photo album generator of <span class='inlinecode'>irregular.ninja</span> in Raku (from Bash).</li> -</ul><br /> -<span>Other related posts are:</span><br /> -<br /> -<a class='textlink' href='./2023-05-01-unveiling-guprecords:-uptime-records-with-raku.html'>2023-05-01 Unveiling <span class='inlinecode'>guprecords.raku</span>: Global Uptime Records with Raku (You are currently reading this)</a><br /> -<a class='textlink' href='./2022-06-15-sweating-the-small-stuff.html'>2022-06-15 Sweating the small stuff - Tiny projects of mine</a><br /> -<a class='textlink' href='./2022-05-27-perl-is-still-a-great-choice.html'>2022-05-27 Perl is still a great choice</a><br /> -<a class='textlink' href='./2011-05-07-perl-daemon-service-framework.html'>2011-05-07 Perl Daemon (Service Framework)</a><br /> -<a class='textlink' href='./2008-06-26-perl-poetry.html'>2008-06-26 Perl Poetry</a><br /> -<br /> -<span>E-Mail your comments to hi@paul.cyou :-)</span><br /> -<br /> -<a class='textlink' href='../'>Back to the main site</a><br /> - </div> - </content> - </entry> - <entry> - <title>Algorithms and Data Structures in Go - Part 1</title> - <link href="gemini://foo.zone/gemfeed/2023-04-09-algorithms-and-data-structures-in-golang-part-1.gmi" /> - <id>gemini://foo.zone/gemfeed/2023-04-09-algorithms-and-data-structures-in-golang-part-1.gmi</id> - <updated>2023-04-09T22:31:42+03:00</updated> - <author> - <name>Paul Buetow</name> - <email>hi@paul.cyou</email> - </author> - <summary>This is the first blog post about my Algorithms and Data Structures in Go series. I am not a Software Developer in my day job. In my current role, programming and scripting skills are desirable but not mandatory. I have been learning about Data Structures and Algorithms many years ago at University. I thought it would be fun to revisit/refresh my knowledge here and implement many of the algorithms in Go.</summary> - <content type="xhtml"> - <div xmlns="http://www.w3.org/1999/xhtml"> - <h1 style='display: inline'>Algorithms and Data Structures in Go - Part 1</h1><br /> -<br /> -<span class='quote'>Published at 2023-04-09T22:31:42+03:00</span><br /> -<br /> -<pre> - ,_---~~~~~----._ - _,,_,*^____ _____``*g*\"*, - / __/ /' ^. / \ ^@q f -[ @f | @)) | | @)) l 0 _/ - \`/ \~____ / __ \_____/ \ - | _l__l_ I - } [______] I - ] | | | | - ] ~ ~ | - | | - | | -</pre> -<br /> -<span>This is the first blog post about my Algorithms and Data Structures in Go series. I am not a Software Developer in my day job. In my current role, programming and scripting skills are desirable but not mandatory. I have been learning about Data Structures and Algorithms many years ago at University. I thought it would be fun to revisit/refresh my knowledge here and implement many of the algorithms in Go.</span><br /> -<br /> -<a class='textlink' href='./2023-04-09-algorithms-and-data-structures-in-golang-part-1.html'>2023-04-09 Algorithms and Data Structures in Go - Part 1 (You are currently reading this)</a><br /> -<br /> -<span>This post is about setting up some basic data structures and methods for this blog series. I promise, everything will be easy to follow in this post. It will become more interesting later in this series.</span><br /> -<br /> -<h2 style='display: inline'>Type constraints</h2><br /> -<br /> -<span>First, the package <span class='inlinecode'>ds</span> (data structures) defines the <span class='inlinecode'>types.go</span>. All examples will either operate on the <span class='inlinecode'>Integer</span> or <span class='inlinecode'>Number</span> type:</span><br /> -<br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><b><font color="#0000FF">package</font></b> ds - -<b><font color="#0000FF">import</font></b> <font color="#990000">(</font> - <font color="#FF0000">"golang.org/x/exp/constraints"</font> -<font color="#990000">)</font> - -<b><font color="#0000FF">type</font></b> Integer <b><font color="#0000FF">interface</font></b> <font color="#FF0000">{</font> - constraints<font color="#990000">.</font>Integer -<font color="#FF0000">}</font> - -<b><font color="#0000FF">type</font></b> Number <b><font color="#0000FF">interface</font></b> <font color="#FF0000">{</font> - constraints<font color="#990000">.</font>Integer <font color="#990000">|</font> constraints<font color="#990000">.</font>Float -<font color="#FF0000">}</font> - -</pre> -<br /> -<h2 style='display: inline'>ArrayList</h2><br /> -<br /> -<span>Next comes the <span class='inlinecode'>arraylist.go</span>, which defines the underlying data structure all the algorithms of this series will use. <span class='inlinecode'>ArrayList</span> is just a type alias of a Go array (or slice) with custom methods on it:</span><br /> -<br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><b><font color="#0000FF">package</font></b> ds - -<b><font color="#0000FF">import</font></b> <font color="#990000">(</font> - <font color="#FF0000">"fmt"</font> - <font color="#FF0000">"math/rand"</font> - <font color="#FF0000">"strings"</font> -<font color="#990000">)</font> - -<b><font color="#0000FF">type</font></b> ArrayList<font color="#990000">[</font>V Number<font color="#990000">]</font> <font color="#990000">[]</font>V - -<b><font color="#0000FF">func</font></b> NewArrayList<font color="#990000">[</font>V Number<font color="#990000">](</font>l int<font color="#990000">)</font> ArrayList<font color="#990000">[</font>V<font color="#990000">]</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">return</font></b> <b><font color="#000000">make</font></b><font color="#990000">(</font>ArrayList<font color="#990000">[</font>V<font color="#990000">],</font> l<font color="#990000">)</font> -<font color="#FF0000">}</font> -</pre> -<br /> -<span>As you can see, the code uses Go generics, which I refactored recently. Besides the default constructor (which only returns an empty <span class='inlinecode'>ArrayList</span> with a given capacity), there are also a bunch of special constructors. <span class='inlinecode'>NewRandomArrayList</span> is returning an <span class='inlinecode'>ArrayList</span> with random numbers, <span class='inlinecode'>NewAscendingArrayList</span> and <span class='inlinecode'>NewDescendingArrayList</span> are returning <span class='inlinecode'>ArrayList</span>s in either ascending or descending order. They all will be used later on for testing and benchmarking the algorithms.</span><br /> -<br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><b><font color="#0000FF">func</font></b> NewRandomArrayList<font color="#990000">[</font>V Number<font color="#990000">](</font>l<font color="#990000">,</font> max int<font color="#990000">)</font> ArrayList<font color="#990000">[</font>V<font color="#990000">]</font> <font color="#FF0000">{</font> - a <font color="#990000">:=</font> <b><font color="#000000">make</font></b><font color="#990000">(</font>ArrayList<font color="#990000">[</font>V<font color="#990000">],</font> l<font color="#990000">)</font> - <b><font color="#0000FF">for</font></b> i <font color="#990000">:=</font> <font color="#993399">0</font><font color="#990000">;</font> i <font color="#990000"><</font> l<font color="#990000">;</font> i<font color="#990000">++</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">if</font></b> max <font color="#990000">></font> <font color="#993399">0</font> <font color="#FF0000">{</font> - a<font color="#990000">[</font>i<font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#000000">V</font></b><font color="#990000">(</font>rand<font color="#990000">.</font><b><font color="#000000">Intn</font></b><font color="#990000">(</font>max<font color="#990000">))</font> - <b><font color="#0000FF">continue</font></b> - <font color="#FF0000">}</font> - a<font color="#990000">[</font>i<font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#000000">V</font></b><font color="#990000">(</font>rand<font color="#990000">.</font><b><font color="#000000">Int</font></b><font color="#990000">())</font> - <font color="#FF0000">}</font> - <b><font color="#0000FF">return</font></b> a -<font color="#FF0000">}</font> - -<b><font color="#0000FF">func</font></b> NewAscendingArrayList<font color="#990000">[</font>V Number<font color="#990000">](</font>l int<font color="#990000">)</font> ArrayList<font color="#990000">[</font>V<font color="#990000">]</font> <font color="#FF0000">{</font> - a <font color="#990000">:=</font> <b><font color="#000000">make</font></b><font color="#990000">(</font>ArrayList<font color="#990000">[</font>V<font color="#990000">],</font> l<font color="#990000">)</font> - <b><font color="#0000FF">for</font></b> i <font color="#990000">:=</font> <font color="#993399">0</font><font color="#990000">;</font> i <font color="#990000"><</font> l<font color="#990000">;</font> i<font color="#990000">++</font> <font color="#FF0000">{</font> - a<font color="#990000">[</font>i<font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#000000">V</font></b><font color="#990000">(</font>i<font color="#990000">)</font> - <font color="#FF0000">}</font> - <b><font color="#0000FF">return</font></b> a -<font color="#FF0000">}</font> - -<b><font color="#0000FF">func</font></b> NewDescendingArrayList<font color="#990000">[</font>V Number<font color="#990000">](</font>l int<font color="#990000">)</font> ArrayList<font color="#990000">[</font>V<font color="#990000">]</font> <font color="#FF0000">{</font> - a <font color="#990000">:=</font> <b><font color="#000000">make</font></b><font color="#990000">(</font>ArrayList<font color="#990000">[</font>V<font color="#990000">],</font> l<font color="#990000">)</font> - j <font color="#990000">:=</font> l <font color="#990000">-</font> <font color="#993399">1</font> - <b><font color="#0000FF">for</font></b> i <font color="#990000">:=</font> <font color="#993399">0</font><font color="#990000">;</font> i <font color="#990000"><</font> l<font color="#990000">;</font> i<font color="#990000">++</font> <font color="#FF0000">{</font> - a<font color="#990000">[</font>i<font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#000000">V</font></b><font color="#990000">(</font>j<font color="#990000">)</font> - j<font color="#990000">--</font> - <font color="#FF0000">}</font> - <b><font color="#0000FF">return</font></b> a -<font color="#FF0000">}</font> -</pre> -<br /> -<h2 style='display: inline'>Helper methods</h2><br /> -<br /> -<span>The <span class='inlinecode'>FirstN</span> method only returns the first N elements of the <span class='inlinecode'>ArrayList</span>. This is useful for printing out only parts of the data structure:</span><br /> -<br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><b><font color="#0000FF">func</font></b> <font color="#990000">(</font>a ArrayList<font color="#990000">[</font>V<font color="#990000">])</font> <b><font color="#000000">FirstN</font></b><font color="#990000">(</font>n int<font color="#990000">)</font> <font color="#009900">string</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">var</font></b> sb strings<font color="#990000">.</font>Builder - j <font color="#990000">:=</font> n - - l <font color="#990000">:=</font> <b><font color="#000000">len</font></b><font color="#990000">(</font>a<font color="#990000">)</font> - <b><font color="#0000FF">if</font></b> j <font color="#990000">></font> l <font color="#FF0000">{</font> - j <font color="#990000">=</font> l - <font color="#FF0000">}</font> - - <b><font color="#0000FF">for</font></b> i <font color="#990000">:=</font> <font color="#993399">0</font><font color="#990000">;</font> i <font color="#990000"><</font> j<font color="#990000">;</font> i<font color="#990000">++</font> <font color="#FF0000">{</font> - fmt<font color="#990000">.</font><b><font color="#000000">Fprintf</font></b><font color="#990000">(&</font>sb<font color="#990000">,</font> <font color="#FF0000">"%v "</font><font color="#990000">,</font> a<font color="#990000">[</font>i<font color="#990000">])</font> - <font color="#FF0000">}</font> - - <b><font color="#0000FF">if</font></b> j <font color="#990000"><</font> l <font color="#FF0000">{</font> - fmt<font color="#990000">.</font><b><font color="#000000">Fprintf</font></b><font color="#990000">(&</font>sb<font color="#990000">,</font> <font color="#FF0000">"... "</font><font color="#990000">)</font> - <font color="#FF0000">}</font> - - <b><font color="#0000FF">return</font></b> sb<font color="#990000">.</font><b><font color="#000000">String</font></b><font color="#990000">()</font> -<font color="#FF0000">}</font> -</pre> -<br /> -<span>The <span class='inlinecode'>Sorted</span> method checks whether the <span class='inlinecode'>ArrayList</span> is sorted. This will be used by the unit tests later on:</span><br /> -<br /> -<!-- Generator: GNU source-highlight 3.1.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><b><font color="#0000FF">func</font></b> <font color="#990000">(</font>a ArrayList<font color="#990000">[</font>V<font color="#990000">])</font> <b><font color="#000000">Sorted</font></b><font color="#990000">()</font> <font color="#009900">bool</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">for</font></b> i <font color="#990000">:=</font> <b><font color="#000000">len</font></b><font color="#990000">(</font>a<font color="#990000">)</font> <font color="#990000">-</font> <font color="#993399">1</font><font color="#990000">;</font> i <font color="#990000">></font> <font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">--</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">if</font></b> a<font color="#990000">[</font>i<font color="#990000">]</font> <font color="#990000"><</font> a<font color="#990000">[</font>i<font color="#990000">-</font><font color="#993399">1</font><font color="#990000">]</font> <font color="#FF0000">{</font> - <b><font color="#0000FF">return</font></b> false - <font colo |
