Euler

Today’s solution feels really good because it reads just like the problem description:

const result = takeUntil(primes(), (n) => n > 2_000_000)
	.reduce(sum)
console.log(result)

The challenge for today was writing the takeUntil function:

function takeUntil<T>(g: Generator<T>, condition: (t: T) => boolean): T[] {
    const result: T[] = [];
    for (const r of g) {
        if (condition(r)) {
            break
        }
        result.push(r)
    }
    return result
}

There’s a little bit of fancy work going on with using generics. It turns out that Generator really only needs one type (the expected return). This function will take any generator and the condition will work as long as the type goes from the type of the generator to boolean (so in the case of this problem it goes from number boolean).

One thing I notice is that we use the prime sequence a lot. And running primes up to two million is kind of expensive:

[nicole@hera euler]$ time deno run 010/main.ts 
142913828922
 
real	0m44.060s
user	0m43.925s
sys	0m0.027s

The sequences of primes doesn’t change and since we keep using I wonder about writing a caching layer around the generator that writes out to disk.

Refactoring Day 02

On Day 2 I wrote:

import { sum } from "../util/util.ts"
import { isEven } from "../util/util.ts"
import { fibonachi } from "../util/sequence/fibonachi.ts"
 
function allFibonacciLessThan(num: number): number[] {
    const result: number[] = []
 
    for (const next of fibonachi()) {
        if (next > num) {
            return result
        }
        result.push(next)
    }
 
    // This will never occur
    return []
}
 
 
 
function main() {
    const result = allFibonacciLessThan(4_000_000)
        .filter(isEven)
        .reduce(sum)
 
    console.log(result)
}
 
main()

Which worked perfectly well! But with the function I wrote today, takeUntil, I actually don’t need the allFibonacciLessThan. I can refactor the code to this:

import { sum } from "../util/util.ts"
import { isEven } from "../util/util.ts"
import { fibonachi } from "../util/sequence/fibonachi.ts"
import { takeUntil } from "../util/functional.ts"
 
function main() {
    const a = takeUntil(fibonachi(), n => n > 4_000_000)
        .filter(isEven)
        .reduce(sum)
    console.log(a)
}
 
main()

Notes

Notes becomes private

I made my notes repository private (but not the website). I did this because as I’m getting to know people more and more I want to remember specific details of their projects and those are theirs to share. My notes at notes.nicole.computerare still public. I control which notes get published with a little bit of headmatter in each markdown file. That looks like:

---
publish: true
---

By default my daily notes are public (publish: true) and notes that end up in my notes/ folder are not (publish: false).

Deploying to Netlify

My build.sh looks like this, if you were to host your notes yours will probably be different because you will use a different forked version of quartz:

#!/bin/bash
 
# Clean up state
rm -rf BUILD_TEMP/
rm -rf quartz
rm -rf public
 
# Get a list of all files before we create BUILD_TEMP
ALL_FILES=$(ls)
 
# Now create a directory to store content
mkdir -p BUILD_TEMP/
 
# Move all of the files into a temp working direction
for i in $ALL_FILES; do
    cp -r "$i" BUILD_TEMP/
    echo "$i"
done
 
git clone https://github.com/nicolecomputer/quartz.git
 
cd quartz/
npm i
 
# build the website using the temp folder as the content root
npx quartz build -d ../BUILD_TEMP -o ../public

Netlifylets a build be configured with a file in the root of the project. That file, netlify.toml lets you set build commands. Unfortunately you can only set one command (which is fine for most sites! I just have something complicated). So my file mostly kicks off the script that I wrote before. So my netlify.toml looks like:

[build]
  publish = "public/"
 
  command = "bash .scripts/build.sh"

Social Time

The missing Semester

There’s a group of us at Recurse doing The Missing Semester. Today we met for the first session. A couple of things that I learned:

  • The /sys directory is a facade to system devices. It can be read and written to to affect change on the system from hardware devices. The lecturer demonstrated using a file to read and write the back-light brightness on his computer
  • I’ve never used tee before - it writes to standard out AND a file at the same time. Neat!
  • I’ve used > to direct a steam somewhere else but also there’s < which can redirect standard in to somewhere else. Can you have commands that use both and so point in both directions? Yes. You totally can.
  • The linux equivalent of Mac OS’s open is xdg-open (which I’ll probably alias to open because it’s in my muscle memory)

Here’s the alias in my ~./.bashrc:

alias open="xdg-open"

Exercises

write the “last modified” date output by semester into a file called last-modified.txt

[nicole@hera]$ ./semester | grep last-modified | cut -d ':' -f2-4 | xargs > last-modified.txt

Learned:

  • passing a stream to xargs with no other flags or arguments causes it to be a trim command
  • cut -d ':' causes cut to split on : and -f2-4 says give me fields 2,3,4 this is unfortunately necessary because the last-modified has ”:“‘s in it for the time Thu, 24 Oct 2024 16:34:06 GMT

Get battery level from the /sys directory

[nicole@hera /]$ cat /sys/class/power_supply/BAT0/capacity
74

This is SO cool for quickly getting battery level

Nametag

My slides needed a nametag and so I coded up a quick vue component. This is “baby’s first vue component”. One thing I liked (and maybe React can do this but not in the setups I’ve used in the past) is that I can have a stylesheet right next to my component file and customize the styles in it. This gave me the full power of my IDE to write CSS and, coupled with live reload, I found it SUPER fast to make a quick nametag.

I’d like a version of this nametag to show up on every slide- and it looks like slidev supports that through component. Something to figure out in the future!

End of day

Today was mostly focused on getting my capstone project for Chapter 0 for the nature of code done. Additionally I had some great conversations!

Worked on Today

  • Project Euler. I did a problem AND I did a big refactor of my codebase because I keep using the same functions: https://github.com/nicolecomputer/euler
  • Moved notes.nicole.computer to be hosted on Cloudflare pages (I also tried Netlify) and set the repo to private to keep private notes truly private
  • Wrote simulations for random walker with memory and levy flight
  • Wrote a first Vue component for my presentation on Thursday
  • First meeting of Missing Semester group

Notebook page for the curious with lots more details: https://notes.nicole.computer/daily/2024-11-12

Tomorrow

  • Creative coding time!
  • I have been dying to implement some ideas from Chapter 1, Vectors from Nature of Code