Kotlin Higher Order Function (Lambda)

This example is basically showing how to pass a function as a parameter to an object that can then be called from within. I use a String but it could be any parameters or none. Comparing to an interface it is a bit cleaner and a bit less code. Interfaces are great when you need multiple functions but using a Lambda works great for just a function and is just as clear.

/**
 ** Higher-Order Function as parameter to a class.
 ** Robert Linnemann
 **/

fun main(args: Array) {
    //make an object that makes an internal object.
    val m = Master()
    //call a function on the internal object that then calls
    //through to the lamda function that was passed into it.
    m.apprentice?.sendMessage()
}

class Master {
	var apprentice: Apprentice?
    init {
        //pass in the function print to the Apprentice object.
		apprentice = Apprentice(::print)        
    }

    fun print(message: String) {
        println(message)
    }
}

class Apprentice(send: (String) -> Unit) {
    val send = send
    fun sendMessage() {
        send("Print This for me.")
    }
}

I’ve also implemented an interface alongside which compares these features a bit better.

/**
 ** Higher-Order Function as parameter to a class.
 ** Robert Linnemann
 **/

fun main(args: Array) {
    //make an object that makes an internal object.
    val m = Master()
    //call a function on the internal object that then calls
    //through to the lamda function that was passed into it.
    m.apprentice?.sendMessageThroughLambda()
    //A similar was to pass a message to a parent object would be to
    //use an interface
    m.apprentice?.sendMessageThroughInterface()
}

interface PassMessageListener {
    fun message(message: String)
}

class Master: PassMessageListener {
	var apprentice: Apprentice?
    init {
        //pass in the function print to the Apprentice object.
		apprentice = Apprentice(::print)    
        //setup the interface listener
        apprentice?.passMessageListener = this
    }

    fun print(message: String) {
        println(message)
    }
    //interface method
    override fun message(message: String) {
        print(message)
    }
}

//This gets a function passed in as the variable 'send'.
class Apprentice(send: (String) -> Unit) {
    val send = send
    var passMessageListener: PassMessageListener? = null
    
    fun sendMessageThroughLambda() { 
        //sends directly to the function on a whole 'notha level
        send("Print this for me.")
    }
    fun sendMessageThroughInterface() {
        //traditional interface way to pass a message
        passMessageListener?.message("Print this for you")
    }
}

To run this you can put it into the great site https://try.kotlinlang.org/
or compile it from the command line (install tools). I prefer using sdkman.io
Then run these commands:

kotlinc higherOrder.kt -include-runtime -d higherOrder.jar
java -jar higherOrder.jar

Check it out in this repo:
https://github.com/mevdev/KotlinHigherOrderMessagePassing

All 4 States of Stack Transitions for iOS

https://github.com/mevdev/View-Controllers-4-State-Transitions

Animate or do special things for each of the 4 state transitions of a View Controller

This fills a need for when I want to do different animations depending on which transition a View Controller is happening.

We’re going to do different animations when View Controllers are pushed or popped from the stack. We’ll start with 4 states, being ‘pushed’, ‘popped’, ‘put back’ and ‘popped to’.

‘Pushed On’ is when you add a new View Controller to the stack and it is now visible.
‘Popped’ is when your view controller was visible and it is transitioning to be gone.
‘Put Back’ is when your view controller was visible and another one is going on top of it.
‘Popped to’ is when your view controller was in the stack and the one on top was just popped.
If you want to slow things down do ‘Slow Animations’ ⌘-T or in the menu Debug->Slow Animations.

FlurryAds Interstitial ads with Cocos2d v3

This is my first foray into ad-supported territory. I am really liking Cocos2d and recently my focus has been on the newest v3 (currently rc4). In this tutorial we will put up some interstitial ads for iphone and/or ipad.

Cocos2d v3 has a great new installer. It gives you a couple options in XCode. It’s even compatible with the latest XCode (5.1) and ARC.

-all those frameworks.
AdSupport.framework (Mark as Optional to support < iOS 6.0)
CoreGraphics.framework
Foundation.framework
MediaPlayer.framework
Security.framework
StoreKit.framework (Mark as Optional to support < iOS 6.0)
SystemConfiguration.framework
UIKit.framework

//do the first delegate stuff
In the AppDelegate.h go into the
-(BOOL) application: didFinishLaunchingWithOptions:

[Flurry setCrashReportingEnabled:YES];
[Flurry startSession:@"YOUR_API_KEY"];
[FlurryAds enableTestAds:YES];

Continue reading