How to fix Google play services has stopped problem

I recently flashed my tablet with a newer Android version that wasn’t available over the air. I kept getting an error “Google play services has stopped” every few seconds. I tried clearing cache and data, but it wouldn’t go away.

What actually did work for me is going to Settings -> Apps -> Show system apps -> Google play services and giving it all the permissions. Apparently it was freaking out because it was lacking some permission.

FizzBuzz in Rust

The FizzBuzz Test is sometimes given at interviews to filter out candidates who don’t program. The test is as follows:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

If you wanted to show that you can program, you’d write something like

fn main () {
    for n in 1..101 {
        if n % 15 == 0 {
            println!("FizzBuzz");
        } else if n % 3 == 0 {
            println!("Fizz");
        } else if n % 5 == 0 {
            println!("Buzz");
        } else {
            println!("{}", n);
        }
    }
}

This would be sufficient to get to the actual part of the interview where the interviewer can assume you can write a for loop. I’ll admit that half of the time I mess up if I’m not paying attention. This time I forgot to write the % 15 case first so I had to go back and fix it.

But let’s say you had to write this at your job for some reason. This solution is fine, until your boss comes back and says “OK, do the same thing, but now can you make it write Bazz instead of Buzz?”

It would be tempting to just make the substitution in place. But you have new knowledge that requirements may change frequently, so you decide to refactor the program before you change it.

fn main() {
    let first = "Fizz";
    let second = "Buzz";

    for n in 1..101 {
        println!("{}", fizzbuzz(first, second, n));
    }
    
}

fn fizzbuzz(first: &str, second: &str, n: i32) -> String {
    if n % 3 == 0 {
        first.to_string()
    } else if n % 5 == 0 {
        second.to_string()
    } else if n % 15 == 0 {
        first.to_string() + second
    } else {
        n.to_string()
    }
}

You can now freely change the string that’s being concatenated, it doesn’t matter if it’s Buzz or Bazz. You can even write a nice test for it:

#[cfg(test)]
mod tests {
    use super::fizzbuzz;

    #[test]
    fn it_works() {
        assert!(fizzbuzz("Fizz", "Buzz", 3) == "Fizz".to_string());
        assert!(fizzbuzz("Fizz", "Buzz", 5) == "Buzz".to_string());
        assert!(fizzbuzz("Fizz", "Buzz", 15) == "FizzBuzz".to_string());
        assert!(fizzbuzz("Fizz", "Buzz", 4) == 4.to_string());
    }
}

But now our boss comes back and says “What I meant is that you’re supposed to do Fizz, Buzz, Bazz for 3, 5, 7”. Suddenly, our solution is bad. Before we had four cases where we duplicate some code in one of them. Now we have nine cases, and if our boss comes back to ask for more features, possibly sixteen. We try this solution:

fn main() {
    let first = "Fizz";
    let second = "Buzz";

    for n in 1..101 {
        println!("{}", fizzbuzz(&[(first, 3), (second, 5)], n));
    }
}

pub fn fizzbuzz(tuples: &[(&str, i32)], n: i32) -> String {
    let mut output = String::from("");
    
    for &tuple in tuples {
        let (s, factor) = tuple;
        
        if n % factor == 0 {
             output = output + s;
        }
    }
    
    if output == "" {
        n.to_string()
    } else {
        output
    }
}

It’s a clever hack, exploiting the fact that the empty string is an identity element in string concatenation. Unfortunately, it doesn’t pass our new test suite:

#[cfg(test)]
mod tests {
    use super::fizzbuzz;

    #[test]
    fn it_works() {
        assert!(fizzbuzz(&[("Fizz", 3), ("Buzz", 5)], 3) == "Fizz".to_string());
        assert!(fizzbuzz(&[("Fizz", 3), ("Buzz", 5)], 5) == "Buzz".to_string());
        assert!(fizzbuzz(&[("Fizz", 3), ("Buzz", 5)], 15) == "FizzBuzz".to_string());
        assert!(fizzbuzz(&[("Fizz", 3), ("Buzz", 5)], 4) == 4.to_string());
        assert!(fizzbuzz(&[("", 3), ("Buzz", 5)], 3) == "".to_string());
    }
}

Because we used arbitrary strings, our function doesn’t work for empty strings! I don’t know about you, but I feel uneasy writing a function that fails on some valid inputs. Even if we think (read: hope) that those inputs won’t be fed into our function, we can do better. Rust helpfully has the Option type to actually mark absence of something. We can mark the absence of a match as None, and the presence of a match as Some(String).

pub fn fizzbuzz(tuples: &[(&str, i32)], n: i32) -> String {
    let default = || n.to_string();
    accumulate(tuples, n).unwrap_or_else(default)
}

Now we’ll write the accumulate function. There is actually a library called itertools that has a function called fold1 that does a fold starting with None and returns Some if there are things to fold.

fn accumulate(tuples: &[(&str, i32)], n: i32) -> Option<String> {
    tuples.iter()
        .filter(|&x|n % x.1 == 0)
        .map(first)
        .cloned()
        .map(<&str>::into)
        .fold1(String::concat)
}

fn first<A, B>(&(ref first, _): &(A, B)) -> &A {
    first
}

The function first is not really necessary, but it feels clearer to me. We also have to unfortunately write a trait for things that can be concatenated.

pub trait Concatenable {
    fn concat(self, other: Self) -> Self;
}

impl Concatenable for String {
    fn concat(mut self, other: String) -> String {
        self.push_str(&*other);
        self
    }
}

But we’ll ignore that and hope Add is implemented on String + String in the future so we can easily call fold1 on it. But notice that our FizzBuzz program no longer has a single if statement or even a match. This is because the logic of excluding non-factors is inside the filter and the logic for writing the original number is in unwrap_or_else. The benefit of this is there is only one path through the program. The whole program is now in a declarative style.

