Why does DocumentFile.getName take a relatively long time? [duplicate] - storage-access-framework

This question already has answers here:
Why is DocumentFile so slow, and what should I use instead?
(5 answers)
Closed 1 year ago.
I have seen posts describing DocumentFile.listFiles() takes a long time. In my case, it is fairly fast but operations on the retrieved instances of DocumentFile take a long time.
DocumentFile[] aFiles = dfDir.listFiles(); //Takes 100 ms with a list of 250 files
//The following takes 5,000ms
for (DocumentFile df : aFiles) {
boolean bFoo = df.isDirectory(); //takes about 10ms
String sName = df.getName(); //takes about 10ms
}
Could anyone shed some light on this?

getName() invokes ContentResolver#query() under the hood ... [this] performs hundreds of queries, which is very inefficient."
From an answer to the duplicate question you linked to.

Related

How do you use reduce(into:) in swift [duplicate]

This question already has answers here:
What is the reduce() function doing, in Swift
(4 answers)
Closed 9 months ago.
I am reading iOS 13 Programming Fundamentals with Swift, got to the part about reduce() and I think I understand it more or less, but then there is reduce(into:) and this piece of code:
let nums = [1,2,3,4,5]
let result = nums.reduce(into: [[],[]]) { temp, i in
temp[i%2].append(i)
}
// result is now [[2,4],[1,3,5]]
So this code takes an array of Int and splits it into 2 arrays, even and odd. The problem is that I have no idea what's happening inside the brackets {}.
In the case of reduce, the first parameter is the first one of the iteration and then the closure is supposed to process all the items one after the other, similar to map() but more powerful (here one loop is enough to get the two arrays but with map() I would need 2 loops, according to the book).
I cannot understand the syntax here anyway, especially what does "temp" stand for and that use of "in". And how is "append()" appending the value to the proper array??
Inside the closure, "temp" is the result format which is [[][]] and "i" is each number. As you said it processes all numbers in a loop. When % is used it returns the division remainder, so for the odd numbers like "1,3,5", it returns "1" and for the even numbers "0", which means that "temp" appends these values to the array in these respective indexes.
So if we debug and replace the variables for constants the results would be:
temp[1].append(1) //1%2 = 1/2 left 1 [[][1]]
temp[0].append(2) //2%2 = 2/2 left 0 [[2][1]]
temp[1].append(3) //3%2 = 3/2 = 1 left 1 [[2][1,3]]
temp[0].append(4) //4%2 = 4/2 left 0 [[2,4][1,3]]
temp[1].append(5) //5%2 = 5/2 = 2 left 1 [[2,4][1,3,5]]
According to the documentation the closure is called sequentially with a mutable accumulating value initialized that when exhausted, is returned to the caller.

