Skip to content

Instantly share code, notes, and snippets.

View kittinunf's full-sized avatar

Kittinun Vantasin kittinunf

View GitHub Profile
fun doSomething(path: String) {
val content: String //instead of declare as `var content: String? = null`
val br = BufferedReader(FileReader(path))
var currentLine = br.readLine()
content = buildString {
while (currentLine != null) {
appendln(currentLine)
currentLine = br.readLine()
struct Parent {
let id: Int
let children: [Int]
}
struct Node {
let id: Int
let parent: Int
let children: [Int]
}
@kittinunf
kittinunf / Json.swift
Last active May 27, 2016 06:25
JsonParser
struct Error : ErrorType {
let message: String
}
struct Json {
let dictionary: [String : AnyObject]
init?(dictionary: [String : AnyObject]?) {
guard let dictionary = dictionary else { return nil }
interface Fooable {
fun foo(): String
}
class FooImpl : Fooable {
override fun foo() = "I am fooImpl"
}
class AnotherFooImpl : Fooable {
override fun foo() = "I am anotherFooImpl"
//a somewhat contrived example of how reactive programming can be useful
//some real concerns that are omitted, but can be easily accomplished using rxswift are
//error handling, displaying loading indicators, etc
// A result object that comes from the network.
// The contents are irrelevant for this example.
struct Result {
let text: String
let someOtherThing: String
std::unordered_map<std::string,std::string> m{{"1", "2"}, {"3", "4"}, {"5", "6"}};
std::unordered_map<std::string,std::string> ms;
std::transform(std::begin(m), std::end(m), std::inserter(ms, std::begin(ms)), [](const auto& p) { return {p.first, p.first}; };
import UIKit
/*:
## Redux
A Predictable state container for not just JavaScript apps
*/
/*:
### Action
Payload of information from app to your __Store__
//
// UIScrollView+RACSupport.h
// Taskworld
//
// Created by Kittinun Vantasin on 8/5/15.
// Copyright (c) 2015 Taskworld. All rights reserved.
//
@import UIKit;
class User(val id: Int, val name: String, val age: Int, val email: String) {
class Deserializer : Deserializable<User> {
override val deserializer: (JSON) -> Result<User, Exception> = { json ->
::User.create.
map(json at "id").
apply(json at "name").
apply(json at "age").
apply(json at "email")
}
- (RACSignal *)rac_requestWithMethod:(NSString *)method path:(NSString *)path parameters:(id)parameters {
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSError *error;
NSURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&error];
if (error) {
[subscriber sendError:error];
return nil;
}