Skip to content

Instantly share code, notes, and snippets.

# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@ianhirschfeld
ianhirschfeld / EnumTableViewTutorial_03.swift
Created June 23, 2017 20:19
Example of UITableViewDataSource and UITableViewDelegate using a enum for sections.
extension ViewController: UITableViewDataSource, UITableViewDelegate {
// As long as `total` is the last case in our TableSection enum,
// this method will always be dynamically correct no mater how many table sections we add or remove.
func numberOfSections(in tableView: UITableView) -> Int {
return TableSection.total.rawValue
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Using Swift's optional lookup we first check if there is a valid section of table.
@ianhirschfeld
ianhirschfeld / EnumTableViewTutorial_02.swift
Last active June 23, 2017 20:21
Example of some test data and how to sort it using an enum.
let MovieData = [
["title": "Jason Bourne", "cast": "Matt Damon, Alicia Vikander, Julia Stiles", "genre": "action"],
["title": "Suicide Squad", "cast": "Margot Robbie, Jared Leto, Will Smith", "genre": "action"],
["title": "Star Trek Beyond", "cast": "Chris Pine, Zachary Quinto, Zoe Saldana", "genre": "action"],
["title": "Deadpool", "cast": "Ryan Reynolds, Morena Baccarin, Gina Carano", "genre": "action"],
["title": "London Has Fallen", "cast": "Gerard Butler, Aaron Eckhart, Morgan Freeman, Angela Bassett", "genre": "action"],
["title": "Ghostbusters", "cast": "Kate McKinnon, Leslie Jones, Melissa McCarthy, Kristen Wiig", "genre": "comedy"],
["title": "Central Intelligence", "cast": "Dwayne Johnson, Kevin Hart", "genre": "comedy"],
["title": "Bad Moms", "cast": "Mila Kunis, Kristen Bell, Kathryn Hahn, Christina Applegate", "genre": "comedy"],
["title": "Keanu", "cast": "Jordan Peele, Keegan-Michael Key", "genre": "comedy"],
@ianhirschfeld
ianhirschfeld / EnumTableViewTutorial_01.swift
Last active June 23, 2017 20:20
Example of setting up an enum for UITableView sections.
class ViewController: UIViewController {
// The magic enum to end our pain and suffering!
// For the most part the order of our cases do not matter.
// What is important is that our first case is set to 0, and that our last case is always `total`.
enum TableSection: Int {
case action = 0, comedy, drama, indie, total
}
// This is the size of our header sections that we will use later on.
@ianhirschfeld
ianhirschfeld / Singleton.cs
Created June 6, 2017 23:51
Unity singleton class. Just inherit.
public class Singleton<T>: MonoBehaviour where T: MonoBehaviour {
protected static T instance;
// Returns the instance of this singleton.
public static T Instance {
get {
if (instance == null) {
instance = (T)FindObjectOfType(typeof(T));
if (instance == null) {
@ianhirschfeld
ianhirschfeld / recursion_examples.rb
Created June 10, 2016 20:50
Examples of recursion in Ruby
# Template when looking at a recursive function:
#
# If base case, then end recursion
# else reducation case, and call recursively
# Example: Adding together all the elements of an array.
def sum(numbers_array)
# Base Case: Is the array empty?
if numbers_array.empty?
return 0
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def reverse_list(list, previous=nil)
@ianhirschfeld
ianhirschfeld / fit_svg_text_to_width.js
Created September 14, 2015 23:14
Dynamically adjust a SVG text box so that the font fits a certain width.
var fitTextToWidth = function(node, width) {
var nodeWidth = node.getBBox().width;
if (nodeWidth > width) {
var $node = $(node);
var fontSize = $node.css('font-size');
fontSize = parseInt(fontSize.substring(0, fontSize.length - 2));
fontSize--;
if (fontSize > 0) {
$node.css('font-size', fontSize + 'px');
fitTextToWidth(node, width);
@ianhirschfeld
ianhirschfeld / AngleGradientBorderTutorial_03.swift
Created October 3, 2014 06:19
Example of using AngleGradientLayer to create an angle gradient border.
//
// AngleGradientBorderView.swift
// AngleGradientBorderTutorial
//
import UIKit
class AngleGradientBorderView: UIView {
// Constants
@ianhirschfeld
ianhirschfeld / AngleGradientBorderTutorial_02.swift
Last active August 29, 2015 14:07
Example of subclassing AngleGradientLayer to allow borders.
//
// AngleGradientBorderLayer.swift
// AngleGradientBorderTutorial
//
import UIKit
class AngleGradientBorderLayer: AngleGradientLayer {
// Properties