Category Archives: Programming

How to filter in Swift using where

In F#, it”s common to pipeline a chain of commands together, perhaps filtering an input array before mapping with a function:

let xs = [| 1; 2; 3; 4; 5 |]
xs |> Array.filter (fun x -> x > 2) |> Array.map (fun x -> x*x)

Marcin Krzyzanowski wrote a neat post showing how Swift uses where in a number of cases to achieve similar filtering.

The fact is you can use where keyword in a case label of a switch statement, a catch clause of a do statement, or in the case condition of an if, while, guard, for-in statement, or to define type constraints

Leave a comment

Filed under Programming, Swift

ACCU: Introducing Concepts for C++

Overload 129October 2015’s edition of Overload includes an excellent article by Andrew Sutton on Concepts for C++. I first heard about Concepts at the ACCU 2013 conference and it’s exciting to hear that they have reached Technical Specification stage.

There’s no doubt in my mind that concepts will play a huge role for those involved in generic programming. In F#, we already have the ability to state constraints on template arguments e.g. to ensure that ‘T satisfies the comparison constraint:

type Container<'T when 'T : comparison>( items : 'T[] ) =
    // ...

However, I wasn’t expecting the ability to use concepts for placeholders (so that, once you’ve defined a concept, you can use it where you might have used auto to gain the benefits of type deduction without the loss of information).

Leave a comment

Filed under C++, F#, Programming

ACCU: Testing Times

CVu264CoverPete Goodliffe wrote this excellent article (login to ACCU may be required) on how to test software. It’s a decent summary on types of test, who should write them, when to write them, what to test and suitable test frameworks. Anyone looking for a robust argument for why effective testing is vital should refer to this.

Leave a comment

Filed under Programming

Tutorial: Writing your first iOS App

I’ve finished working through this excellent introduction to writing iOS apps. Having written step-by-step guides for developers learning new software myself, it’s clear just how much work has gone into this guide to make it accurate as well as useful and informative.

  • Having never used Xcode before, the guide did a good job of introducing the development environment and showing the workflow and how to manipulate the various views
  • The FoodTracker application itself covers quite a few techniques – it’s a shame that some of the early lessons are later deleted as the app is built, but it’s a trade-off to get something to run quickly
  • The frameworks clearly do much of the heavy-lifting. This reminds me of writing user interfaces in versions of Visual Basic – you didn’t need to know much of VB as a language, it was more about leveraging the tools to get a result
  • Swift is very concise, parameter syntax takes some getting used to (some parameters have both external and internal names, which is esoteric) but even the passing parameters by name is starting to grow on me.
  • It’s very cool that unit testing is integrated in the dev tools – I’m sure I’ll be writing plenty of tests against my data model when I work on this for real.

I have a couple of ideas for apps that I’d like to write for my new Apple Watch, so next I’m looking to try a tutorial specifically for a Watch app.

Leave a comment

Filed under Programming, Swift

MIT develops file system with data guaranteed

Wired reports that MIT have used formal verification to write a file system that to probably will not lose data in the event of a crash. 

Making sure that the file system can recover from a crash at any point is tricky because there are so many different places that you could crash. You literally have to consider every instruction or every disk operation

They used Coq, a proof assistant that enabled them to build the system from the ground up in a logically consistent manner. 

Leave a comment

Filed under Programming

Video: Animations

I was interested to see how animations are executed for Apple Watch apps.  These videos shed some light on it: WWDC 2015 Apple Developer Videos.  I watched:

  • Layout and animation techniques for WatchKit
  • Designing with Animation

It seems that a lot of the animations are ‘tricks’ achieved by tweaking the layout groups (either changing size, alignment or visibility).  Other recommendations include writing skeleton apps to mock animations by switching between images put together in KeyNote – this enables choosing between competing ideas without taking the time to code them all individually.

Leave a comment

Filed under Programming, Video

Programming is…

Bruce Dawson described programming as follows:

When I’m describing what I do for a living to non-programmers I sometimes say that I solve puzzles. I solve fascinating puzzles that are different every day, and there’s no answer key, and very often nobody else knows the solution. Whether it’s figuring out why code is slow, or why it is crashing, or how to make code simpler and better, it’s all puzzles, and I love it.

I think that’s a useful point of view.  Whilst programmers need knowledge of some particular programming language and platform, it’s often the ability to solve puzzles that makes the star programmers stand out from the rest.  An average programmer will often make a meal out of a simple task (whether it’s development or testing) because they can’t see an elegant way to a solution.  The best programmers just get on with delivering quality, testing solutions without making a fuss – and they’re having a great time doing what they love!

Leave a comment

Filed under Musing, Programming

C++: std::make_array (N4315)

I read in May 2015’s CVU that there’s a proposal for std::make_array, a utility method in the same family as std::make_tuple and std::make_pair.

This would be a useful shorthand:

// Existing usage
std::array<double, 3> a = { 1.1, 2.2, 3.3 };

// Proposed usage
auto a = std::make_array( 1.1, 2.2, 3.3 };

The obvious benefit is that you would no longer need to specify the number of elements in the array, making the code more maintainable.

Leave a comment

Filed under C++, C++ Code

How to call methods with byref parameters from F#

When consuming C# libraries in F# projects, out parameters will be interpreted in F# with type byref. For example:

    public class Arithmetic
    {
        public static void sumAndDifference(
            int a, int b, out int sum, out int difference)
        {
            sum = a + b;
            difference = a - b;
        }
    }

Here, sumAndDifference has the following signature when the library is referenced from an F# project:
ByRef
The literal way to call the method is as follows, declaring reference value up-front and de-referencing them for use after the method call:

    let sum : int ref = ref 1
    let difference : int ref = ref 2
    Arithmetic.sumAndDifference(5, 3, sum, difference )
    printfn "Sum = %d, Difference = %d" !sum !difference

But F# also provides a much more concise syntax, allowing the out parameters to appear on the left hand side of an assignment:

    let sum, difference = Arithmetic.sumAndDifference(5, 3)
    printfn "Sum = %d, Difference = %d" sum difference

This syntax is so much cleaner and avoids any mention of references.

1 Comment

Filed under F#, Programming

How to write optional parameters in F# with default values

Question: What’s the difference between

let f ( a : int option, b : string option ) = ...

and

let f ( ?a : int, ?b : string ) = ...

Answer: in the first example, even if you have no values for a and b, you must call

let ret = f ( None, None )

whereas in the second example, you can simplify matters:

let ret = f ()

But what if you want to supply default values for those optional parameters? Here’s an example of a type definition in which the constructor takes optional parameters and assigns them default values if no value has been supplied. defaultArg is an F# operator, not a locally defined function.

type T( ?a : int, ?b : string ) =
  let a = defaultArg a 42
  let b = defaultArg b "Hello, World"

Whilst this syntax seems rather clunky, it is effective. You can read more about it on MSDN (scroll down to the section “Optional Parameters”).

Leave a comment

Filed under Programming