Dividing two doubles gives wrong result in Xcode console [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I was trying some things in Xcode, and ran into an unexplained situation where Xcode gives me a wrong result for a simple division:
let a : Double = 0.235
let b : Double = 0.001
let nrOfDivisions = a / b
print("Divisions: ", nrOfDivisions) //prints 234.99999999999997
Strange enough, if I divide from 0.230 ... 0.234 to the same amount of 0.001, I get correct results, but starting from 0.235 ... 0.239 I get these wrong results.
I've tested now with 0.225, 0.226, 0.227, 0.245, 0.246, 0.247 and they all divide correctly.
What might be the issue here? It is a bug in Xcode, or am I missing something?
Well this is probably due to this issue: Why not use Double or Float to represent currency?. Were you thinking that Apple implemented floating point wrong? In the Java World, these questions came up quite often, and BigDecimal was the solution, you can read about that.

NSDictionary frome one key to many [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I got NSDictionary
Printing description of gameDict:
{
result = {
developer = "Bethesda Game Studios";
genre = "Role-Playing";
name = "The Elder Scrolls V: Skyrim";
platform = "PlayStation 3";
publisher = "Bethesda Softworks";
rating = M;
rlsdate = "2011-11-11";
score = 92;
summary = "The next chapter in the Elder Scrolls saga arrives from the Bethesda Game Studios. Skyrim reimagines the open-world fantasy epic, bringing to life a complete virtual world open for you to explore any way you choose. Play any type of character you can imagine, and do whatever you want; the legendary freedom of choice, storytelling, and adventure of The Elder Scrolls is realized like never before. Skyrim's new game engine brings to life a complete virtual world with rolling clouds, rugged mountains, bustling cities, lush fields, and ancient dungeons. Choose from hundreds of weapons, spells, and abilities. The new character system allows you to play any way you want and define yourself through your actions. Battle ancient dragons like you've never seen. As Dragonborn, learn their secrets and harness their power for yourself.";
thumbnail = "http://static.metacritic.com/images/products/games/6/372529409ea6cc3faadf5674fd96ba2a-98.jpg";
url = "http://www.metacritic.com/game/playstation-3/the-elder-scrolls-v-skyrim";
userscore = "6.3";
};
}
It got only one key "result"
How can i get other keys?
gameDict is an NSDictionary with one key/pair. The key "result" points to another NSDictionary which has lots more keys. So to access any of the deeper keys you can either do it inline:
NSNumber *score = gameDict[#"result"][#"score"];
Or you can pull out the Dictionary and query it
NSDictionary *resultDict = gameDict[#"result"];
NSNumber *score = resultDict[#"score"];

Table elements executing a function [duplicate]

This question already has answers here:
Search for an item in a Lua list
(12 answers)
Lua find a key from a value
(3 answers)
Closed 9 years ago.
I have this table:
maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
How can I make a script so if 1702169 (for example) is picked from the table, it prints ''That's the number''?
The easiest way to do what (i think) you want is with pairs() function. This is a stateless iterator which you can read more about here: http://www.lua.org/pil/7.3.html
If you simply want to scan through the entire table and see if it contains a value, then you can use this simple code:
local maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
local picked = 1702169
for i, v in pairs(maps) do
if v == picked then
print("That's the number")
break
end
end
The above code will iterate through the whole table where i is the key and v is the value of the table[key]=value pairs.
I am slightly unclear about your end goal, but you could create this into a function and/or modify it to your actual needs. Feel free to update your original post with more information and I can provide you with a more specific answer.

Can someone define what a closure is in real world language? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is a ‘Closure’?
I read more and more about closures but I don't really get what it is from a practical standpoint. I read the wikipedia page on it but it doesn't really clear anything up for me because I have more of a practical background in programming (self taught) as opposed to a computer science background. If this is a reundant question, I apologize as my initial search didn't yield anything that really answered it for me.
edit: Thanks for pointing me in the right direction! I see this has already been clearly answered before so I will close the question out.
Eric Lippert's blog does a pretty good job at explaining this in a practical sense.
And so does Kyle at SO
An operation is said to be closed over a set when the result of that operation also belongs to that set.
For example - Consider a set of positive integers. Addition is a closed operation over this set because adding two positive integers would always give a positive integer.
However subtraction is not closed over this set because 2 - 3 would give -1 which does not belong to the set of positive integers.
cheers
Two one sentence summaries:
• a closure is the local variables for a function - kept alive after the function has returned, or
• a closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)
http://blog.morrisjohns.com/javascript_closures_for_dummies
probably best demonstrated by an example
program output (artificially) prefixed by *
Javascript:
js> function newCounter() {
var i = 0;
var counterFunction = function () {
i += 1;
return i;
}
return counterFunction;
}
js> aCounter = newCounter()
* function () {
* i += 1;
* return i;
* }
js> aCounter()
* 1
js> aCounter()
* 2
js> aCounter()
* 3
js> bCounter = newCounter()
* function () {
* i += 1;
* return i;
* }
js> bCounter()
* 1
js> aCounter()
* 4
"Real world language" is a difficult measure to gauge objectively, but I'll give it a try since after all I also lack a CS background (EE major here) and am a little self-taught;-)
In many languages, a function "sees" more than one "scope" (group) of variables -- not only its local variables, and that of the module or namespace it's in, but also (if it's within another function) the local variables of the function that contains it.
So, for example (in Python, but many other languages work similarly!):
def outer(haystack):
def inner(needle):
eldeen = needle[::-1]
return (needle in haystack) or (eldeen in haystack)
return [x for x in ['gold','silver','diamond','straw'] if inner(x)]
inner can "see" haystack without needing to see it as an argument, just because its containing function outer has haystack "in scope" (i.e. "visible"). So far, so clear, I hope -- and this isn't yet about closures, it's about lexical scoping.
Now suppose the outer function (in a language treating functions as first-class objects, and in particular allowing them to be returned as results of other functions) the outer function returns the inner one instead of just calling it internally (in Python, that's for example what usually happens when you use decorator syntax #something).
How can the inner function (returned as a result) still refer to the outer function's variable, since the outer function has finished?
The answer is exactly this "closure" business -- those variables from the outer function which the inner (returned) function may still need are preserved and attached as attributes of the inner-function object that's returned.

Resources