Skip to content

Instantly share code, notes, and snippets.

View avdyushin's full-sized avatar

Grigory Avdyushin avdyushin

View GitHub Profile
@avdyushin
avdyushin / string.swift
Last active May 27, 2016 09:23
Swift extension for string manipulation
//
// String 1.0
// Created by Caleb Hess on 2/22/16.
//
public extension String {
public var count: Int {
return self.characters.count
}
@avdyushin
avdyushin / UIImageExtensions.swift
Last active May 31, 2016 11:33
UIImage missed resize method via Swift extensions
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSizeMake(1, 1)) {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
self.init(CGImage: UIGraphicsGetImageFromCurrentImageContext().CGImage!)
UIGraphicsEndImageContext()
@avdyushin
avdyushin / Functional.swift
Last active March 22, 2016 09:08
Functional Swift Cheat Sheet
// Product of list
func product(list: [Double]) -> Double {
return list.reduce(1, combine: *)
}
// Sum of list
func sum(list: [Int]) -> Int {
return list.reduce(0, combine: +)
}
@avdyushin
avdyushin / ghci.conf
Last active August 14, 2016 09:53
Nice λ prompt at ~/.ghc/ghci.conf
import qualified Control.Applicative
import qualified Data.Char
import qualified Data.List
:set prompt "\ESC[34m\STX λ \ESC[m\STX"
:set +t
:set +m
@avdyushin
avdyushin / ASCII.swift
Created February 15, 2016 09:59
Swift Character get ASCII code value
extension Character {
var asciiValue: Int {
get {
let s = String(self).unicodeScalars
return Int(s[s.startIndex].value)
}
}
}
@avdyushin
avdyushin / Stack.swift
Created February 1, 2016 09:03
Stack data structure via linked lists using Swift enums
indirect enum LinkedList<T> {
case Empty
case Node(value: T, next: LinkedList<T>)
init() { self = .Empty }
}
extension LinkedList {
func cons(x: T) -> LinkedList<T> {
return .Node(value: x, next: self)
@avdyushin
avdyushin / Unix command cheats
Last active July 2, 2022 22:56
Terminal commands cheat list
#
# ZSH, Mac OS X or Ubuntu
#
# Decode base 64 and convert into hex string
echo 'aGI32ansgxzKWh3RV4EjRwQsW1v1la+7NBTdjx8zYq0=' | base64 -D | hexdump -ve '1/1 %.2x' | pbcopy
# Get dirs sizes in one level and sort them
du --max-depth=1 -h | sort -n
@avdyushin
avdyushin / HttpClient.swift
Created January 25, 2016 12:17
Demo HTTP client for RESTful services in Swift 2.0
import Foundation
class HttpClient: NSObject {
let logging = true
let session = NSURLSession.sharedSession()
func makeRequest(request: NSURLRequest, completionHandler: (NSDictionary?, NSURLResponse?, NSError?) -> Void) {
logRequest(request)
@avdyushin
avdyushin / debugLog.m
Created September 11, 2013 13:00
Nice debug log wrapper for NSLog()
#define __FILE_NAME__ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String]
#ifdef DEBUG
#define DLog( s, ... ) NSLog( @"%s:%d %s %@", \
__FILE_NAME__, \
__LINE__, \
__PRETTY_FUNCTION__, \
[NSString stringWithFormat:(s), ##__VA_ARGS__] )
@avdyushin
avdyushin / deviceDetection.m
Created September 11, 2013 06:56
Detect which device is running app
#define is_iPad ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
#define is_iPhone ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
#define is_iPhone5 ( is_iPhone && [[UIScreen mainScreen] bounds].size.height == 568.0f )
#define is_Retina ( [[UIScreen mainScreen] scale] == 2.0f )