Adding Digitransit API GraphQL to an Swift project

This is a step by step instructions on how to add GraphQL API of Finnish public transportation information to a Swift project in Xcode. This is mostly for my own future reference and collected from more verbose post in Medium Using GraphQL in iOS with Swift - Better Programming - Medium.

(as of writing instructions are valid for Swift 5.1 and Xcode 11)

Step 0: prerequisites:

Install apollo npm toolchain for generating the schema file npm install -g npm

Step 1: Create an Xcode project

Nothing special here.

Step 2: Install Apollo framework dependency for your project

All in detail at Apollo documentation Installation - Client (iOS) - Apollo GraphQL Docs

Step 3: Generate GraphQL schema.json

Run the apollo tool you installed in step 0 to generate the schema.json to be used for generating the datatypes

apollo schema:download –endpoint=api.digitransit.fi/routing/v…

Step 3.1: Add schema.json to XCode project

Nothing weird here

Step 4: Add a Code Generation Step

As documented here: Installation - Client (iOS) - Apollo GraphQL Docs

Step 5: Add .graphql queries to project

Next step is to add .graphql file to your project. Digitransit has an excellent documentation and a sandbox for trying things out

Digitransit documentation for stop arrival information Stops | Digitransit has convenient links to sandbox for testing the API out

Step 6: (Re-)Generate API.swift file when needed

Finally, one runs the build to generate the API.swift file that contains the types of the queries you created. Everytime you add a new type, rerun the build to generate new API.swift

Installation - Client (iOS) - Apollo GraphQL Docs

Joy of Missing Out

After quitting Twitter I have had my share of FOMO moments but all and all it has been more of a case of joy of missing out. JOMO moment of this week: one people in a IG story gave a quick review of new Star Wars movie and I realised I have zero idea what the Internet thinks of the latest Star Wars movie and don’t miss that at all. I can go in with zero expectations and take for whatever it is

Random TV/Streaming tip: Russian national championships for figure skating are streamed free and with English commentary on YouTube. Men’s free program is just about to start

Men. Free skating. 2020 Russian Figure Skating Championships/Мужчины. Произвольная программа - YouTube

Code Kata 4

Just a brief write up on code kata 4 Kata04: Data Munging - CodeKata where the task is to write a short program to pick up the smallest daily variation of temperature using a predetermined weather data table. The second task is to find smallest goal difference in a Premier league table. Third, and the final task is to write general solution to handle both cases.

Writing these solutions I learnt among other things: * How to make an HTTP request using Swift * How to use guard clauses * How to split and join strings to and from arrays * Enums in Swift

My solution is available at swiftkatas/Kata4/Kata4 at master · timokoola/swiftkatas

Sci-fi that turned real

There must be an early 80’s science fiction story where kids of an advanced civilization carry a pocket sized super computer to school and schools need to remind kids to keep their computers (which they call telephones for some weird reason) in a back-bag and on silent.

I would have enjoyed that story as a kid but wouldn’t have believed it will happen on my lifetime

Piece of paper on a Finnish school door that says in Finnish: “keep your phones on silent and in your backbag, please”

So many ideas for a post but I feel like I just want to write code on my spare time. Need to dedicate some time over the holidays just to get the things out on my mind:

Things brewing in my brain: * how present is the sci-fi future of my childhood * 2019 year end review * Code kata 4 and 5 * My first real Swift project! * My plans for 2020 * My book pile for 2020 * Exploration of expanding the “known/unknown” 2x2 to “known/unknown/knowable/unknowable” 4x4

This is quite awesome and fun

文言, or wenyan, is an esoteric programming language that closely follows the grammar and tone of classical Chinese literature. Moreover, the alphabet of wenyan contains only traditional Chinese characters and 「」 quotes, so it is guarenteed to be readable by ancient Chinese people.

wenyan-lang.lingdong.works

Reading Why

is Not an Accordion - daverupert.com. Shocking tidbit:

In a world where 97.8% of sites are inaccessible and sites that do use ARIA are 26.7% more inaccessible,

Source for those numbers WebAIM: The WebAIM Million - An accessibility analysis of the top 1,000,000 home pages

Code(less) kata 3

Today I am working on (non-coding) Code Kata 3 Kata03: How Big? How Fast? - CodeKata. And I finished the first part: “How Big”

roughly how many binary digits (bit) are required for the unsigned representation of:

1,000 - this is easy to do in head (or perhaps more accurately fingers) 11 bits as 2^11 is 1024 1,000,000 - again easy, shift again 11 bits, so roughly 22 bits 1,000,000,000 - hmm, is not quite aliging to power of 2 mess up my calculations? 33 1,000,000,000,000 - with the same caveat, 44 8,000,000,000,000 - multiple by 8 is shifting by 3, so 47

Quick checkup with a Swift playground:

var num = 8000000000000

var presentation = String(num, radix: 2)

presentation.count

And the result is 43. Hmm, my intuition was a bit off. Turns out I was off by one! 2^10 = 1024, not 2 ^ 11, bummer.

My town has approximately 20,000 residences. How much space is required to store the names, addresses, and a phone number for all of these (if we store them as characters)?

Let’s assign 50 characters for a name, 15 for phone number, let’s present address as two address lines, a zip code, city, state and country = 50 + 50 + 10 (zip code) + 20 (city) + state (50) + country (20) = at max 200 characters but let’s assume conservative 100 chars, so 165 chars * 20000 = with some calculator help is 3 Megabytes

I’m storing 1,000,000 integers in a binary tree. Roughly how many nodes and levels can I expect the tree to have? Roughly how much space will it occupy on a 32-bit architecture?

Worst case is a tree where tree only ever goes left or right, then the depth is 1,000,000. Best case is fully packed tree, where all levels are full and balanced. That is 19 full levels and a lot of nodes on level 20. So best case depth is 20.

Tree node is three items, one number and two pointers. With million nodes that is roughly 3 * 4 bytes * 1M, so roughly 12 MB.

Today I am reading: Web Performance Calendar » The unseen performance costs of CSS-in-JS in React apps. I recommend all the other articles in Web Performance Calendar as well

Code Kata 2

I now have implemented two different implementations of Code Kata 2 Kata02: Karate Chop - CodeKata. Learning Swift as I go along. Didn’t realise/remember language has Pythonic array slicing!

My implementations are swiftkatas/chopper.swift at master · timokoola/swiftkatas, still think I could do a much better job if I knew language in idiomatic level. Also: realised binary search is one of those things one thinks they know how to implement but when rubber hits the road it is not at all that easy. In addition I feel that recursive solutions are somehow much more easy to implement for me.

Will try to think three more implementations before moving on to the next one. Can I do this in one big chain of functions? I could also randomly pick the split point but what could be the fifth way?

Should I code a bit for fun or go to bed like a responsible adult I am?

The probably incomplete list of The Incomparable Hugo and Nebula episodes

This is mostly for my own reference. I am building a reading list for year 2020 and started to go through The Incomparable podcast episodes for Hugo and Nebula nominees. Picked up already a couple of books to read

Edit: I will update this blog post as time goes on

2020 Nebula Nominees The Incomparable | Like a Fruit Salad (Episode 516)

2019 Hugo Nominees The Incomparable | 33 Percent Rule (Episode 472)

2018 Hugo Nominees The Incomparable | His Books Are Like Popcorn (Episode 412)

2017 Hugo and Nebula Nominees The Incomparable | I Tried to Quit This Book Twice (Episode 359)

2016 Hugo Nominees The Incomparable | Space Bureaucracy (Episode 256)

2015 Hugo Nominees The Incomparable | Space Bureaucracy (Episode 256)

2014 Hugo Nominees The Incomparable | The Partial Monty (Episode 202)

