Skip to content

Instantly share code, notes, and snippets.

View jweinst1's full-sized avatar
🎯
Focusing

Josh Weinstein jweinst1

🎯
Focusing
View GitHub Profile
@jweinst1
jweinst1 / 0_reuse_code.js
Last active August 29, 2015 14:25
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@jweinst1
jweinst1 / Closer While Loops swift
Last active August 29, 2015 14:25
Threading While Loops into Closures in Swift
// multiplies by adding using a while loop within a closure
var MulbyAdding = {(x:Int, y:Int) -> Int in var add = 0; var count = 0; while count < y {
add += x
count += 1
}; return add
}
// A Power closure that uses MulbyAdding and a while loop
var PowerbyMul = {(x:Int, y:Int) -> Int in var Pow = x; var count = 0; while count < y {
Pow *= x; count += 1
}; return Pow
not_reviewed_by_user = {restaurant_name(food_place):predictor(food_place) for food_place in restaurants if food_place not in reviewed}
for r in reviewed: #this for loop adds the restaurants reviewed by the user with the rating
not_reviewed_by_user.update({restaurant_name(r):user_rating(user, restaurant_name(r))})
return not_reviewed_by_user
@jweinst1
jweinst1 / Summation and Mean of an Array
Created July 18, 2015 05:18
In swift, closures that take the count, sum, and mean of an array in swift.
// sums all the integers in an array
var ArraySum = {(var list:Array<Int>) -> Int in
var sum = 0
for element in list {
sum += element
}
return sum
}
// uses a for loop to count the integers in an array
var ArrayCount = {(var list:Array<Int>) -> Int in
@jweinst1
jweinst1 / Range functions in Swift
Created July 18, 2015 06:02
Creates Ranges and Reversed Arrays of Integers in Swift
// creates an array of integers from 0 to number-1 values
var RangeArray = {(number:Int) ->Array<Int> in var value = 0; var list = [Int]()
while value < number {
list.append(value)
value += 1
}
return list
}
// creates an array of integers from num1 to num2-1 values
var SpecificRange = {(num1:Int, num2:Int) ->Array<Int> in var value = num1; var list = [Int]()
@jweinst1
jweinst1 / Check for Divsibility
Created July 18, 2015 06:33
Returns the Integers for which a number is divisible by within a specified Array in Swift
//returns an array of integers that are divisible by num, within a parameter array
var DivisibleFilter = {(num:Int, divisor:Int) ->Bool in num % divisor == 0}
var DivisibleInRange = {(num:Int, list:Array<Int>) ->Array<Int> in var divlist = [Int]()
for element in list {
if DivisibleFilter(element, num) == true {
divlist.append(element)
}
}
return divlist
@jweinst1
jweinst1 / String character Indexing Swift
Created July 19, 2015 02:48
Functions that allow you to check for the indexes at which a character appears in a string in Swift.
// returns a dictionary that holds the indexes for every character in a string
func StringIndexer(str:String) ->Dictionary<Int, Character> {
var strlist = Array(str); var strdict = [Int:Character](); var indexer = 0
for letter in strlist {
strdict.updateValue(letter, forKey: indexer)
indexer += 1
}
return strdict
}
// returns an array containing the indexes where a character appears in a string
@jweinst1
jweinst1 / Array Slicing Swift
Created July 19, 2015 05:00
Two functions that slice arrays in Swift
// Returns a slice of an array that contains characters
func StringSlicer(list:Array<Character>, n1:Int, n2:Int) ->Array<Character> {
var slicer = n1; var slicedlist = [Character]()
while slicer <= n2 {
slicedlist.append(list[slicer])
slicer += 1
}
return slicedlist
}
// returns a slice of an array of characters with an increment
@jweinst1
jweinst1 / String Slicer Swift
Created July 19, 2015 07:12
For every a character appears in a string, it creates a slice of the string and appends it to an array
// returns a dictionary that holds the indexes for every character in a string
func StringIndexer(str:String) ->Dictionary<Int, Character> {
var strlist = Array(str); var strdict = [Int:Character](); var indexer = 0
for letter in strlist {
strdict.updateValue(letter, forKey: indexer)
indexer += 1
}
return strdict
}
// returns an array containing the indexes where a character appears in a string
@jweinst1
jweinst1 / Counting Words and Letters in Swift.swift
Created July 20, 2015 20:14
Functions that count words and letters in a string in swift.
//counts a specific letter in a string
func SpecificLetterCount(str:String, char:Character) ->Int {
var letters = Array(str); var count = 0
for letter in letters {
if letter == char {
count += 1
}
}
return count
}