Skip to content

Instantly share code, notes, and snippets.

@ndchorley
ndchorley / examples.scala
Last active Jun 27, 2018
Scala examples
View examples.scala
// Some small Scala examples.
// Scala is statically typed and types are inferred (meaning you usually don't have to provide
// them), but I've chosen to include them because it helps illuminate some of the points.
val values: List[Int] = List(1, 2, 3) // the type of values is List[Int], i.e. a list containing integers.
// Let's talk about the map method. It is used to transform the items in the list in some way, returning
// a new list. The way we specify how to transform the values is to pass a function to map that describes
// the transformation we want. map then applies this function to every item in the list.
View classes_example.py
# Classes let you model the things in your application's domain and how
# they interact. In programming terms, they let you define new types - in
# the same way that int, float, bool, list, str, etc. are types and have things
# you can do with them (e.g. with ints or floats, you can add them, for lists
# you can append to them or ask for the item at a given position, ...), you
# can define your own. Clearly, then, you're already used to using classes!
# Let's say we need to work with books and in our model, a book just has
# a title and an author. A class definition to model a book might look
# like this: