Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
View GitHub Profile
@ts95
ts95 / Fibonacci Haskell
Last active December 27, 2015 02:59
Fibonacci Sequence, Haskell.
import Data.List
fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
main :: IO ()
main = putStrLn $ intercalate ", " $ map show $ map fib [0..10]
@ts95
ts95 / Model.swift
Created June 23, 2017 22:42
Model protocol for Firebase/Database
import Foundation
import FirebaseDatabase
enum ModelError: Error {
case invalidProperties
}
protocol Model {
init?(snapshot: FIRDataSnapshot)
@ts95
ts95 / Book.swift
Created June 23, 2017 22:49
An example of a book model that implements the Model protocol
import Foundation
import FirebaseDatabase
struct Book {
var id: String?
var isbn: String
var title: String
var authors: String
var year: String
var country: String
@ts95
ts95 / StoreNewBook.swift
Last active June 23, 2017 23:24
Example of storing a new book model instance in the Firebase database
let db = FIRDatabase.database()
let booksRef = db.reference(withPath: Endpoint.books.path)
var newBook = Book(
id: nil,
isbn: "95-0443-843-0",
title: "Do Androids Dream of Electric Sheep?",
authors: "Philip K. Dick",
year: "1968",
country: "United States",
@ts95
ts95 / FetchBook.swift
Created June 23, 2017 23:25
Example of fetching a book by its id and printing out the title of it
let db = FIRDatabase.database()
let bookRef = db.reference(withPath: Endpoint.book("-KnEUaDbm6-snUq_Ojzo").path)
bookRef.observeSingleEvent(of: .value, with: { snapshot in
guard let book = Book(snapshot: snapshot) else { return }
print(book.title)
})
import UIKit
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(describing: self)
let book = FirestoreBook(
isbn: "95-0443-843-0",
title: "Do Androids Dream of Electric Sheep?",
authors: ["Philip K. Dick"],
year: 1968,
country: "United States",
createdAt: Date(),
format: .ebook,
award: nil)
let book = FirestoreBook(
documentID: nil,
isbn: "95-0443-843-0",
title: "Do Androids Dream of Electric Sheep?")
let firestore = Firestore.firestore()
let ref = firestore.collection("books").document()
ref.setModel(book)
let firestore = Firestore.firestore()
let ref = firestore.document("books/95-0443-843-0")
ref.getModel(FirestoreBook.self) { book, error in
guard let book = book else { return }
print("The book \(book.title) was published in \(book.year).")
}
struct FirestoreBook {
let documentID: String!
var isbn: String
var title: String
}
extension FirestoreBook: FirestoreModel {
init?(modelData: FirestoreModelData) {
try? self.init(