Skip to content

Instantly share code, notes, and snippets.

@mathonsunday
mathonsunday / gist:3734988
Created September 17, 2012 00:38
viewcontroller.m
//
// ViewController.m
// LayerFun
//
// Created by Veronica on 9/16/12.
// Copyright (c) 2012 Veronica Ray. All rights reserved.
//
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@mathonsunday
mathonsunday / gist:73fda67b25d39743982a
Created May 29, 2014 21:16
Utopian Tree Solution
num_test_cases = gets.chomp.to_i
each_num_of_cycles = []
height_after_n_cycles = {}
i = 0
while i < num_test_cases
each_num_of_cycles.push(gets.chomp.to_i)
i += 1
end
puts "Hello World"
public boolean groupNoAdj(int start, int[] nums, int target) {
if(start >= nums.length) return (target == 0);
if(start + 2 < nums.length) {
if(groupNoAdj(start + 2, nums , target - nums[start]) == true) return true;
}
if(groupNoAdj(start + 1, nums, target) == true) return true;
return false;
}
var latestSession : SessionInfo = validSessions.reduce(validSessions[0], combine: {$0.timestamp > $1.timestamp ? $0 : $1})
var viewController = ProfileViewController(musicServicer: MockMusicService())
// invoke
viewController.viewDidLoad()
// verify
XCTAssertTrue(mockMusicService.playCalled)
}
First step is to desugar do notation. In this case we want to translate it to the bind (>>=) operator:
greeter =
name >>= ask
return ("hello, " ++ name ++ "!")
Now that it's desugared (not convinced I've done it correctly) I will translate it to Swift.
I'm relying on the Reader monad implementation in Swiftz (https://github.com/typelift/Swiftz/blob/968075391aedb15ec51ff9d5b7d4921b8fa3a3c6/Swiftz/Reader.swift)
Current code is:
//: Playground - noun: a place where people can play
import Cocoa
import Foundation
// WinterBuddies a social network to connect you with people who can help you survive the winter
class ProfileViewController : UIViewController {
let musicService = MusicService()
// ProfileViewControllerTests.swift
func testShouldPlayMusicWhenViewDidLoad() {
// setup mock
class MockMusicService : MusicService {
var musicPlayed = false
override func play() {
musicPlayed = true
}
}