Skip to content

Instantly share code, notes, and snippets.

View nahive's full-sized avatar

Szymon Maślanka nahive

View GitHub Profile
@nahive
nahive / .gitrc
Created March 29, 2018 12:51
Script for syncing current branch with other branch
#!/bin/bash
function sync {
branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)" # detached HEAD
branch_name=${branch_name##refs/heads/}
echo -e "\033[0;32mSyncing branch $1 into branch $branch_name...\033[0m"
echo -e "$(git checkout $1 && git pull)"
echo -e "$(git checkout $branch_name && git merge $1)"
protocol CollectionViewType {
func register<T: UICollectionViewCell>(_ cellClass: T.Type)
func dequeue<T: UICollectionViewCell>(_ cellClass: T.Type, for indexPath: IndexPath) -> T?
}
extension UICollectionView: CollectionViewType {
func register<T: UICollectionViewCell>(_ cellClass: T.Type) {
register(cellClass, forCellWithReuseIdentifier: String(describing: cellClass))
}
extension Comparable {
/// Returns a Boolean value indicating whether a value is included in a
/// range.
///
/// You can use this pattern matching operator (`~=`) to test whether a value
/// is included in a range. The following example uses the `~=` operator to
/// test whether an integer is included in a range of single-digit numbers.
///
/// let chosenNumber = 3
/// if chosenNumber ~= 0...9 {
/// Returns a Boolean value indicating whether the given element is contained
/// within the range.
///
/// A `ClosedRange` instance contains both its lower and upper bound.
/// `element` is contained in the range if it is between the two bounds or
/// equal to either bound.
///
/// - Parameter element: The element to check for containment.
/// - Returns: `true` if `element` is contained in the range; otherwise,
/// `false`.
/// Returns a Boolean value indicating whether a value is included in a
/// range.
///
/// You can use this pattern matching operator (`~=`) to test whether a value
/// is included in a range. The following example uses the `~=` operator to
/// test whether an integer is included in a range of single-digit numbers.
public static func ~=(pattern: ClosedRange<Bound>, value: Bound) -> Bool
@nahive
nahive / UITableView+SectionsEnumExample.swift
Last active February 16, 2017 13:31
Example of handling sections in UITableView
enum Section: Int {
case robots = 0
case cats
case dogs
case humans
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section(rawValue: section) {
case .robots?: // return rows count
@nahive
nahive / Observable.swift
Last active February 13, 2017 11:50
Simple observable class for Swift
import Foundation
class ObservableModel: NSObject {
private var observers: [(keypath: String, block: (Any?) -> Void)] = []
// keypath has to exist and has to have dynamic property to be notified
func observe<T>(keypath: String, with block: @escaping (T?) -> Void) {
guard let _ = value(forKeyPath: keypath) as? T else {
fatalError("value at \(keypath) is not of type \(T.self)")
@nahive
nahive / LinkedListOperator.swift
Last active January 22, 2017 18:39
Linked Lists in Swift using custom operator
// maybe someone will find this useful
// i was going through codewars and
// encountered linkedlist challenge
// i noticed special A -> B operator
// and i decided to recreate it it swift :)
//MARK: custom class that holds data
class Node<T> {
var data: T
var next: Node<T>?
@nahive
nahive / robot.js
Created December 4, 2012 13:07
masSlany
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.ahead(100);