Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@mtackes
mtackes / NSURLSession-uploadTask-example.swift
Last active December 7, 2018 04:03
An basic example of using an upload task with a completion handler.
import Cocoa
let session = NSURLSession.sharedSession()
let request = NSURLRequest(URL: NSURL(string: "http://example.com")!)
let dataToUpload = "Some Arbitrary Data".dataUsingEncoding(NSUTF8StringEncoding)
let uploadTask = session.uploadTaskWithRequest(request, fromData: dataToUpload,
completionHandler: { (responseData, response, error) in
// Check on some response headers (if it's HTTP)
@nickstanisha
nickstanisha / trie.py
Created November 21, 2015 22:58
An object-oriented implementation of a "Trie" in Python
class Node:
def __init__(self, label=None, data=None):
self.label = label
self.data = data
self.children = dict()
def addChild(self, key, data=None):
if not isinstance(key, Node):
self.children[key] = Node(key, data)
else:
@sanderman01
sanderman01 / OrbitCamera.cs
Created October 29, 2015 10:02
Orbital Camera in Unity
using UnityEngine;
using System.Collections;
/// <summary>
/// Defines angle limits as the maximum deviation away from the rotation of this object.
/// (in other words: if the yawlimit is 45, then you can only move up to 45 degrees away from this rotation in both directions.
/// This means the total angle available would be an angle of 90 degrees)
/// An angle of 180 allows complete freedom of movement on that axis.
/// </summary>
public class FocusPoint : MonoBehaviour
@mchirico
mchirico / RW.swift
Created October 15, 2015 16:25
RW to file in swift
//
// RW.swift
// Web
//
// Created by Mike Chirico on 10/15/15.
// Copyright © 2015 Mike Chirico. All rights reserved.
//
import UIKit
func openAndSelectFile(filename:String){
let files = [NSURL(fileURLWithPath: filename)];
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(files);
}
@ericdke
ericdke / splitBy.swift
Last active July 10, 2023 09:55
Swift: split array by chunks of given size
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
extension Array {
func splitBy(subSize: Int) -> [[Element]] {
return 0.stride(to: self.count, by: subSize).map { startIndex in
let endIndex = startIndex.advancedBy(subSize, limit: self.count)
return Array(self[startIndex ..< endIndex])
}
}
}
@marchinram
marchinram / UIImage+PixelColor.swift
Last active January 19, 2022 08:53
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
guard x >= 0 && x < Int(size.width) && y >= 0 && y < Int(size.height),
let cgImage = cgImage,
let provider = cgImage.dataProvider,
let providerData = provider.data,
let data = CFDataGetBytePtr(providerData) else {
return nil
@figgleforth
figgleforth / NSImage+pixelData.swift
Last active July 22, 2022 15:22
Extract RGBA pixel data from an NSImage in Swift
//
// NSImage+pixelData.swift
//
// Created by Figgleforth on 5/21/15.
// Copyright (c) 2015 Bojan Percevic. All rights reserved.
//
import AppKit
extension NSImage {
@xiaohanyu
xiaohanyu / knight.org
Last active April 12, 2017 10:22
Knight problem

Problem

There’s a chess board of size $N × N$, and an piece Knight, which has the initial position $(x, y)$.

Knight can step by going forward or backward two units in one direction, and one units in the other direction.

Find a way to position $(N, N)$ for this Knight piece.

@ericdke
ericdke / resizeNSImage.swift
Last active March 3, 2023 13:47
Resize NSImage
func resize(image: NSImage, w: Int, h: Int) -> NSImage {
var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
var newImage = NSImage(size: destSize)
newImage.lockFocus()
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.CompositeSourceOver, fraction: CGFloat(1))
newImage.unlockFocus()
newImage.size = destSize
return NSImage(data: newImage.TIFFRepresentation!)!
}