https://www.gravatar.com/avatar/306d401b2a8c0efe0996fec60e0c90aa?s=240&d=mp

My coding journey

Disclaimer: it's just for fun

Define a reusable and lazy pipeline

The problem

I have the idea of a pipeline: it is defined once, it can be reused and when called, it changes its state but does not evaluate eagerly, because it could be “fed” from an infinite source of data.

For example:

  • all integers which are multiple of 13 and has in its binary representation more ones than zeroes
  • indices of bytes read from /dev/random which are ascii characters

Perhaps there are two separate issues I’m thinking about here:

Some exercism and kata

  1. Exercism Completed Phase 1 week 3 exercises on exercism:
    1. hello-world
    2. two-fer
    3. raindrops
    4. bob
    5. hamming
    6. pangram
    7. isogram
    8. difference-of-squares

./exer.jpg

  1. Played with function composition in scala - implemented simple banner generator (see header of this and previous entry).

Also, learned about external process calling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  object Colors {

    abstract class ColorSource {
      //generate colors from string once
      lazy val into: List[Color] = {
        val hx = hexes()
        println(hx)
        hx.map(Color.decode)
      }
      def hexes(): List[String]
      def random: Color = into(r.nextInt(into.length))
    }

    class Pastel extends ColorSource {
      def hexes(): List[String] = {
        import scala.sys.process._
        val result = (Seq("pastel", "random","-s",  "vivid") #| Seq("pastel", "format", "hex")).!!
        result.split("\n").toList
      }
    }

    class Gradient(from: String="ffffcc", to: String="fd8d3c", n: Int = 5) extends ColorSource {
      def hexes(): List[String] = {
        import scala.sys.process._
        val cmd = Seq("pastel", "gradient", "--colorspace", "HSL", from, to, "-n", n.toString) #| Seq("pastel", "format", "hex")
        println(cmd)
        val result = cmd.!!
        result.split("\n").toList
      }
    }

    class Fixed(cols: List[String]) extends ColorSource {
      def hexes(): List[String] = cols              
    }

    def pastel: ColorSource = new Pastel
    def gradient(from: String, to: String, n: Int) = new Gradient(from, to, n)
    def fixed(cols: List[String]) = new Fixed(cols)
	
	/** ... */
}
  1. Setting up rustlings failed. I probably messed up the configuration (or had old version of rustlings tool). Worked after removal of whole repo (minus execrises done so far),tool reinstall (cargo install rustlings), rustlings init and copying over my exercises.

./rustlings.jpg

Discover duplicated files

Problem

I have a directory where I store files: articles, images, notes - in a bit unordered and random fashion: I copy them from other devices, generate some files requried for teaching sessions or create subdirectrories for experimentation.

As a result, this directory is messy and contains a lot of duplicated files. Some of them are quite big (think: .epub or .pdf, not linux distro .iso).

So, I thought, this is a good candiate for a practical problem solving exercise joined with language learning practice for Rust and Scala.

So what Options do I have?

Option types

Option type documentation:

Same names, a bit different API

Scala

In Scala the variants are called: None and Some which are final case subclasses of Option. I’ve noticed the subclassing has the nice property of helping in IDE to discover the variants (which is also the case, for example, for Try subclasses.

Rust

In Rust, the variants are also conveniently named Some and None.

Scala AND Rust ?

Science based programming language learning

Exploring two prorgamming languages at the same time seems like a bad idea, doesn’t it? Well, good learning sources say that the best approaches to effective, expert learnings are:

  • interleaving (study both languages concurrently β€” never sequentially)
  • spaced repetition
  • deliberate practice at the edge of your comfort zone
  • retrieval practice (close the docs)
  • and the explanation effect

Looks like a very good idea to me.

Back to Scala

Discovery

Leaving a familiar ground of Java, Maven and Spring Boot, and switching back to another JVM language - Scala - shouldn’t be a Big Deal, right?

Especially when once upon a time I did complete two Cursera courses about Scala and Functional Programming, which - as far as I remember - were really eye-opening, rewarding and funny.

Let’s dive into remote memories…

When I was learning Clojure, I had one issue (no, it was not about parenthesis; those serve me well) and it was: learning Emacs. An adventure of its own. Also, some language concepts needed to settle in my head. My fingers needed to learn paredit shortcuts. And I had a lot of fun.

Code in Rust

Anniversary reminder

Let’s write some Rust! πŸ¦€ Today I had an idea to write a short program to remind me about important anniversaries.

Create

Let’s create a new rust project named diary:

1
cargo new diary

Cargo creates this simple directory structure:

1
2
3
4
.
β”œβ”€β”€ Cargo.toml
└── src
    └── main.rs

Open main.rs

1
2
3
fn main() {
    println!("Let's eat some fruit!");
}
  • run it with cargo run

Optimize a bit

  • check its size with du -sh target/debug/diary (3.8M)
  • build with --release flag, i.e. cargo build --release
  • check the size now: du -sh target/release/diary (428K)

Configure cargo

…to automaticlally strip symbols from binary (strip=true) and enable link time optimization which removes dead code and may therefore reduce binary size (lto=true)

New me, new tools, new hosting

Big changes are hapenning in my life:

  • I started using Emacs
  • I switched my hosting provider (smarthost.pl -> small.pl) and switched my domain provider (smarthost.pl -> hostido.pl)
  • I switched from 8k to 10k steps a day

Decision to start using Emacs was primarily made because I was unable to use neovim at work:

  • github.com is blocked and we cannot clone repositories from it
  • so automatic plugin updates are not possibe (manual installation is not an option)

I also found a few refreshing resources: