The solution is totally obvious. The code we write has a form defined by it's input->output mapping over all possible inputs.
Write a function and you immediately define this input output mapping. All you have to do at this point is search through all previously written functions to find the one that most resembles the one you are trying to write.
Trying to write a nested for loop to pull out pixel values from a jpeg? Start writing it, shit you get it wrong - but the input output mapping is close enough to the cluster of previously written solutions that the correct code pops onto the screen.
Structure aware code search across all open source code would make a lot of the pain just go away.