Skip to content

Instantly share code, notes, and snippets.

View JonNorman's full-sized avatar

Jon Norman JonNorman

View GitHub Profile
@JonNorman
JonNorman / grab_ad_units.scala
Created February 8, 2017 17:34
Paginated DFP Ad Unit PQL request
import com.google.api.ads.dfp.axis.factory.DfpServices
import com.google.api.ads.dfp.axis.utils.v201608.{DateTimes, StatementBuilder}
import com.google.api.ads.dfp.axis.v201608.{AdUnitPage, InventoryServiceInterface}
def run(dfpSession: DfpSession): Unit = {
// Construct DFP Services
val dfpServices = new DfpServices
val adUnitService: InventoryServiceInterface = dfpServices.get(dfpSession, classOf[InventoryServiceInterface])
@tailrec
@JonNorman
JonNorman / find_local_only_branches.py
Created January 13, 2018 14:35
Find local Git branches that haven't got remotes
import os
import subprocess
# only look in home and subdirectories
home_dir = os.path.expanduser('~')
git_repos = [root for root, dirs, _ in os.walk(home_dir) if '.git' in dirs ]
row_format = '{:30s} {:40s} ({})'
print(row_format.format('Repository', 'Branch', 'Path'))
@JonNorman
JonNorman / week2-recap.scala
Created October 15, 2018 14:03
A quick overview of the concepts covered last week
/******************************************
* Declaring variables and using types
*******************************************/
/*
We can declare variables with val - once the value has been set, it cannot be changed
*/
val myName = "Jon"
myName = "Jonathan" // not valid!
@JonNorman
JonNorman / week3-recap.scala
Created October 23, 2018 10:59
A quick overview of the concepts covered in week 3 of scala-school
/**************************************************************************************
* Control structures
*
* Control structures are bits of code that determine "where" your program goes - it
* isn't that useful having a program that does the same thing every time you run it,
* you want to be able to specify conditions and checks and for it to act according to
* those results
**************************************************************************************/
/*
@JonNorman
JonNorman / week4-recap_pt1.scala
Created October 23, 2018 10:59
A quick overview of the concepts covered in week 4 of scala-school (part 1)
/**********************************************************************************************
* Case Classes
*
* Creating our own types (through classes and, later, traits) allows us to better model the
* domain we are working in. Create higher level concepts and manipulate them more intuitively
**********************************************************************************************/
/*
Let's say we want to calculate the distance between two cartesian points on a regular X-Y graph.
We could do it like so (using pythagoras's theorum of length = (x^2 + y^2) ^ 1/2):
@JonNorman
JonNorman / week4-recap-pt2.scala
Created October 23, 2018 11:00
A quick overview of the concepts covered in week 4 of scala-school (part 2)
/***********************************************************************************************
* Traits
*
* In the same way that case classes allow us to express our ideas at a higher, more conceptual
* level, traits can be used to link case classes together that share some commonality.
**********************************************************************************************/
/*
Sticking with our geometry domain, let's say that we want to create an app
that will do some basic geometry for us, we can start with some squares.
@JonNorman
JonNorman / week4-exercises-pt1.md
Last active October 24, 2018 12:18
Exercises for Scala School, starting to try and tie together everything we have covered so far.

Week 4 Exercises

These exercises are intended to bring together a lot of what we have covered so far in our lessons. Make sure you ask for help on the scala-school channel if you get stuck!

There will be a part-2 of these exercises that will look at expanding our domain to include Books and Music, which will explore traits and how we can use them.

Remember that there are the recap worksheets that you can use as references and cheat sheets whilst attempting the exercises.

Scenario

Flying in the face of successful subscription-based services, let's start mapping out the domain of an entertainment website where people can rent items of interest, we can call it Guflix and we can start with Films.

@JonNorman
JonNorman / week5-class-exercises.md
Last active October 29, 2018 13:23
Exercises to go through in class, introducing the `Option` type.

Exercises:

  1. Write a function called parsePhoneNumber that takes as an argument phoneNumber: String and returns a Option[String] representing a phone number. Why might we want to use String here and not Int?

  2. Write a better max function that returns an Option[Int] given an argument of type List[Int]. How would you write this for a list of String? How about a List[Boolean]? Would the implementations differ in any meaninful way?

  3. Define a case class User that represent a user in our imaginary app. A User should have an id a name and an optional email.

  4. Create a list of Users - call it users - that contains 2 Users. Let's pretend that this is some database of users for our app.

/***
* Handling errors and working with success! aka: Option
*
* Every language needs to deal with things not going to plan.
* Many languages - including Java - do this via exceptions. Even
* if your function says it returns an Int, it might just explode.
*
* Some languages - including Java ( ͡ಠ ʖ̯ ͡ಠ) - use nulls. Even
* if your function says it returns an Int, it might return nothing.