At work, we are discussing about a simple (or not so simple) question. ¿Is there any difference between the following invocation methods?
class Obj {
def num
def prtn = {
print this.num
}
}
def listObj = [new Obj(num:1), new Obj(num:2), new Obj(num:3), new Obj(num:4)]
// End Common code
// Method #1
def lst = listObj.collect{ obj-> obj.prtn() }
// Method #2
def lst = listObj.collect{ obj-> { -> obj.prtn()} }.each{ it() }
// Common code
The side-effect of both statements is the same: they print "1234". But the return value is different.
The prtn method always returns null, so the value of listObj.collect{ obj-> obj.prtn() } is [null, null, null, null].
In the case of listObj.collect{ obj-> { -> obj.prtn()} }.each{ it() }, as each returns the collection on which it was invoked, the result will be a list of Closure objects (as that is what listObj.collect{ obj-> { -> obj.prtn()} } returns).
In general, if you only want to iterate a collection for some side effect, use each; if you want to transform it into a different collection, use collect. And try no to mix both side-effect iteration and transformation :)
You can also do:
listObj*.prtn()
And this gives the same result as your Method #1
Related
Let's say I have the following Java class:
public class A {
public Result method1(Object o) {...}
public Result method2(Object o) {...}
...
public Result methodN(Object o) {...}
}
Then, in my Kotlin code:
fun myFunction(...) {
val a: A = ...
val parameter = ...
val result = a.method1(parameter) // what if i want methodX?
do more things with result
}
and I want to be able to choose which methodX will be called inside myFunction. in Java, I would pass A::method7 as an argument and call it. in Kotlin it doesn't compile. How should I solve it in Kotlin?
You can also pass the method reference in Kotlin (without needing the heavy hammer that is reflection):
fun myFunction(method: A.(Any) -> Result) {
val a: A = ...
val parameter = ...
val result = a.method(parameter)
do more things with result
}
myFunction(A::method1)
myFunction {/* do something in the context of A */}
This declares method as part of A, meaning you can call it with normal object.method() notation. It Just Works™ with the method reference syntax.
There's also another form that works with the same call syntax, but makes A more explicit:
fun myFunction(method: (A, Any) -> Result) { ... }
myFunction(A::method1)
myFunction {a, param -> /* do something with the object and parameter */}
You can actually do this exactly like you wanted to:
fun myFunction(kFunction: KFunction2<A, #ParameterName(name = "any") Any, Result>) {
val parameter = "string"
val result: Result = kFunction(A(), parameter)
//...
}
myFunction(A::method1)
myFunction(A::method2)
I'm dealing with some asynchronous functions and trying to update views. In short I have function 1 with asynchronous function that will return a string to be passed to function 2. I am updating views in both functions, on main thread. It all works but I need to understand if this is correct way.
class A {
var varA = ""
var varB = ""
func f1 (_ completion: #escaping (String) -> void ){
some asynchronous call ... { in
...
DispatchQueue.main.async {
self.varA = "something"
sef.labelA.text = self.varA
completion(self.varA)
}
}
}
func f2 (_ string: String){
another asynchronous call ... { in
...
DispatchQueue.main.async {
self.varB = string
sef.labelB.text = self.varB
}
}
}
// funcation call
f1(completion: f2)
}
Three questions, 1) What is the right way to run a dependent function where there is wait for an asynchronous callback?
2) Is DispatchQueue.main.async needed to update views?
3) Is it ok to call async func in another async callback? Isn't there chance self may be nil in some cases if you are updating views in some escaping function?
I'm going to try helping you according to your questions:
Question 1) There are many right ways and each developer can have its own logic, but in this case, what I personally would probably do is something like this:
class A {
func f1 (_ completion: #escaping (String) -> void ){
some asynchronous call ... { in
...
DispatchQueue.main.async { [weak self] in // 1
guard let strongSelf = self else { return } // 2
let varA = "something" // 3
strongSelf.label.text = varA
completion(varA) // 4
}
}
}
func f2 (_ string: String){
another asynchronous call ... { in
...
DispatchQueue.main.async {
sef.labelB.text = string // 5
}
}
}
// function call
// 6
f1(completion: { [weak self] text in
guard let strongSelf = self else { return }
strongSelf.f2(text)
})
}
1 - Here I'm using [weak self] to avoid retain cycles.
2 - Just unwrapping the optional self, case it's nil, I'll just return.
3 - In your case, it's not really necessary to have class variables, so I'm just creating local variables inside the block.
4 - Finally, I'm calling the completion with the variable containing the string.
5 - I also don't really need to set a class variable in here, so I'm just updating the label text with the string provided as a paramater.
6 - Then, I just need to call the first function and use the completion block to call the second after the first one completes.
Question 2) Yes, you must call DispatchQueue.main to update the view. This way your making sure that your code will be executed in the main thread that is crucial for things interacting with UI because it allow us to have a sincronization point as you can read in Apple's documentation.
Question 3) Using [weak self] and guard let strongSelf = self else { return }, I'm avoiding retain cycles and the cases where self can be nil.
If I have a collection of an object in Kotlin, is there a quick way to get a collection of a certain property of those objects? I looked at a list of collection operations for Kotlin, but nothing stood out for me (but I may have overlooked something)
In python it would be akin to:
[person.name for person in persons]
And I'd prefer to use a collections function instead of doing:
var nameMap = mutableListOf<String>()
persons.forEach{person -> nameMap.add(person.name)}
I'm pretty lacking in knowledge of filtering/lambda functions and anything other than list comprehension, so apologies if this is a simple question
it's easy to do in Kotlin:
// v--- the variable type can be removed
var nameMap: MutableList<String> = persons.map { it.name }.toMutableList();
IF you want an immutable List, it can simplify as below:
// v--- the variable type can be removed
var nameMap: List<String> = persons.map { it.name };
OR using function reference expression instead:
var nameMap = persons.map(Person::name);
If you want to map model then do this.
var nameMap: List<Model> = persons.map { Model(it.name,it.number) };
Option 1:
inline fun <reified T, Y> MutableList<T>.listOfField(property: KMutableProperty1<T, Y?>): MutableList<Y> {
val yy = ArrayList<Y>()
this.forEach { t: T ->
yy.add(property.get(t) as Y)
}
return yy
}
Usage:
val serviceIds = services.listOfField(ServiceModel::id)
Option 2:
var serviceIds: MutableList<String> = services.map { it.id }.toMutableList()
Option 3:
Function Reference
var serviceIds = services.map(Person::id)
I am currently trying to understand a problem with recursion. This code checks to see if a node is a leaf node, if it is it will increment the amount of leaf nodes.
I can't understand what the fn.call(this) does and then how it then calls the closure inside the for loop.
Here is my code.
class TreeNode {
String name
List<TreeNode> children = []
// if no children, there is no leaf node
boolean isLeaf() {
return !children
}
void walkTree(Closure fn) {
fn.call(this)
for (child in children) {
child.walkTree(fn)
}
}
}
testTreeNode = new TreeNode(name: "Fred", children: [
new TreeNode(name: "Janet", children: [
new TreeNode(name: "Mark"),
new TreeNode(name: "Anne", children: [new TreeNode(name: "Simon") ]),
new TreeNode(name: "Kelly")
]),
new TreeNode(name: "Nigel")
])
def leafCount = 0
testTreeNode.walkTree { node -> if (node.leaf) leafCount++}
I hope I'm following correctly, but it seems you have 2 questions:
1. What does fn.call(this) do?
Firstly, a Closure is an anonymous block of code that can be executed at a later time.
When you call testTreeNode.walkTree, you are passing the Closure (block of anonymous code) node -> if (node.leaf) leafCount++ as a parameter of the walkTree method, so that closure becomes the variable named fn within the context of walkTree.
Within the walkTree method, the Closure (block of code) is then being explicitly invoked using the Closure.call(args) method.
See: http://groovy-lang.org/closures.html#_calling_a_closure
2. How is the closure executed inside the for loop?
As the closure can be referred to using the fn variable name, you are then able to pass that as an argument directly to walkTree for each child TreeNode in the loop, which then invokes that closure using fn.call(this).
If we substitute the Closure usage with the block of code passed, it might be clearer what is happening:
void walkTree(Closure fn) {
//Closure executed here
if (this.leaf) leafCount++
for (child in children) {
//closure passed as argument to walkTree method of child TreeNodes
child.walkTree { node -> if (node.leaf) leafCount++ }
}
}
I'm looking at a bunch of names, and I want to record all the names. My plan was to iterate over the array of names, and use a dictionary to keep track of each name, so I could just look up the name in the dictionary in O(1) time to see if it existed.
What would be the best data structure for this available in Swift? (I'd love to learn the name of the generic data structure that would be best for this, even if it's not in Swift.)
A Dictionary object would do fine, but it requires a value for the key, when I really only need the key.
You're looking for a Set (a collection of unordered unique objects - some implementations are ordered, though).
Swift and ObjectiveC have NSSet, wouldn't this work for you?
Swift does not provide a set type. You can refer to this blog post for how to define a Set.
Code reproduced here for quick reference:
struct Set<T: Hashable> {
typealias Element = T
private var contents: [Element: Bool]
init() {
self.contents = [Element: Bool]()
}
/// The number of elements in the Set.
var count: Int { return contents.count }
/// Returns `true` if the Set is empty.
var isEmpty: Bool { return contents.isEmpty }
/// The elements of the Set as an array.
var elements: [Element] { return Array(self.contents.keys) }
/// Returns `true` if the Set contains `element`.
func contains(element: Element) -> Bool {
return contents[element] ?? false
}
/// Add `newElements` to the Set.
mutating func add(newElements: Element...) {
newElements.map { self.contents[$0] = true }
}
/// Remove `element` from the Set.
mutating func remove(element: Element) -> Element? {
return contents.removeValueForKey(element) != nil ? element : nil
}
}
Otherwise, you can just use Dictionary and use the name as both key and value.