Skip to content

Instantly share code, notes, and snippets.

View jdmoreira's full-sized avatar

João D. Moreira jdmoreira

  • Yubico
  • Stockholm, Sweden
View GitHub Profile
@jdmoreira
jdmoreira / Conclusion.md
Last active December 18, 2019 19:37
Conclusion

Here we are at last. So when I make the claim that "the ViewModels don't know about the ViewControllers" what I mean is:

1. Materializes are pure functions

Materializes are "pure" functions that implicitly receive one argument - self: T. T conforms to Presentable and is always one of our ViewModels. Our materializes are never mutable nor should they be! They simple receive a viewModel and return a tuple of (Matter, Result) which are usually (UIViewController, ObservableConvertible) but there are exceptions.

2. Materializes are just namespaced in the ViewModel

The only relation between materialize and the ViewModel is that materialize receives the ViewModel as an argument and is namespaced in type(of: ViewModel)

@jdmoreira
jdmoreira / Pure_Functions.md
Last active December 18, 2019 19:10
Pure Functions

Pure Functions

This is what wikipedia has to say about function purity

In computer programming, a pure function is a function that has the following properties:

Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).

Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams).

In some languages it's possible to annotate any function with a pure keyword and the compiler will give an error if the function is not pure. Swift does not have this annotation but it has an annotation that basically guarantees that the function is impure - mutable. All other functions could be impure or pure - no guarantees!

@jdmoreira
jdmoreira / Namespaces_Part_2.md
Last active December 18, 2019 18:26
Namespaces Part 2

Namespaces Part 2

In Part 1 all the functions were namespaced on types (Swift.Type). They were Type methods. Even the Int.init(in: ) is for all effects a Type method, albeit special.

In the The Swift Programming language this is what's said about methods.

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C.

For completion this is said about instances in general

@jdmoreira
jdmoreira / Namespaces_Part_1.md
Last active December 18, 2019 20:06
Namespaces Part 1

Namespaces Part 1

Let's say we want a global function but we don't want it to live in the global namespace. Swift lacks a namespace keyword but we can accomplish the same thing using an enum with no cases.

public enum Random {
   static func integer(in range: Range<Int>) -> Int {
      ...
   }