If this sentence is true, then Santa Claus is real.
This statement seems innocuous enough, but it exposes a fatal flaw in some logical systems. If it is possible for the above sentence to exist as a statement, then it follows that Santa Claus is real…
I have seen several articles recently that take the position that switch
statements (or even if
statements) are an anti-pattern in regards to best practice object-orientated programming. In a way, the sentiment is usually fine, but I don’t think that these articles really tell the full story. …
If you think this article is too long, you can skip to Should I refactor my code? below
In software engineering, “clean code” is a sought after ideal. Code that is “clean” is easy to read…
Recently I made a game and entered it into the Github Game Off 2019. The premise of the game is that there are a series of cows that run around following rules. The aim in the earlier levels is to get all of the cows into a green area. The aim in later levels is to use the cows to create and program a basic computer.
With these kinds of game jams, there is a restrictive time limit involved. In this case, 30 days. On day 0, you receive a theme in which to base your game upon: the theme…
Box
with Rc
, and replacing mutable dereferences with Rc::make_mut
.Data structures are persistent when they can keep track of all of their old states. Every time a modification is made, a new structure is created instead of modifying the original data. …
First of all a disclaimer. I’m going to use the terms such as “interface” and “encapsulation”, and I’m going to define them a little bit…
Safe…
fn
) and a context.Fn
.FnMut
.FnOnce
.Unlike some other languages, Rust is explicit about our use of the self
parameter. We have to specify self
to be the first parameter of a function signature when we are implementing a struct:
struct MyStruct {
text: &'static str,
number: u32,
}impl MyStruct { fn new (text…
Summary:
Using “open” recursion:
Consider a recursive function. The most basic example is the Fibonacci sequence. Here is a naive implementation:
fn fib_naive(arg: u32) -> u32 {
match arg {
0 => 0,
1 => 1,
n => fib_naive(n - 1) + fib_naive(n - 2),
}
}fn main() {
assert_eq!(fib_naive(40), 102334155);
}
Calculating fib_naive(40)
on my computer takes almost a second. If we look at this function, we can reason…
I’ll use Rust as the language to describe the concepts behind this post, but try to make it accessible to those using other languages.
We can start with a simple object, such as a circle. A circle is completely defined by it’s radius. As long as our radius is positive, we have a valid circle:
#[derive(Debug)]
struct Circle {
radius: f64
}impl Circle {
fn new_option (radius: f64) -> Option<Circle> {
if radius > 0.0 …
The stories I write are a part of a learning journey through life, logic and programming. Share this journey with me.