Skip to content

Instantly share code, notes, and snippets.

Avatar
🤠
Sailing the seas of Swift

Nicholas Maccharoli Nirma

🤠
Sailing the seas of Swift
View GitHub Profile
View slow fibonacci
> [fibs x | x <- take 100 [0..]]
-- Fibonacci
fibs :: Int -> Int
fibs hoge
| hoge == 0 = 0
| hoge == 1 = 1
| otherwise = fibs_rec
where fibs_rec = fibs (hoge - 2) + fibs (hoge - 1)
View Samplify.rb
=begin
This fuction is designed to summarize the contents of a dictionary of arrays.
As many elements as possible will be grabbed for a specified size N while maintaining maximum diversity.
Function will terminate as soon as the desired amount of items are gathered.
Example IO:
posts = { 'Greg' => ['A','A','A'],
'Steve' => [],
@Nirma
Nirma / StaleCodeCleaner
Last active Aug 29, 2015
Ruby script to clean up unused / old classes
View StaleCodeCleaner
#!/usr/local/bin/ruby
#Get Contents
file = File.open(ARGV[0], "r")
data = file.read
file.close
search_results = []
data.each_line{ |line|
@Nirma
Nirma / Radix.rb
Last active Aug 29, 2015
Radix Sort in Ruby
View Radix.rb
def radix (a, base = 10)
m = a.max
exp = 1
loop do
bkt = Array.new(base){[]}
a.each{|elem|
pos = (elem / exp) % base
bkt[pos] << elem
}
a = bkt.flatten
View WeirdIdea.swift
import UIKit
extension String {
var length: Int {return count(self)}
}
func nextBinaryTrialIndex(seriesSize: Int, index: Int, pass: Bool) -> Int? {
//Error No case exists
if index >= seriesSize || index <= 0 {
@Nirma
Nirma / QuickHighlighting.swift
Created Sep 7, 2015
Simple highlighting with special characters
View QuickHighlighting.swift
func highlighted(originalString: String, delimiter:[String]) -> NSMutableAttributedString? {
if delimiter.count != 2 {
return nil
}
let highlightedAttributes = [NSForegroundColorAttributeName: UIColor.redColor()]
let searchString = originalString.componentsSeparatedByString(delimiter[0])
var attributedResult = NSMutableAttributedString()
View Shift.swift
enum ShiftDirection {
case Left, Right
}
func shiftBy<T>(shiftDegree: Int, inArray: [T], shiftDirection: ShiftDirection) -> [T] {
guard inArray.count > 2 else {
return inArray
}
@Nirma
Nirma / Decorators.go
Created Dec 28, 2015
Using Decorators in Golang to profile performance
View Decorators.go
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
@Nirma
Nirma / map_filter_reduce.swift
Last active Jan 24, 2016
Map and filter implemented with a version of reduce taking a list as input.
View map_filter_reduce.swift
func reduce<T,R>(list: [T], block: (([R],T) -> R?)) -> [R]? {
var acc = [R]()
for x in list {
if let val = block(acc, x) {
acc += [val]
}
}
return acc
}
View Break Larger Array up into slices
extension Array {
func forEachSlice(sliceSize: Int, block: [Element] -> Void) {
guard sliceSize > 0 && self.count > 0 else {
return
}
var currentSlice = [Element]()
defer {