Sharing the RX Goodness

I’ve decided to separate my RX musings into its own dedicated area.

http://rxgoodness.wordpress.com/

Enjoy!

Advertisements

Facebook Photo Sync

The social media giant’s latest offering of background photo uploads had me slightly worried.

I’m working on an app to do effectively just that. However, on further reading, I’m not so worried.

Firstly, the official app uploads every single photo from your phone automatically. Don’t worry about picking out the keepers or my mobile data limit – just upload everything.

This is not the workflow that I’m targeting. It shouldn’t really be anyone’s work flow!

My app assumes the following workflow.

First, you to choose the photos you want to upload. This is usually the post processed version – even the best shots usually benefit from a little crop, some tone curves applied, a touch of sharpening.

Next, I would like to arrange the photos for my album, add descriptions and tag people – all offline.

Finally, I’d like it to upload in the background at some point. Perhaps I’ll only want to transfer over wifi.

Thankfully, Facebook must cater to the lowest common denominator. Whilst its brute force method may suit some, I hope to find a more select audience. At least before they improve the app too much!

Infinite Sequences

Before I work through my solution to the Fibonacci problem, lets have a look at infinite sequences.

Infinite Sequences

As discussed previously, a sequence can be thought of as a line of sweets all laid out in front of us, and we take one at a time until either they run out or we’ve had enough.

Another way to think of it is like a sweet machine. Every time we crank the handle, the next sweet comes out.

The consuming code that evaluates a sequence is cranking the handle.

The sequence code is the machine and using the yield return keyword is the machine dispensing a sweet.

The yield break keyword ends the sequence – no more sweets. A sequence can also end when the code execution reaches the end of the method.

I think it’s best demonstrated by examples.

Let’s write a sequence that returns the numbers from 1 to 10.


        IEnumerable<int> GetOneToTen()
        {
                // Loop from 1 to 10
                for(int i=1; i<=10; i++)
                {
                        // yield the value of i to the sequence
                        yield return i;
                }

                // i gets to 11, for loop ends, end of the method is reached, sequence ends.
        }

It’s easy to see how you could modify this code to produce the numbers from 1 to 100 or to any other number.

What if we wanted the sequence to just keep going and going?


        IEnumerable<int> GetNaturalNumbers()
        {
                // Loop forever-ish (until i overflows the int)
                var i = 1;
                while(true)
                {
                        // yield the value of i to the sequence
                        yield return i++;
                }

                // end of the method is never reached, so the sequence never ends.
        }

Now I found it difficult to understand why the thread doesn’t get stuck in here.

Cleverly, the compiler rewrites this code into a state machine that produces the sequence.

The code is more for our benefit to understanding the sequence. It’s clear to see what numbers will be produced and also clear that it never ends. So I guess the code does its job, whilst the compiler does its.

So if you now wanted a sequence of the numbers from 1 to 50, you could just Take the first 50 numbers from this sequence.


        var oneToFifty = GetNaturalNumbers().Take(50);

We can also create more infinite sequences by using and filtering the GetNaturalNumbers. For example, here’s a sequence of even numbers.


         var evenNumbers= GetNaturalNumbers().Where(n => n % 2 == 0);

You should have enough information to produce an infinite-ish sequence of Fibonacci numbers now?

Sequences – Fibonacci Interview Question

During a recent job interview I attended, I was asked to produce the code to solve the following – using only pen and paper.

It’s fair to say that without intellisense and the compiler, I struggled somewhat. It’s also fair to say that until I was asked to do this, I hadn’t realised how lacking my understanding of Sequences was!

Since then, I’ve become far more familiar with Sequences and this problem is a good challenge.

The question was:

  1. Write the code that produces a Sequence that yields the Fibonacci numbers, starting 0, 1, 1, 2, 3, 5 and so on.
  2. Using that sequence, produce another sequence that yields G(n) = F(n+1)/F(n) where F(n) is the nth Fibonacci number.
  3. G(n) tends towards the Golden Ratio. Write a method that takes a double that signifies the accuracy E to which we want to
    compute the Golden Ratio e.g. E = 0.00001, By using the absolute value of G(n)-G(n-1) < E, return the Golden Ratio to the
    specified accuracy.

…using only pen and paper for goodness sake.

Before I post my solution, why not have a go yourself?

Bonus points if you really can write it using only pen and paper.

Ask for Permission

Android users that have installed applications from the Android Play Store Market whatever it’s called will be familiar with the list of permissions that an app requests –  you know, that page that you don’t read with the install button on it?

Well, from an app development point of view, it’s important to remember to add the permissions we need for an app to function.

Note that I say it’s important to “remember” and not important to “know”. I did know you had to check the relevant permission box in the Mono for Android project properties. I just forgot to! 🙂