Skip to content

Instantly share code, notes, and snippets.

View gavi's full-sized avatar
🎯
Focusing

Gavi Narra gavi

🎯
Focusing
View GitHub Profile
@gavi
gavi / columns.js
Created March 31, 2014 20:09
Get Column names from csv data using D3
/*
As you read the data, use Object.keys() method to extract the column names. Might be useful for binding dropdowns.
*/
var columns=null;
d3.csv('nutdata.csv').row(function(row){
if(columns==null){
columns=Object.keys(row);
columns=columns.slice(2,48); //Optional - to remove columns you dont need
}
@gavi
gavi / geocoder.swift
Created June 13, 2015 19:57
Semaphore based NSURLSession for Command Line GeoCoder
import Foundation
func BlockingGet(url:String)->String?{
let sema=dispatch_semaphore_create(0)
let url:NSURL!=NSURL(string:url)
var output:String?
NSURLSession.sharedSession().dataTaskWithURL(url) {
data, response, error in
output = NSString(data: data!, encoding: NSUTF8StringEncoding) as String?
dispatch_semaphore_signal(sema)
@gavi
gavi / async.swift
Last active December 27, 2015 01:16
Running asynchronous code in Xcode Playground
import Foundation
import XCPlayground
// Updated for Swift 2.0
let url:NSURL!=NSURL(string: "http://objectgraph.com")
NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
print(error)
print(response)
}.resume()
print("This line is printed first as the network call is not complete")
//: Playground - noun: a place where people can play
import UIKit
import MapKit
import PlaygroundSupport
class Container:UIView{
let mapView:MKMapView
override init(frame: CGRect) {
@gavi
gavi / NLP.swift
Created June 8, 2017 13:09
NSLinguisticTagger updates in Foundation for Swift 4
import Foundation
var str = """
This is some text that needs to be processed. I do not know how fast this runs?
日本,
Лорем ипсум долор сит амет, перпетуа урбанитас ин про, проприае цонсететур ид сит
"""
let tagger=NSLinguisticTagger(tagSchemes: [.lemma, .language, .lexicalClass], options:0 )
tagger.string=str
extension String{
static func randomEmoji()->String{
let range = [UInt32](0x1F601...0x1F64F)
let ascii = range[Int(drand48() * (Double(range.count)))]
let emoji = UnicodeScalar(ascii)?.description
return emoji!
}
}
@gavi
gavi / JSONEncode.swift
Created July 15, 2017 16:16
JSON Encoding in Swift Basic Example
import Foundation
struct Point:Codable{
var x:Float
var y:Float
var moment:Date
}
let p=Point(x:10,y:10,moment:Date())
let x=JSONEncoder()
@gavi
gavi / JSONDecode.swift
Created July 15, 2017 16:19
JSON Decoding in Swift Basic
let inputData = """
{"x":10,"y":10,"moment":"2017-06-08T21:36:37Z"}
""".data(using: .utf8)!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let obj=try? decoder.decode(Point.self, from: inputData)
if let point=obj{
print(point.x)
}
@gavi
gavi / CodableNestedStructures.swift
Last active February 1, 2023 14:27
Parsing Nested JSON Structure in Swift
import Foundation
let geoResult="""
{
"results": [
{
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
@gavi
gavi / CodingKeysExample.swift
Created July 15, 2017 16:57
CodingKeys Enum for renaming JSON keys
struct GeocodingResult:Codable{
struct Geometry:Codable{
struct Location:Codable{
let lat:Float
let lng:Float
}
let location:Location
}
let formattedAddress:String
let geometry:Geometry