Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-byrne committed Mar 7, 2019
1 parent e76e2c7 commit b91e8d5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Kotlin Night Rochester 2019

## Crash Course intro to Kotlin.

Go through the intro subproject. Start in `io.mfj.kotlinnight.intro.Classes`
2 changes: 2 additions & 0 deletions intro/src/main/java/io/mfj/kotlinnight/intro/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,5 @@ with comments. This is super nice.
*/
// The comment ended there

// Let's take a look at collections
22 changes: 15 additions & 7 deletions intro/src/main/java/io/mfj/kotlinnight/intro/CollectionsIntro.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package io.mfj.kotlinnight.intro

// Objects are like static classes
object CollectionsIntro {
// Kotlin has its own set of collections that (in most cases) wrap the ones in java.util.
// They can be mutable or immutable.
// Immutability is important for functional programming.

// Kotlin has its own set of collections that (in most cases) wrap the ones in java.util.
// They can be mutable or immutable.
// Immutability is important for functional programming.
// (Objects are singletons classes)
object CollectionsIntro {

val someStuff = listOf( "a", "b", 0 )
// someStuff is mutable.
// what is the type? (show hints)

fun test() {
@JvmStatic
fun main( args:Array<String> ) {
val thisCanChange = mutableListOf("a", "b", "c")
thisCanChange.plus("d")
thisCanChange.add("d")

//someStuff.add("d")

val someStuffPlusD = someStuff.plus("d")
for ( x in someStuffPlusD ) {
println(x)
}
}

val aMap:Map<String,Int> = mapOf( "a" to 1, "b" to 2, "c" to 3 )
Expand Down
22 changes: 21 additions & 1 deletion intro/src/main/java/io/mfj/kotlinnight/intro/Functional.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,24 @@ object Functional {

// let's make the body take a parameter and return something

}
}

// You can define functions in any scope
object InnerFUnction{

@JvmStatic
fun main( args:Array<String> ) {

listOf( "Tom", "Dick","Harry", "Sally" ).forEach { name ->

fun say(text:String) {
println( "${name} said \"${text}\"" )
}

say("Hello")
say("Goodbye")

}

}
}
37 changes: 37 additions & 0 deletions intro/src/main/java/io/mfj/kotlinnight/intro/X.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.mfj.kotlinnight.intro

// Kotlin lets you add functions to existing classes with Extension Functions

// This is the syntax to create one:

fun String.excite():String = "${this}!!!!!!!"

object Ext {

@JvmStatic
fun main( args:Array<String> ) {

// You can call it just like any other function
println( "hello world".excite() )
}

}

// Classes can contain extension functions, and they are in the class' scope
class Foo( private val name:String ) {

// This extension function has access to the instance.
fun String.said() = "${name} said \"${this}.\""

fun sayHello() {
println( "hello world".said() )
}

companion object {
@JvmStatic
fun main( args:Array<String> ) {
Foo("John").sayHello()
}
}

}

0 comments on commit b91e8d5

Please sign in to comment.