Skip to content

Instantly share code, notes, and snippets.

View pushkarnk's full-sized avatar

Pushkar N Kulkarni pushkarnk

  • Canonical | ex-IBM
  • India
View GitHub Profile
@pushkarnk
pushkarnk / DownloadTask.swift
Last active June 10, 2016 04:56
Downloading a file using NSURLSession
import Foundation
public class Espresso : NSObject {
internal var running: Bool = false
var currDownload: Int64 = -1
internal func download(urlString: String) {
let config = NSURLSessionConfiguration.default()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
@pushkarnk
pushkarnk / UploadTask.swift
Last active May 24, 2016 20:24
Uploading a file to Dropbox using NSURLSession
import Foundation
//Simple upload example without a delegate
public class UploadTask : NSObject {
public var uploading = false
let filePath: String
let fileName: String
var task: NSURLSessionDataTask! = nil
var session: NSURLSession! = nil
-- sum of first N integers
sumtorial :: Integer -> Integer
sumtorial 0 = 0
sumtorial n = n + sumtorial (n-1)
-- the abs function using guards
mabs :: Integer -> Integer
mabs x
| x == 0 = 0
@pushkarnk
pushkarnk / newton.scala
Last active March 11, 2019 06:32
Newton's square root method (functional scala)
def abs(x: Double) = if (x > 0) x else -x
def isGoodEnough(guess: Double, x: Double) = abs(guess * guess - x)/x < 0.0000001
def improve(guess: Double, x: Double) = (guess + x/guess)/2
def newtonsMethod(guess: Double, x: Double): Double = {
if (isGoodEnough(guess,x)) guess
else newtonsMethod(improve(guess, x), x)
}
@pushkarnk
pushkarnk / chapter1.scala
Created May 26, 2016 11:11
Passing arguments by name and value in Scala
def sum(x: Int, y: Int) = {
print("In sum\n")
x + y
}
def square(x: Int) = {
print("In square\n")
x * x
}
@pushkarnk
pushkarnk / chapter2.hs
Created May 27, 2016 15:12
Parametric Polymorphism and Functional programming in Haskell
-- counting the number of vowels in a string
import Data.Char
isVowel::Char->Bool
isVowel c
| u == 'A' || u == 'E' = True
| u == 'I' || u == 'O' = True
| u == 'U' = True
| otherwise = False
where u = toUpper c
@pushkarnk
pushkarnk / first.erl
Last active May 29, 2016 11:56
Simple functions and pattern matching
% My first Erlang program
-module(first).
-export([double/1,mul/2,areaOfTriangle/3,productOfList/1,squaredList/1]).
-export([factorial/1, aNd/2, xOr/2, oR/2, nOr/1,howMany/3, fibonacci/1, fib/1]).
-export([fact/1]).
mul(X,Y) ->
X*Y.
double(X) ->
import Foundation
public class DataTask: NSObject {
public var downloading = false
let baseURL = "https://restcountries.eu/rest/v1/name/"
public var capital = "unknown"
var task: NSURLSessionDataTask! = nil
var session: NSURLSession! = nil
private var country: String
@pushkarnk
pushkarnk / se1.swift
Last active June 24, 2016 10:33
Swifty Encounter[1]
import Foundation
public class Test : NSObject {
public init?(x: Bool) {
guard x == true else { return nil }
}
public override var description: String {
return "test"
@pushkarnk
pushkarnk / se1a.swift
Created June 24, 2016 10:08
Swift Encounter[1]
import Foundation
public class Test : NSObject {
public override var description: String {
return "test"
}
}
print(Test())