The final result (with some dependencies inlined) is here: https://is.gd/QVal2B

I also wrote the same thing using Cow<'a, str> and each condition being a closure in my repository:

https://bitbucket.org/iopq/fizzbuzz-in-rust

 

How to make foobar2000 look like Google Play Music

1pygp57

I liked the Google Play Music UI because I could remember albums by their covers. So I figured out how to style foobar2000 to do the same thing.

I needed the EsPlaylist component from http://foo2k.chottu.net/

After installing it, I had to add it to the UI in Quick Edit mode. After that I could right click and select Browse -> Browse Library. That lets me view my library using the EsPlaylist component. I also used the Biography View component from the same website to download the cover art for the albums.

 

 

How to update the PATH variable on windows without logging off or rebooting

When you go to Computer->Properties->Advanced System Settings->Environment Variables you can change the PATH variable. But the general advice is that you have to log off or reboot to actually see the change.

I actually found a work-around:

  1. Change the PATH variable.
  2. Add a new variable, call it something random. Maybe something like CHANGE_TO_UPDATE and put a random value like x in it.
  3. Remember to restart cmd.exe or whatever program that needs to see the new path variable.

Voila! It should now work. When I didn’t do step 2, it did not work for me no matter which other solutions I tried.

Implementing FizzBuzz with monoids in Rust

I was very impressed with the Haskell solution to FizzBuzz. The blog is now down, but here’s the Internet Archive of the page:

https://web.archive.org/web/20130511210903/http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html

I asked /r/rust about implementing monoids in the Rust programming language and got help with porting the Haskell version.

Here’s the repository:

https://bitbucket.org/iopq/fizzbuzz-in-rust

It’s mostly written by Reddit and IRC with me piecing it together until it type checked and did the same thing the Haskell program did.

Brag: it’s actually possible to write this in Rust because Monoids don’t require higher kinds.
Beat: I have to nest .op(.op()) to do several ops because of C-style syntax
Variance: I spent lots of time trying to figure out why [_, n, ..rest] didn’t type check, until [_, n, ..] just worked

Why Opera switching to Blink (WebKit) was the blunder of a century

Despite the fact that the switch to the Blink rendering engine was supposed to be an “under the hood” change, it’s clear that this switch is in fact a total rewrite. None of the Opera exclusive features have made it into Opera Next. It doesn’t matter how good you remake the browser, the current users want their old work flow. It will probably take several years to partially re-implement the features that the old version had. In the end, those features will not be delivered hundred percent even years down the line. Current 12.x users will keep using 12.x for as long as they can, which means that eventually it’s going to become a legacy browser that will no longer be supported by major JS frameworks. The newest Opera versions will still be largely a UI for Chromium.

I believe this fits under what Joel Spolsky wrote in Things You Should Never Do, Part I. Opera is throwing away the entire Presto engine. That means they won’t be able to differentiate themselves from Chrome for several years, which means that Opera users will have the option between a soon-to-be-legacy browser and basically Chromium with a different UI.

What is the benefit of using Opera 15 over just plain Chromium? Because they removed some features like bookmarks people are supposed to change how they work to accommodate the opinion of some guy who thought this was a good idea? I don’t even care if the new feature is a hundred times better than bookmarks, people know how to use bookmarks so they want their bookmarks. The reason why people tend not to switch to a new browser is because they are so used to the UI and features their browser has that it makes it difficult. Opera Link keeps your bookmarks synced, but there is no way to transfer them to Opera 15. This means that there is no continuity to the new browser, so Opera effectively killed Opera 12.x and started from scratch. They will have to fight with EVERY other browser maker for their desktop market share because it’s just as hard to switch to Opera 15 as it is to switch to Firefox. You have none of your search engines, bookmarks, customized UI/menus, shortcuts, custom mouse gestures, etc.

The correct thing for Opera Software to do was to open source Presto so that the development could keep up with Blink and Gecko. What Opera Software has done instead is to become irrelevant.

Show more/show less functionality with jQuery

I looked at the code at jQuery More/Less Text and I think this is a pretty good solution. Depending on the height of the box, the contents either hide the overflow and show a “Show More” button or they show the overflow and show a “Show Less” button.

The post by Stu Green also shows how to hide this button when contents fit into the box. However, I need to explain how to do this yourself. Your HTML should be:

<div class="showmore">
    <div class="moreblock">
    	<p>The Content</p>
    </div>
</div>

Your JS function should be:

function showMore() {
    // The height of the content block when it's not expanded
    var adjustheight = 55;
    var moreText = "Read more...";
    var lessText = "Read less...";

    $(".showmore > .moreblock").each(function(){
        if ($(this).height() > adjustheight){
            $(this).css('height', adjustheight).css('overflow', 'hidden');
            $(this).parent(".showmore").append('<p class="continued">&hellip;</p><a href="#" class="adjust"></a>');
            $(this).parent(".showmore").find("a.adjust").text(moreText);
            $(this).parent(".showmore").find(".adjust").toggle(function() {
                $(this).parents("div:first").find(".moreblock").css('height', 'auto').css('overflow', 'visible');
                $(this).parents("div:first").find("p.continued").css('display', 'none');
                $(this).text(lessText);
            }, function() {
                $(this).parents("div:first").find(".moreblock").css('height', adjustheight).css('overflow', 'hidden');
                $(this).parents("div:first").find("p.continued").css('display', 'block');
                $(this).text(moreText);
            });
        }
    });
}

then you just call showMore() somewhere. Usually it will be when the document is ready, but sometimes you’ll do this in the callback after dynamically loading content. I hope this helps.

https://www.cityblis.com/ uses a similar approach ported to the newest jQuery (toggle has been deprecated)