Skip to content

Instantly share code, notes, and snippets.

View debreuil's full-sized avatar

Robin Debreuil debreuil

View GitHub Profile
<?xml version="1.0"?>
<flash_profile version="1.0" name="pngExport">
<PublishFormatProperties enabled="true">
<defaultNames>1</defaultNames>
<flash>0</flash>
<generator>0</generator>
<projectorWin>0</projectorWin>
<projectorMac>0</projectorMac>
<html>0</html>
<gif>0</gif>
fl.outputPanel.clear();
var trace = fl.trace;
fl.showIdleMessage(false);
var runOnAllItems = true;
var runOnAllFiles = true;
var runOnSubdirectories = false;
var flashDocs = [];
var docOrg = fl.getDocumentDOM();
@debreuil
debreuil / protostyle_react.js
Created April 14, 2015 02:08
protostyle_react.js
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ScrollView,
@debreuil
debreuil / RungeKuttaSpringImplementation.m
Created February 3, 2015 22:50
Pixate spring implementation
// Evaluates one tick of a spring using the Runge-Kutta Algorithm.
// This is generally a bit mathy, here is a reasonable intro for those interested:
// http://gafferongames.com/game-physics/integration-basics/
// (double)stepSize: the duration between steps. This should be around 1/60th of a second. Values too large will not work as the spring adjusts itself over time based on previous values, so if values are larger than 1/60th of a second this method should be called for each whole 1/60th of a second segment plus the remainder.
// external variables
// (double) _curT: How far along the spring the current value is, 0 being start and 1 being end. Because this is a spring that value will go beyond 1 and then back below 1 etc, as it 'springs'.
// (double) _tVelocity: the current velocity of the spring.
// (BOOL) _stopSpring: indicates the spring has settled to minimum amount and should be considered the last 'tick'
extension String {
func getUnicodeIndex()->Int {
return Int(self.unicodeScalars[self.unicodeScalars.startIndex].value)
}
static func getUnicodeRangeFrom(from:Int, to:Int, closedRange:Bool)->String {
let step = from > to ? -1 : 1;
let final = closedRange ? to + step : to
let chars = Int[]((from..final).by(step)).map{String(UnicodeScalar($0))}
return "".join(chars)
// What are nested and curried functions? How do you use them in Swift?
// nesting/currying allows piping function calls together like fn(1)(2)(3)
// multiple functions, requires fnA(1) + fnB(2) + fnC(3) syntax
func daysToSeconds(days:Int)->Int {
return days * 24 * 60 * 60
}
func hoursToSeconds(hours:Int)->Int {
return hours * 60 * 60
}
// How to you safely convert an AnyObject with a number value into a Double?
let anyDouble:AnyObject = 6.41
if let xx = anyDouble as? NSNumber {
println( Double(xx)) // prints 6.41
}
// will throw an error if the conversion fails!
let casted = Double(anyDouble as NSNumber) // 6.41
// How do you compare tuples for equality?
var tupleA = (1, "one")
var tupleB = (1, "one")
// tup == tup2 // error: tuples can not be compared with ==, can not be used as hash key etc
// If you need to test for object equality, a tuple is not suitable
tupleA.0 == tupleB.0 && tupleA.1 == tupleB.1 // but you can do this
// you can also use element names
var tupleC = (a:1, b:"one")
tupleA.0 == tupleC.a && tupleA.1 == tupleC.b
// How do you get the keys from a dictionary?
let array = [1,2,3]
let dict = [1: "One", 2: "Two", 3: "Three"]
// cast to get the keys in an array
var keys = Array(dict.keys)
keys == array // comapre arrays with ==
var values = Array(dict.values) // the same works for values
typealias Sig = (num:Int)->Int[]
var wrap:Sig = {(num:Int) in return [num]}
wrap(num:3) // [3]
wrap(num:4) // [4]
func numLog() -> Sig{
var log:Int[] = []
return {
log += $0