2014 Hugo Nominees - again? The Incomparable | Golem and Jinni Detective Agency (Episode 196)

2013 Hugo Nominees The Incomparable | Not Technically Incest (Episode 151)

Episode that goes through “The Human Division” by John Scalzi + discusses Hugo Award nominee short stories The Incomparable | Space Fish (Episode 147)

2012 Hugo Nominees The Incomparable | Space Fedora (Episode 96)

2011 Hugo Nominees The Incomparable | Zombie Marmaduke (Hugo Awards 2011) (Episode 51)

Three Hugo winners in a podcast “Three Hugo winners enter our Book Club.” The Incomparable | To Be Continued? (Episode 11)

2019 Nebula Nominees The Incomparable | Team Jerkface (Episode 461)

2018 Nebula Nominees The Incomparable | Robot Lovin’ (Episode 407)

2017 Hugo and Nebula Nominees The Incomparable | I Tried to Quit This Book Twice (Episode 359)

2016 Nebula Nominees The Incomparable | All the Best Words (Episode 301)

2015 Nebula Nominees The Incomparable | Tea, Sandwiches, and Mushrooms (Episode 251)

2014 Nebula Nominees The Incomparable | Golem and Jinni Detective Agency (Episode 196)

And for completeness all the book episodes of The Incomparable listed on their webpage: Archive | The Incomparable (Book Club)

Swift Code Kata

Started my journey in learning Swift coding in earnest today. Eventually I want to be able to prototype quickly with SwiftUI whatever concept I have in hand. But before UI I need to grasp Swift programming language and XCode IDE properly. It means, going to kitchen and chopping onions, er, going to really basics.

So, I read how to create unit tests by Jon Sundell Unit Testing | Swift by Sundell. I have skimmed through the Swift book by Apple and I will start with Code Katas. I am starting out with Kata 2 Kata02: Karate Chop - CodeKata and today I have mostly set up the unit tests described in there. If you want to follow along my code is in here timokoola/swiftkatas: My code katas implemented in Swift.

Black Friday shopping

This year, like many years before, my Black Friday shopping was mostly digital with an exception of Kindle (basic 6”) my gf asked for Christmas and a electric razor dad asked from us for Christmas as well.

My ongoing project to not to be as bad in CSS as I currently am lead me to buy two courses:

Every layout Relearn CSS layout: Every Layout and more challenge driven Front-End Challenges Club. Probably will not have time for this before the new year but they will be there waiting.

Also bought Outlier Mandarin Pronunciation & Accent Masterclass – Outlier Linguistics for my Chinese studies to improve my rather weak pronunciation game

I like this deep learning keyboard from AWS

One of those “I wish I had time” things would be to make myself familiar with generative music.

Amazing morning walk in a forest where winter is doing its best to win over the last bits of autumn (loc: Hämeenlinna, Finland)

Flu shot season

I think this only happens to you if you are very pedantic like me and non-Native English speaker. You say “flu shot” and immediately think how inaccurate that is and then think of the word “influenza vaccination” and realise I can never say that out loud sounding natural.

That said got my flu shot this morning

All the TVs I regularly see have “attach USB memory device to record”-functionality, but I don’t think I can think of anyone actually ever using that feature.

A first for me in learning Chinese: Spotted a typo (most likely pinyin keyboard error) in a Chinese text. Feeling pretty good about myself now

Bought a mouse for Raspberry Pi setup needs and it turns out nowadays nobody sells plain old mice anymore. Every mouse is “a gaming mouse”.

Adding this “Machine learning systems design” by Chip Huyen to my work related reading list: machine-learning-systems-design/consolidated.pdf at master · chiphuyen/machine-learning-systems-design

Q: How do people cultivate and keep track of their ideas? Do you have a note book? Do you have a framework for evaluating ideas? A github repo? Post-its?

Thinking of a way to record and then work with the idea before implementing it. Of course some ideas are small enough for me to just make them real right away but sometimes I might want to kick the tyres first

Mastodon