Skip to content

Instantly share code, notes, and snippets.

View Nirma's full-sized avatar

Nicholas Maccharoli Nirma

  • Screaming Cactus LLC
  • Tokyo
  • 00:55 (UTC +09:00)
View GitHub Profile
@Nirma
Nirma / map_filter_reduce.swift
Last active January 24, 2016 14:16
Map and filter implemented with a version of reduce taking a list as input.
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
}
@Nirma
Nirma / Decorators.go
Created December 28, 2015 12:22
Using Decorators in Golang to profile performance
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
@Nirma
Nirma / Shift.swift
Last active December 25, 2015 07:39
enum ShiftDirection {
case Left, Right
}
func shiftBy<T>(shiftDegree: Int, inArray: [T], shiftDirection: ShiftDirection) -> [T] {
guard inArray.count > 2 else {
return inArray
}
@Nirma
Nirma / QuickHighlighting.swift
Created September 7, 2015 09:06
Simple highlighting with special characters
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()
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 / Radix.rb
Last active August 29, 2015 14:19
Radix Sort in Ruby
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
@Nirma
Nirma / StaleCodeCleaner
Last active August 29, 2015 14:18
Ruby script to clean up unused / old classes
#!/usr/local/bin/ruby
#Get Contents
file = File.open(ARGV[0], "r")
data = file.read
file.close
search_results = []
data.each_line{ |line|
=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' => [],
> [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)